svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / DEMO / PruebasGT2.java @ 10626
History | View | Annotate | Download (11.1 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.util.HashMap; |
52 |
import java.util.Map; |
53 |
|
54 |
import javax.swing.JOptionPane; |
55 |
|
56 |
import org.geotools.data.DataSourceException; |
57 |
import org.geotools.data.DataStore; |
58 |
import org.geotools.data.FeatureSource; |
59 |
import org.geotools.data.postgis.PostgisDataStore; |
60 |
import org.geotools.data.postgis.PostgisDataStoreFactory; |
61 |
import org.geotools.map.DefaultMapLayer; |
62 |
import org.geotools.map.MapLayer; |
63 |
import org.geotools.styling.Style; |
64 |
import org.geotools.styling.StyleBuilder; |
65 |
|
66 |
import com.iver.cit.gvsig.fmap.MapContext; |
67 |
import com.iver.cit.gvsig.fmap.layers.CancelationException; |
68 |
import com.iver.cit.gvsig.fmap.layers.FLyrGT2; |
69 |
import com.iver.cit.gvsig.fmap.layers.FLyrGT2_old; |
70 |
import com.vividsolutions.jts.geom.LineString; |
71 |
import com.vividsolutions.jts.geom.MultiLineString; |
72 |
import com.vividsolutions.jts.geom.MultiPoint; |
73 |
import com.vividsolutions.jts.geom.Point; |
74 |
|
75 |
/**
|
76 |
* Pruebas de capas GT2 (ArcSDE, etc), hechas por Fran, sacadas del
|
77 |
* CommandListener
|
78 |
* @author Luis W. Sevilla (sevilla_lui@gva.es)
|
79 |
*/
|
80 |
public class PruebasGT2 implements ActionListener { |
81 |
static PostgisDataStoreFactory postGisFactory = new PostgisDataStoreFactory(); |
82 |
// static ArcSDEDataStoreFactory arcSdeFactory = new ArcSDEDataStoreFactory();
|
83 |
|
84 |
Map remote;
|
85 |
private MapContext m_Mapa;
|
86 |
|
87 |
|
88 |
/**
|
89 |
* Returns the parameters
|
90 |
*/
|
91 |
public HashMap getParams() { |
92 |
HashMap params = new HashMap(); |
93 |
// Param[] dbParams = postGisFactory.getParametersInfo();
|
94 |
params.put("dbtype", "postgis"); //$NON-NLS-1$ |
95 |
params.put("host", "localhost"); |
96 |
params.put("port", new Integer(5432)); |
97 |
|
98 |
params.put("database", "latin1"); |
99 |
|
100 |
params.put("user", "postgres"); |
101 |
params.put("passwd", "aquilina"); |
102 |
|
103 |
params.put("wkb enabled", Boolean.TRUE); |
104 |
params.put("loose bbox", Boolean.TRUE); |
105 |
|
106 |
params.put("namespace", ""); //$NON-NLS-1$ |
107 |
|
108 |
return params;
|
109 |
} |
110 |
|
111 |
/**
|
112 |
*
|
113 |
*/
|
114 |
protected void addLayerGT2_Shp() { |
115 |
/* JFileChooser fileChooser = new JFileChooser(); // lastFolder);
|
116 |
fileChooser.addChoosableFileFilter(new SimpleFileFilter("shp", "Shapefile (*.shp)"));
|
117 |
|
118 |
int result = fileChooser.showOpenDialog(theView);
|
119 |
|
120 |
if (result == JFileChooser.APPROVE_OPTION) {
|
121 |
File file = fileChooser.getSelectedFile();
|
122 |
// lastFolder = file.getParentFile();
|
123 |
|
124 |
try {
|
125 |
// Load the file
|
126 |
URL url = file.toURL();
|
127 |
|
128 |
DataStore store;
|
129 |
|
130 |
// Para shapes
|
131 |
store = new ShapefileDataStore(url);
|
132 |
|
133 |
loadLayer(store, store.getTypeNames()[0]);
|
134 |
} catch (Throwable t) {
|
135 |
JOptionPane.showMessageDialog(null, "An error occurred while loading the file",
|
136 |
"Demo GT2", JOptionPane.ERROR_MESSAGE);
|
137 |
t.printStackTrace();
|
138 |
}
|
139 |
} */
|
140 |
} |
141 |
protected void addLayer_PostGIS() |
142 |
{ |
143 |
/* String nomTabla = JOptionPane.showInputDialog(null, "?Nombre de la tabla a cargar?", "provin");
|
144 |
PostGisDriver driver = new PostGisDriver();
|
145 |
String dburl = "jdbc:postgresql://localhost/latin1";
|
146 |
String dbuser = "postgres";
|
147 |
String dbpass = "aquilina";
|
148 |
String fields = "ASBINARY(the_geom, 'XDR') as the_geom, ND_4, NOM_PROVIN";
|
149 |
String whereClause = "";
|
150 |
|
151 |
|
152 |
Connection conn;
|
153 |
try {
|
154 |
Class.forName("org.postgresql.Driver");
|
155 |
conn = DriverManager.getConnection(dburl, dbuser, dbpass);
|
156 |
conn.setAutoCommit(false);
|
157 |
driver.setData(conn, nomTabla, fields, whereClause, -1);
|
158 |
FLayer lyr = LayerFactory.createDBLayer(driver, nomTabla, null);
|
159 |
m_Mapa.getLayers().addLayer(lyr);
|
160 |
} catch (SQLException e) {
|
161 |
// TODO Auto-generated catch block
|
162 |
e.printStackTrace();
|
163 |
} catch (ClassNotFoundException e) {
|
164 |
// TODO Auto-generated catch block
|
165 |
e.printStackTrace();
|
166 |
}
|
167 |
*/
|
168 |
} |
169 |
protected void addLayer_ArcSDE_propio() |
170 |
{ |
171 |
/* String nomTabla = "vias"; //JOptionPane.showInputDialog(null, "?Nombre de la tabla a cargar?", "vias");
|
172 |
ArcSdeDriver driver = new ArcSdeDriver();
|
173 |
String dbHost = "Alvaro";
|
174 |
String dbuser = "sde";
|
175 |
String dbpass = "sde";
|
176 |
// String[] fields = {"Shape", "NOM_PROVIN"};
|
177 |
String[] fields = null; //{"Shape", "RD_5", "RD_11"};
|
178 |
int instance = 5151;
|
179 |
String database = "sigespa";
|
180 |
|
181 |
String whereClause = "";
|
182 |
|
183 |
|
184 |
driver.setData(dbHost, instance, database, dbuser, dbpass, nomTabla, fields, whereClause);
|
185 |
FLayer lyr = LayerFactory.createArcSDELayer(nomTabla, driver, null);
|
186 |
m_Mapa.getLayers().addLayer(lyr);
|
187 |
*/
|
188 |
} |
189 |
|
190 |
protected void addLayer_mySQL() |
191 |
{ |
192 |
/* String nomTabla = JOptionPane.showInputDialog(null, "?Nombre de la tabla a cargar?", "provin");
|
193 |
MySQLDriver driver = new MySQLDriver();
|
194 |
String dburl = "jdbc:mysql://localhost/test";
|
195 |
String dbuser = "root";
|
196 |
String dbpass = "aquilina";
|
197 |
String fields = "ASBINARY(ogc_geom) as the_geom, ID, NOM_PROVIN, ND_4, ND_5, ND_14, AREA";
|
198 |
// String fields = "ASBINARY(ogc_geom) as the_geom, ID, RD_5, RD_11";
|
199 |
String whereClause = "";
|
200 |
|
201 |
|
202 |
Connection conn;
|
203 |
try {
|
204 |
Class.forName("com.mysql.jdbc.Driver");
|
205 |
// Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=monty&password=greatsqldb");
|
206 |
|
207 |
conn = DriverManager.getConnection(dburl, dbuser, dbpass);
|
208 |
conn.setAutoCommit(false);
|
209 |
driver.setData(conn, nomTabla, fields, whereClause, 2);
|
210 |
FLayer lyr = LayerFactory.createDBLayer(driver, nomTabla, null);
|
211 |
m_Mapa.getLayers().addLayer(lyr);
|
212 |
} catch (SQLException e) {
|
213 |
// TODO Auto-generated catch block
|
214 |
e.printStackTrace();
|
215 |
} catch (ClassNotFoundException e) {
|
216 |
// TODO Auto-generated catch block
|
217 |
e.printStackTrace();
|
218 |
}
|
219 |
*/
|
220 |
} |
221 |
|
222 |
protected void addLayerGT2_PostGIS() |
223 |
{ |
224 |
String nomTabla = JOptionPane.showInputDialog(null, "?Nombre de la tabla a cargar?", "provin"); |
225 |
|
226 |
remote = getParams(); |
227 |
/* remote.put("dbtype","postgis");
|
228 |
remote.put("host","localhost"); //Jos? Miguel
|
229 |
remote.put("port", new Integer(5432));
|
230 |
remote.put("database", "latin1");
|
231 |
remote.put("user", "postgres");
|
232 |
remote.put("passwd", "aquilina");
|
233 |
remote.put("charset", "");
|
234 |
remote.put("namespace", ""); */
|
235 |
|
236 |
PostgisDataStore store; |
237 |
try {
|
238 |
store = (PostgisDataStore) postGisFactory.createDataStore(remote); |
239 |
FLyrGT2 lyrGT2 = new FLyrGT2();
|
240 |
lyrGT2.setDataStore(store); |
241 |
lyrGT2.setTableName(nomTabla); |
242 |
m_Mapa.getLayers().addLayer(lyrGT2); |
243 |
// loadLayer(store, nomTabla);
|
244 |
|
245 |
} catch (IOException e) { |
246 |
// TODO Auto-generated catch block
|
247 |
e.printStackTrace(); |
248 |
} catch (CancelationException e) {
|
249 |
// TODO Auto-generated catch block
|
250 |
e.printStackTrace(); |
251 |
} |
252 |
} |
253 |
|
254 |
|
255 |
/**
|
256 |
* Load the data from the specified dataStore and construct a {@linkPlain Context context} with
|
257 |
* a default style.
|
258 |
*
|
259 |
* @param url The url of the shapefile to load.
|
260 |
* @param name DOCUMENT ME!
|
261 |
*
|
262 |
* @throws IOException is a I/O error occured.
|
263 |
* @throws DataSourceException if an error occured while reading the data source.
|
264 |
* @throws FileNotFoundException DOCUMENT ME!
|
265 |
*/
|
266 |
protected void loadLayer(DataStore store, String layerName) |
267 |
throws IOException, DataSourceException { |
268 |
long t1 = System.currentTimeMillis(); |
269 |
final FeatureSource features = store.getFeatureSource(layerName);
|
270 |
long t2 = System.currentTimeMillis(); |
271 |
System.out.println("t2-t1= " + (t2-t1)); |
272 |
// Create the style
|
273 |
final StyleBuilder builder = new StyleBuilder(); |
274 |
final Style style; |
275 |
Class geometryClass = features.getSchema().getDefaultGeometry().getType();
|
276 |
|
277 |
if (LineString.class.isAssignableFrom(geometryClass)
|
278 |
|| MultiLineString.class.isAssignableFrom(geometryClass)) { |
279 |
style = builder.createStyle(builder.createLineSymbolizer()); |
280 |
} else if (Point.class.isAssignableFrom(geometryClass) |
281 |
|| MultiPoint.class.isAssignableFrom(geometryClass)) { |
282 |
style = builder.createStyle(builder.createPointSymbolizer()); |
283 |
} else {
|
284 |
style = builder.createStyle(builder.createPolygonSymbolizer(Color.ORANGE, Color.BLACK, 1)); |
285 |
} |
286 |
|
287 |
final MapLayer layer = new DefaultMapLayer(features, style); |
288 |
layer.setTitle(layerName); |
289 |
|
290 |
FLyrGT2_old lyrGT2 = new FLyrGT2_old(layer);
|
291 |
try {
|
292 |
m_Mapa.getLayers().addLayer(lyrGT2); |
293 |
} catch (CancelationException e) {
|
294 |
// TODO Auto-generated catch block
|
295 |
e.printStackTrace(); |
296 |
} |
297 |
|
298 |
} |
299 |
|
300 |
protected void addLayerGT2_ArcSDE() |
301 |
{ |
302 |
/* remote = new HashMap();
|
303 |
remote.put("dbtype","arcsde");
|
304 |
remote.put("server","Alvaro"); //Jos? Miguel
|
305 |
remote.put("port", new Integer(5151));
|
306 |
remote.put("instance", "sigespa");
|
307 |
remote.put("user", "sde");
|
308 |
remote.put("password", "sde");
|
309 |
|
310 |
|
311 |
DataStore store;
|
312 |
try {
|
313 |
store = arcSdeFactory.createDataStore(remote);
|
314 |
loadLayer(store, "SDE.VIAS");
|
315 |
|
316 |
} catch (IOException e) {
|
317 |
// TODO Auto-generated catch block
|
318 |
e.printStackTrace();
|
319 |
} */
|
320 |
|
321 |
|
322 |
} |
323 |
|
324 |
public void setMapContext(MapContext map) |
325 |
{ |
326 |
m_Mapa = map; |
327 |
} |
328 |
public void actionPerformed(ActionEvent e) { |
329 |
|
330 |
if (e.getActionCommand() == "ADD_GT2_POSTGIS_PROPIO") { |
331 |
addLayer_PostGIS(); |
332 |
} |
333 |
if (e.getActionCommand() == "ADD_GT2_MYSQL_PROPIO") { |
334 |
addLayer_mySQL(); |
335 |
} |
336 |
if (e.getActionCommand() == "ADD_GT2_ARCSDE_PROPIO") { |
337 |
addLayer_ArcSDE_propio(); |
338 |
} |
339 |
|
340 |
if (e.getActionCommand() == "ADD_GT2_POSTGIS") { |
341 |
addLayerGT2_PostGIS(); |
342 |
} |
343 |
if (e.getActionCommand() == "ADD_GT2_SHP") { |
344 |
addLayerGT2_Shp(); |
345 |
} |
346 |
if (e.getActionCommand() == "ADD_GT2_ARCSDE") { |
347 |
addLayerGT2_ArcSDE(); |
348 |
} |
349 |
} |
350 |
} |