root / branches / gvSIG_03_SLD / applications / appgvSIG / src / com / iver / cit / gvsig / DEMO / PruebasGT2.java @ 2131
History | View | Annotate | Download (9.75 KB)
1 |
/*
|
---|---|
2 |
* Created on 22-dic-2004
|
3 |
*/
|
4 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
5 |
*
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Generalitat Valenciana
|
25 |
* Conselleria d'Infraestructures i Transport
|
26 |
* Av. Blasco Ib??ez, 50
|
27 |
* 46010 VALENCIA
|
28 |
* SPAIN
|
29 |
*
|
30 |
* +34 963862235
|
31 |
* gvsig@gva.es
|
32 |
* www.gvsig.gva.es
|
33 |
*
|
34 |
* or
|
35 |
*
|
36 |
* IVER T.I. S.A
|
37 |
* Salamanca 50
|
38 |
* 46005 Valencia
|
39 |
* Spain
|
40 |
*
|
41 |
* +34 963163400
|
42 |
* dac@iver.es
|
43 |
*/
|
44 |
package com.iver.cit.gvsig.DEMO; |
45 |
|
46 |
import java.awt.Color; |
47 |
import java.awt.event.ActionEvent; |
48 |
import java.awt.event.ActionListener; |
49 |
import java.io.FileNotFoundException; |
50 |
import java.io.IOException; |
51 |
import java.sql.Connection; |
52 |
import java.sql.DriverManager; |
53 |
import java.sql.SQLException; |
54 |
import java.util.HashMap; |
55 |
import java.util.Map; |
56 |
|
57 |
import javax.swing.JOptionPane; |
58 |
|
59 |
import org.geotools.data.DataSourceException; |
60 |
import org.geotools.data.DataStore; |
61 |
import org.geotools.data.FeatureSource; |
62 |
//import org.geotools.data.postgis.PostgisDataStoreFactory;
|
63 |
import org.geotools.map.DefaultMapLayer; |
64 |
import org.geotools.map.MapLayer; |
65 |
import org.geotools.styling.Style; |
66 |
import org.geotools.styling.StyleBuilder; |
67 |
|
68 |
import com.iver.cit.gvsig.fmap.FMap; |
69 |
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver; |
70 |
import com.iver.cit.gvsig.fmap.drivers.jdbc.mysql.MySQLDriver; |
71 |
import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGisDriver; |
72 |
import com.iver.cit.gvsig.fmap.layers.CancelationException; |
73 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
74 |
import com.iver.cit.gvsig.fmap.layers.FLyrGT2; |
75 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
76 |
import com.vividsolutions.jts.geom.LineString; |
77 |
import com.vividsolutions.jts.geom.MultiLineString; |
78 |
import com.vividsolutions.jts.geom.MultiPoint; |
79 |
import com.vividsolutions.jts.geom.Point; |
80 |
|
81 |
/**
|
82 |
* Pruebas de capas GT2 (ArcSDE, etc), hechas por Fran, sacadas del
|
83 |
* CommandListener
|
84 |
* @author Luis W. Sevilla (sevilla_lui@gva.es)
|
85 |
*/
|
86 |
public class PruebasGT2 implements ActionListener { |
87 |
// static PostgisDataStoreFactory postGisFactory = new PostgisDataStoreFactory();
|
88 |
// static ArcSDEDataStoreFactory arcSdeFactory = new ArcSDEDataStoreFactory();
|
89 |
|
90 |
Map remote;
|
91 |
private FMap m_Mapa;
|
92 |
|
93 |
/**
|
94 |
*
|
95 |
*/
|
96 |
protected void addLayerGT2_Shp() { |
97 |
/* JFileChooser fileChooser = new JFileChooser(); // lastFolder);
|
98 |
fileChooser.addChoosableFileFilter(new SimpleFileFilter("shp", "Shapefile (*.shp)"));
|
99 |
|
100 |
int result = fileChooser.showOpenDialog(theView);
|
101 |
|
102 |
if (result == JFileChooser.APPROVE_OPTION) {
|
103 |
File file = fileChooser.getSelectedFile();
|
104 |
// lastFolder = file.getParentFile();
|
105 |
|
106 |
try {
|
107 |
// Load the file
|
108 |
URL url = file.toURL();
|
109 |
|
110 |
DataStore store;
|
111 |
|
112 |
// Para shapes
|
113 |
store = new ShapefileDataStore(url);
|
114 |
|
115 |
loadLayer(store, store.getTypeNames()[0]);
|
116 |
} catch (Throwable t) {
|
117 |
JOptionPane.showMessageDialog(null, "An error occurred while loading the file",
|
118 |
"Demo GT2", JOptionPane.ERROR_MESSAGE);
|
119 |
t.printStackTrace();
|
120 |
}
|
121 |
} */
|
122 |
} |
123 |
protected void addLayer_PostGIS() |
124 |
{ |
125 |
String nomTabla = JOptionPane.showInputDialog(null, "?Nombre de la tabla a cargar?", "provin"); |
126 |
VectorialDatabaseDriver driver = new PostGisDriver();
|
127 |
String dburl = "jdbc:postgresql://localhost/latin1"; |
128 |
String dbuser = "postgres"; |
129 |
String dbpass = "aquilina"; |
130 |
String fields = "ASBINARY(the_geom, 'XDR') as the_geom, ND_4, NOM_PROVIN"; |
131 |
String whereClause = ""; |
132 |
|
133 |
|
134 |
Connection conn;
|
135 |
try {
|
136 |
Class.forName("org.postgresql.Driver"); |
137 |
conn = DriverManager.getConnection(dburl, dbuser, dbpass);
|
138 |
conn.setAutoCommit(false);
|
139 |
driver.setData(conn, nomTabla, fields, whereClause, -1);
|
140 |
FLayer lyr = LayerFactory.createDBLayer(driver, nomTabla, null);
|
141 |
m_Mapa.getLayers().addLayer(lyr); |
142 |
} catch (SQLException e) { |
143 |
// TODO Auto-generated catch block
|
144 |
e.printStackTrace(); |
145 |
} catch (ClassNotFoundException e) { |
146 |
// TODO Auto-generated catch block
|
147 |
e.printStackTrace(); |
148 |
} |
149 |
|
150 |
} |
151 |
protected void addLayer_mySQL() |
152 |
{ |
153 |
String nomTabla = JOptionPane.showInputDialog(null, "?Nombre de la tabla a cargar?", "provin"); |
154 |
VectorialDatabaseDriver driver = new MySQLDriver();
|
155 |
String dburl = "jdbc:mysql://localhost/test"; |
156 |
String dbuser = "root"; |
157 |
String dbpass = "aquilina"; |
158 |
String fields = "ASBINARY(ogc_geom) as the_geom, ID, NOM_PROVIN, ND_4, ND_5, ND_14, AREA"; |
159 |
// String fields = "ASBINARY(ogc_geom) as the_geom, ID, RD_5, RD_11";
|
160 |
String whereClause = ""; |
161 |
|
162 |
|
163 |
Connection conn;
|
164 |
try {
|
165 |
Class.forName("com.mysql.jdbc.Driver"); |
166 |
// Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=monty&password=greatsqldb");
|
167 |
|
168 |
conn = DriverManager.getConnection(dburl, dbuser, dbpass);
|
169 |
conn.setAutoCommit(false);
|
170 |
driver.setData(conn, nomTabla, fields, whereClause, 2);
|
171 |
FLayer lyr = LayerFactory.createDBLayer(driver, nomTabla, null);
|
172 |
m_Mapa.getLayers().addLayer(lyr); |
173 |
} catch (SQLException e) { |
174 |
// TODO Auto-generated catch block
|
175 |
e.printStackTrace(); |
176 |
} catch (ClassNotFoundException e) { |
177 |
// TODO Auto-generated catch block
|
178 |
e.printStackTrace(); |
179 |
} |
180 |
|
181 |
} |
182 |
|
183 |
protected void addLayerGT2_PostGIS() |
184 |
{ |
185 |
/* String nomTabla = JOptionPane.showInputDialog(null, "?Nombre de la tabla a cargar?", "provin");
|
186 |
|
187 |
remote = new HashMap();
|
188 |
remote.put("dbtype","postgis");
|
189 |
remote.put("host","localhost"); //Jos? Miguel
|
190 |
remote.put("port", new Integer(5432));
|
191 |
remote.put("database", "latin1");
|
192 |
remote.put("user", "postgres");
|
193 |
remote.put("passwd", "aquilina");
|
194 |
remote.put("charset", "");
|
195 |
remote.put("namespace", "");
|
196 |
|
197 |
DataStore store;
|
198 |
try {
|
199 |
store = postGisFactory.createDataStore(remote);
|
200 |
loadLayer(store, nomTabla);
|
201 |
|
202 |
} catch (IOException e) {
|
203 |
// TODO Auto-generated catch block
|
204 |
e.printStackTrace();
|
205 |
} */
|
206 |
} |
207 |
|
208 |
|
209 |
/**
|
210 |
* Load the data from the specified dataStore and construct a {@linkPlain Context context} with
|
211 |
* a default style.
|
212 |
*
|
213 |
* @param url The url of the shapefile to load.
|
214 |
* @param name DOCUMENT ME!
|
215 |
*
|
216 |
* @throws IOException is a I/O error occured.
|
217 |
* @throws DataSourceException if an error occured while reading the data source.
|
218 |
* @throws FileNotFoundException DOCUMENT ME!
|
219 |
*/
|
220 |
protected void loadLayer(DataStore store, String layerName) |
221 |
throws IOException, DataSourceException { |
222 |
long t1 = System.currentTimeMillis(); |
223 |
final FeatureSource features = store.getFeatureSource(layerName);
|
224 |
long t2 = System.currentTimeMillis(); |
225 |
System.out.println("t2-t1= " + (t2-t1)); |
226 |
// Create the style
|
227 |
final StyleBuilder builder = new StyleBuilder(); |
228 |
final Style style; |
229 |
Class geometryClass = features.getSchema().getDefaultGeometry().getType();
|
230 |
|
231 |
if (LineString.class.isAssignableFrom(geometryClass)
|
232 |
|| MultiLineString.class.isAssignableFrom(geometryClass)) { |
233 |
style = builder.createStyle(builder.createLineSymbolizer()); |
234 |
} else if (Point.class.isAssignableFrom(geometryClass) |
235 |
|| MultiPoint.class.isAssignableFrom(geometryClass)) { |
236 |
style = builder.createStyle(builder.createPointSymbolizer()); |
237 |
} else {
|
238 |
style = builder.createStyle(builder.createPolygonSymbolizer(Color.ORANGE, Color.BLACK, 1)); |
239 |
} |
240 |
|
241 |
final MapLayer layer = new DefaultMapLayer(features, style); |
242 |
layer.setTitle(layerName); |
243 |
|
244 |
FLyrGT2 lyrGT2 = new FLyrGT2(layer);
|
245 |
try {
|
246 |
m_Mapa.getLayers().addLayer(lyrGT2); |
247 |
} catch (CancelationException e) {
|
248 |
// TODO Auto-generated catch block
|
249 |
e.printStackTrace(); |
250 |
} |
251 |
|
252 |
} |
253 |
|
254 |
protected void addLayerGT2_ArcSDE() |
255 |
{ |
256 |
remote = new HashMap(); |
257 |
remote.put("dbtype","arcsde"); |
258 |
remote.put("server","192.168.0.165"); //Jos? Miguel |
259 |
remote.put("port", new Integer(5151)); |
260 |
remote.put("instance", "sde"); |
261 |
remote.put("user", "sde"); |
262 |
remote.put("password", "aquilina"); |
263 |
|
264 |
|
265 |
DataStore store; |
266 |
/* try {
|
267 |
store = arcSdeFactory.createDataStore(remote);
|
268 |
loadLayer(store, "SDE.VIAS");
|
269 |
|
270 |
} catch (IOException e) {
|
271 |
// TODO Auto-generated catch block
|
272 |
e.printStackTrace();
|
273 |
} */
|
274 |
|
275 |
|
276 |
} |
277 |
|
278 |
public void setMapContext(FMap map) |
279 |
{ |
280 |
m_Mapa = map; |
281 |
} |
282 |
public void actionPerformed(ActionEvent e) { |
283 |
|
284 |
if (e.getActionCommand() == "ADD_GT2_POSTGIS_PROPIO") { |
285 |
addLayer_PostGIS(); |
286 |
} |
287 |
if (e.getActionCommand() == "ADD_GT2_MYSQL_PROPIO") { |
288 |
addLayer_mySQL(); |
289 |
} |
290 |
|
291 |
if (e.getActionCommand() == "ADD_GT2_POSTGIS") { |
292 |
addLayerGT2_PostGIS(); |
293 |
} |
294 |
if (e.getActionCommand() == "ADD_GT2_SHP") { |
295 |
addLayerGT2_Shp(); |
296 |
} |
297 |
if (e.getActionCommand() == "ADD_GT2_ARCSDE") { |
298 |
addLayerGT2_ArcSDE(); |
299 |
} |
300 |
} |
301 |
} |