svn-gvsig-desktop / trunk / extensions / extWFS / src / com / iver / cit / gvsig / fmap / drivers / wfs / WFSDriver.java @ 2277
History | View | Annotate | Download (12.7 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.cit.gvsig.fmap.drivers.wfs; |
42 |
|
43 |
import java.awt.geom.Rectangle2D; |
44 |
import java.io.IOException; |
45 |
import java.net.MalformedURLException; |
46 |
import java.net.URL; |
47 |
import java.sql.SQLException; |
48 |
import java.util.ArrayList; |
49 |
import java.util.HashMap; |
50 |
import java.util.Hashtable; |
51 |
import java.util.Map; |
52 |
|
53 |
import org.geotools.data.DataStore; |
54 |
import org.geotools.data.DefaultQuery; |
55 |
import org.geotools.data.FeatureReader; |
56 |
import org.geotools.data.Query; |
57 |
import org.geotools.data.Transaction; |
58 |
import org.geotools.data.wfs.WFSDataStoreFactory; |
59 |
import org.geotools.feature.AttributeType; |
60 |
import org.geotools.filter.FidFilter; |
61 |
import org.geotools.filter.Filter; |
62 |
import org.geotools.filter.FilterFactory; |
63 |
|
64 |
import com.hardcode.gdbms.engine.data.driver.ObjectDriver; |
65 |
import com.hardcode.gdbms.engine.data.edition.DataWare; |
66 |
import com.hardcode.gdbms.engine.values.Value; |
67 |
import com.iver.cit.gvsig.fmap.DriverException; |
68 |
import com.iver.cit.gvsig.fmap.core.FShape; |
69 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
70 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
71 |
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes; |
72 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
73 |
import com.iver.cit.gvsig.fmap.drivers.IWFSdriver; |
74 |
|
75 |
|
76 |
/**
|
77 |
* Driver de WFS, utilizando DataStore de geotools.
|
78 |
*
|
79 |
* @author Vicente Caballero Navarro
|
80 |
*/
|
81 |
public class WFSDriver implements IWFSdriver, ObjectDriver { |
82 |
private Rectangle2D fullExtent = null; |
83 |
private Rectangle2D extent = null; |
84 |
private DataStore ds;
|
85 |
|
86 |
//private FeatureReader fr;
|
87 |
private String urlS = "http://localhost:8080/geoserver/wfs"; |
88 |
private String versionS = "version=1.0.0"; |
89 |
private String requestS = "request=getcapabilities"; |
90 |
private String serviceS = "service=wfs"; |
91 |
private String userS = "admin"; |
92 |
private String pwdS = "geoserver"; |
93 |
private String[] layersS; |
94 |
private boolean protocolS = true; |
95 |
private int numFeaturesS = 100; |
96 |
private int timeoutS = 10000; |
97 |
private Hashtable hashRelate; |
98 |
|
99 |
///private Hashtable indexFID;
|
100 |
protected int idFID_FieldName; |
101 |
private int numReg = -1; |
102 |
private int numField = -1; |
103 |
private AttributeType[] attributes; |
104 |
|
105 |
//private FeatureReader fr;
|
106 |
///private ArrayList typeNames=new ArrayList();
|
107 |
private ArrayList features = new ArrayList(); |
108 |
|
109 |
/**
|
110 |
* DOCUMENT ME!
|
111 |
*
|
112 |
* @return DOCUMENT ME!
|
113 |
*/
|
114 |
public Rectangle2D getFullExtent() { |
115 |
return fullExtent;
|
116 |
} |
117 |
|
118 |
/**
|
119 |
* DOCUMENT ME!
|
120 |
*
|
121 |
* @return DOCUMENT ME!
|
122 |
*/
|
123 |
public int getShapeType() { |
124 |
return FShape.MULTI;
|
125 |
} |
126 |
|
127 |
/**
|
128 |
* DOCUMENT ME!
|
129 |
*
|
130 |
* @param fids DOCUMENT ME!
|
131 |
*
|
132 |
* @return DOCUMENT ME!
|
133 |
*/
|
134 |
private Filter createFidFilter(String[] fids) { |
135 |
FidFilter filter = FilterFactory.createFilterFactory().createFidFilter(); |
136 |
|
137 |
for (int i = 0; i < fids.length; i++) { |
138 |
filter.addFid(fids[i]); |
139 |
} |
140 |
|
141 |
return filter;
|
142 |
} |
143 |
|
144 |
/**
|
145 |
* DOCUMENT ME!
|
146 |
*
|
147 |
* @param index DOCUMENT ME!
|
148 |
*
|
149 |
* @return DOCUMENT ME!
|
150 |
*/
|
151 |
public IGeometry getShape(int index) { |
152 |
return ((IFeature) features.get(index)).getGeometry();
|
153 |
} |
154 |
|
155 |
/**
|
156 |
* DOCUMENT ME!
|
157 |
*
|
158 |
* @return DOCUMENT ME!
|
159 |
*/
|
160 |
public URL getHost() { |
161 |
try {
|
162 |
return new URL(urlS + "?" + versionS + "&" + requestS + "&" + |
163 |
serviceS); |
164 |
} catch (MalformedURLException e) { |
165 |
e.printStackTrace(); |
166 |
} |
167 |
|
168 |
return null; |
169 |
} |
170 |
|
171 |
/**
|
172 |
* DOCUMENT ME!
|
173 |
*
|
174 |
* @param r DOCUMENT ME!
|
175 |
* @param strEPSG DOCUMENT ME!
|
176 |
*
|
177 |
* @return DOCUMENT ME!
|
178 |
*
|
179 |
* @throws DriverException DOCUMENT ME!
|
180 |
* @throws IOException
|
181 |
*/
|
182 |
public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) |
183 |
throws DriverException, IOException { |
184 |
WFSFeatureIterator geomIterator = null;
|
185 |
|
186 |
/*if (r!=null && extent.contains(r)){
|
187 |
//extent=(Rectangle2D)r.clone();
|
188 |
return new WFSFeatureIterator(features);
|
189 |
}
|
190 |
*/
|
191 |
|
192 |
// ArrayList shapes=new ArrayList();
|
193 |
|
194 |
/* BBoxExpressionImpl RectangleFilter=null;
|
195 |
try {
|
196 |
RectangleFilter = (BBoxExpressionImpl)FilterFactoryImpl.createFilterFactory().createBBoxExpression(new Envelope(fullExtent.getX(),fullExtent.getY(),fullExtent.getMaxX(),fullExtent.getMaxY()));
|
197 |
} catch (IllegalFilterException e2) {
|
198 |
e2.printStackTrace();
|
199 |
} catch (FactoryConfigurationError e2) {
|
200 |
e2.printStackTrace();
|
201 |
}
|
202 |
*/
|
203 |
Query query = new DefaultQuery(layersS[0]); |
204 |
FeatureReader fr = null;
|
205 |
|
206 |
|
207 |
fr = ds.getFeatureReader(query, Transaction.AUTO_COMMIT); |
208 |
|
209 |
if (numField == -1) { |
210 |
numField = fr.getFeatureType().getAttributeCount(); |
211 |
attributes = fr.getFeatureType().getAttributeTypes(); |
212 |
} |
213 |
|
214 |
geomIterator = new WFSFeatureIterator(fr);
|
215 |
return geomIterator;
|
216 |
} |
217 |
|
218 |
/**
|
219 |
* DOCUMENT ME!
|
220 |
*/
|
221 |
public void close() { |
222 |
} |
223 |
|
224 |
/**
|
225 |
* DOCUMENT ME!
|
226 |
*
|
227 |
* @throws DriverException DOCUMENT ME!
|
228 |
*/
|
229 |
public void open() throws DriverException { |
230 |
} |
231 |
|
232 |
/**
|
233 |
* DOCUMENT ME!
|
234 |
*
|
235 |
* @param FID DOCUMENT ME!
|
236 |
*
|
237 |
* @return DOCUMENT ME!
|
238 |
*/
|
239 |
public int getRowIndexByFID(IFeature FID) { |
240 |
int resul;
|
241 |
Integer rowIndex = (Integer) hashRelate.get(FID.getID()); |
242 |
resul = rowIndex.intValue(); |
243 |
|
244 |
return resul;
|
245 |
} |
246 |
|
247 |
/**
|
248 |
* DOCUMENT ME!
|
249 |
*
|
250 |
* @return DOCUMENT ME!
|
251 |
*
|
252 |
* @throws IOException DOCUMENT ME!
|
253 |
*/
|
254 |
public int getShapeCount() throws IOException { |
255 |
return numReg;
|
256 |
} |
257 |
|
258 |
/**
|
259 |
* DOCUMENT ME!
|
260 |
*
|
261 |
* @return DOCUMENT ME!
|
262 |
*/
|
263 |
public DriverAttributes getDriverAttributes() {
|
264 |
return null; |
265 |
} |
266 |
|
267 |
/**
|
268 |
* DOCUMENT ME!
|
269 |
*
|
270 |
* @return DOCUMENT ME!
|
271 |
*/
|
272 |
public String getName() { |
273 |
return "WFS Driver"; |
274 |
} |
275 |
|
276 |
/**
|
277 |
* Recorre el recordset creando una tabla Hash que usaremos para
|
278 |
* relacionar el n?mero de un registro con su identificador ?nico. Debe
|
279 |
* ser llamado en el setData justo despu?s de crear el recorset principal
|
280 |
* @throws IOException
|
281 |
*/
|
282 |
protected void doRelateID_FID() throws IOException { |
283 |
if (ds == null) { |
284 |
URL url = null; |
285 |
|
286 |
try {
|
287 |
url = new URL(urlS + "?" + versionS + "&" + requestS + "&" + |
288 |
serviceS); |
289 |
} catch (MalformedURLException e) { |
290 |
e.printStackTrace(); |
291 |
} |
292 |
|
293 |
//int timeout=10000;
|
294 |
//boolean protocol=true;
|
295 |
Map m = new HashMap(); |
296 |
m.put(WFSDataStoreFactory.URL.key, url); |
297 |
m.put(WFSDataStoreFactory.TIMEOUT.key, new Integer(timeoutS)); |
298 |
m.put(WFSDataStoreFactory.PROTOCOL.key, new Boolean(protocolS)); |
299 |
m.put(WFSDataStoreFactory.PASSWORD.key, pwdS); |
300 |
m.put(WFSDataStoreFactory.USERNAME.key, userS); |
301 |
m.put(WFSDataStoreFactory.BUFFER_SIZE.key, new Integer(numFeaturesS)); |
302 |
|
303 |
try {
|
304 |
ds = (new WFSDataStoreFactory()).createNewDataStore(m);
|
305 |
} catch (IOException e) { |
306 |
e.printStackTrace(); |
307 |
} |
308 |
|
309 |
/*
|
310 |
String[] names=null;
|
311 |
try {
|
312 |
names = ds.getTypeNames();
|
313 |
} catch (IOException e1) {
|
314 |
e1.printStackTrace();
|
315 |
}
|
316 |
System.out.println("Capas :");
|
317 |
|
318 |
for (int i=0;i<names.length;i++){
|
319 |
System.out.println("Capa "+i+" : "+names[i]);
|
320 |
//Se selecciona la capa que se quiere abrir
|
321 |
//if (true)//si esta seleccionada en la lista o arbol que se muestre en el wizard
|
322 |
//typeNames.add(names[i]);
|
323 |
}
|
324 |
*/
|
325 |
} |
326 |
|
327 |
hashRelate = new Hashtable(); |
328 |
|
329 |
///indexFID = new Hashtable();
|
330 |
//Query query = new DefaultQuery(layersS[0]);
|
331 |
|
332 |
//FeatureReader fr=null;
|
333 |
//fr = ds.getFeatureReader(query,Transaction.AUTO_COMMIT);
|
334 |
int index = 0; |
335 |
IFeatureIterator iter = null;
|
336 |
|
337 |
try {
|
338 |
iter = getFeatureIterator(null, null); |
339 |
} catch (DriverException e) {
|
340 |
e.printStackTrace(); |
341 |
} |
342 |
|
343 |
try {
|
344 |
while (iter.hasNext()) {
|
345 |
IFeature feature = iter.next(); |
346 |
features.add(feature); |
347 |
|
348 |
Rectangle2D rect = feature.getGeometry().getBounds2D();
|
349 |
|
350 |
if (fullExtent == null) { |
351 |
fullExtent = new Rectangle2D.Double(rect.getMinX(), |
352 |
rect.getMinY(), rect.getWidth(), rect.getHeight()); |
353 |
} else {
|
354 |
fullExtent.add(new Rectangle2D.Double(rect.getMinX(), |
355 |
rect.getMinY(), rect.getWidth(), rect.getHeight())); |
356 |
} |
357 |
|
358 |
hashRelate.put(feature.getID(), new Integer(index)); |
359 |
|
360 |
///indexFID.put(new Integer(index),feature.getID());
|
361 |
index++; |
362 |
} |
363 |
} catch (SQLException e) { |
364 |
e.printStackTrace(); |
365 |
} |
366 |
|
367 |
numReg = index; |
368 |
extent = (Rectangle2D)fullExtent.clone();
|
369 |
} |
370 |
|
371 |
/**
|
372 |
* DOCUMENT ME!
|
373 |
*
|
374 |
* @param url DOCUMENT ME!
|
375 |
* @param user DOCUMENT ME!
|
376 |
* @param pwd DOCUMENT ME!
|
377 |
* @param layers DOCUMENT ME!
|
378 |
* @param protocol DOCUMENT ME!
|
379 |
* @param numFeatures DOCUMENT ME!
|
380 |
* @param timeout DOCUMENT ME!
|
381 |
* @param id_FID_field DOCUMENT ME!
|
382 |
* @throws IOException
|
383 |
*/
|
384 |
public void setData(String url, String user, String pwd, String[] layers, |
385 |
boolean protocol, int numFeatures, int timeout, int id_FID_field) throws IOException { |
386 |
idFID_FieldName = id_FID_field; |
387 |
urlS = url; |
388 |
userS = user; |
389 |
pwdS = pwd; |
390 |
layersS = layers; |
391 |
protocolS = protocol; |
392 |
numFeaturesS = numFeatures; |
393 |
timeoutS = timeout; |
394 |
doRelateID_FID(); |
395 |
} |
396 |
|
397 |
/**
|
398 |
* DOCUMENT ME!
|
399 |
*
|
400 |
* @param arg0 DOCUMENT ME!
|
401 |
* @param arg1 DOCUMENT ME!
|
402 |
*
|
403 |
* @return DOCUMENT ME!
|
404 |
*
|
405 |
* @throws com.hardcode.gdbms.engine.data.driver.DriverException DOCUMENT ME!
|
406 |
*/
|
407 |
public Value getFieldValue(long arg0, int arg1) |
408 |
throws com.hardcode.gdbms.engine.data.driver.DriverException {
|
409 |
/*try {
|
410 |
ds.getTypeNames();
|
411 |
} catch (IOException e1) {
|
412 |
e1.printStackTrace();
|
413 |
}*/
|
414 |
/*System.out.println("Filter");
|
415 |
Filter fidFilter=createFilter(new String[]{(String)indexFID.get(new Integer((int)arg0))});
|
416 |
System.out.println("Query");
|
417 |
Query query = new DefaultQuery((String)typeNames.get(0),fidFilter);
|
418 |
FeatureReader fr=null;
|
419 |
try {
|
420 |
System.out.println("getFeatureReader");
|
421 |
fr = ds.getFeatureReader(query,Transaction.AUTO_COMMIT);
|
422 |
} catch (IOException e) {
|
423 |
e.printStackTrace();
|
424 |
}
|
425 |
try {
|
426 |
return getValue(fr,arg1);
|
427 |
} catch (NoSuchElementException e) {
|
428 |
e.printStackTrace();
|
429 |
} catch (IOException e) {
|
430 |
e.printStackTrace();
|
431 |
} catch (IllegalAttributeException e) {
|
432 |
e.printStackTrace();
|
433 |
}
|
434 |
*/
|
435 |
return ((IFeature) features.get((int) arg0)).getAttribute(arg1); |
436 |
} |
437 |
|
438 |
/**
|
439 |
* DOCUMENT ME!
|
440 |
*
|
441 |
* @return DOCUMENT ME!
|
442 |
*
|
443 |
* @throws com.hardcode.gdbms.engine.data.driver.DriverException DOCUMENT ME!
|
444 |
*/
|
445 |
public int getFieldCount() |
446 |
throws com.hardcode.gdbms.engine.data.driver.DriverException {
|
447 |
return numField;
|
448 |
} |
449 |
|
450 |
/**
|
451 |
* DOCUMENT ME!
|
452 |
*
|
453 |
* @param arg0 DOCUMENT ME!
|
454 |
*
|
455 |
* @return DOCUMENT ME!
|
456 |
*
|
457 |
* @throws com.hardcode.gdbms.engine.data.driver.DriverException DOCUMENT ME!
|
458 |
*/
|
459 |
public String getFieldName(int arg0) |
460 |
throws com.hardcode.gdbms.engine.data.driver.DriverException {
|
461 |
return attributes[arg0].getName();
|
462 |
} |
463 |
|
464 |
/**
|
465 |
* DOCUMENT ME!
|
466 |
*
|
467 |
* @return DOCUMENT ME!
|
468 |
*
|
469 |
* @throws com.hardcode.gdbms.engine.data.driver.DriverException DOCUMENT ME!
|
470 |
*/
|
471 |
public long getRowCount() |
472 |
throws com.hardcode.gdbms.engine.data.driver.DriverException {
|
473 |
return numReg;
|
474 |
} |
475 |
|
476 |
/**
|
477 |
* DOCUMENT ME!
|
478 |
*
|
479 |
* @param arg0 DOCUMENT ME!
|
480 |
*
|
481 |
* @return DOCUMENT ME!
|
482 |
*
|
483 |
* @throws com.hardcode.gdbms.engine.data.driver.DriverException DOCUMENT ME!
|
484 |
*/
|
485 |
public int getFieldType(int arg0) |
486 |
throws com.hardcode.gdbms.engine.data.driver.DriverException {
|
487 |
attributes[arg0].getType(); |
488 |
|
489 |
return FIELD_TYPE_STRING; //fr.getFeatureType().getAttributeType(arg0).getType(); |
490 |
} |
491 |
|
492 |
/**
|
493 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
|
494 |
*/
|
495 |
public int[] getPrimaryKeys() throws com.hardcode.gdbms.engine.data.driver.DriverException { |
496 |
throw new UnsupportedOperationException(); |
497 |
} |
498 |
|
499 |
/**
|
500 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
|
501 |
*/
|
502 |
public void write(DataWare arg0) throws com.hardcode.gdbms.engine.data.driver.DriverException { |
503 |
// TODO Auto-generated method stub
|
504 |
|
505 |
} |
506 |
} |