Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extWFS / src / com / iver / cit / gvsig / fmap / drivers / wfs / WFSDriver.java @ 5026

History | View | Annotate | Download (14.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.util.ArrayList;
48
import java.util.Hashtable;
49

    
50
import org.cresques.cts.IProjection;
51
import org.cresques.cts.ProjectionPool;
52
import org.geotools.data.DefaultQuery;
53
import org.geotools.data.FeatureReader;
54
import org.geotools.data.Transaction;
55
import org.geotools.feature.AttributeType;
56
import org.geotools.filter.FidFilter;
57
import org.geotools.filter.Filter;
58
import org.geotools.filter.FilterFactory;
59

    
60
import com.hardcode.gdbms.engine.data.DataSourceFactory;
61
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
62
import com.hardcode.gdbms.engine.data.edition.DataWare;
63
import com.hardcode.gdbms.engine.values.Value;
64
import com.iver.cit.gvsig.fmap.DriverException;
65
import com.iver.cit.gvsig.fmap.core.FShape;
66
import com.iver.cit.gvsig.fmap.core.IFeature;
67
import com.iver.cit.gvsig.fmap.core.IGeometry;
68
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
69
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
70
import com.iver.utiles.IPersistance;
71
import com.iver.utiles.XMLEntity;
72

    
73

    
74
/**
75
 * Driver de WFS, utilizando DataStore de geotools.
76
 *
77
 * @author Vicente Caballero Navarro
78
 */
79
public class WFSDriver implements IWFSdriver, ObjectDriver,IPersistance{
80
        private Rectangle2D fullExtent = null;
81
        //private Rectangle2D extent = null;
82
        private WFSDataStore2 ds;
83

    
84
        //private FeatureReader fr;
85
        private String urlS = "http://localhost:8080/geoserver/wfs";
86
        private String versionS = "version=1.0.0";
87
        private String requestS = "request=getcapabilities";
88
        private String serviceS = "service=wfs";
89
        private String userS = "admin";
90
        private String pwdS = "geoserver";
91
        private String[] layersS;
92
        private boolean protocolS = true;
93
        private int numFeaturesS = 100;
94
        private int timeoutS = 10000;
95
        private Hashtable hashRelate;
96

    
97
        ///private Hashtable indexFID;
98
        protected int idFID_FieldName;
99
        private int numReg = -1;
100
        private int numField = -1;
101
        private AttributeType[] attributes;
102

    
103
    private String strOriginalSRID;
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
                DefaultQuery query = new DefaultQuery(layersS[0]);
204

    
205
        FeatureReader fr = null;
206

    
207
                fr = ds.getFeatureReader(query, Transaction.AUTO_COMMIT);
208

    
209

    
210
                if (numField == -1) {
211
                        numField = fr.getFeatureType().getAttributeCount();
212
                        attributes = fr.getFeatureType().getAttributeTypes();
213
                }
214
        int SRID = fr.getFeatureType().getDefaultGeometry().getGeometryFactory().getSRID();
215
        String auxStrEPSG = "EPSG:" + SRID;
216
        IProjection prj = ProjectionPool.get(auxStrEPSG);
217

    
218
                geomIterator = new WFSFeatureIterator(fr);
219
                return geomIterator;
220
        }
221

    
222
        /**
223
         * DOCUMENT ME!
224
         */
225
        public void close() {
226
        }
227

    
228
        /**
229
         * DOCUMENT ME!
230
         *
231
         * @throws DriverException DOCUMENT ME!
232
         */
233
        public void open() throws DriverException {
234
        }
235

    
236
        /**
237
         * DOCUMENT ME!
238
         *
239
         * @param FID DOCUMENT ME!
240
         *
241
         * @return DOCUMENT ME!
242
         */
243
        public int getRowIndexByFID(IFeature FID) {
244
                int resul;
245
                Integer rowIndex = (Integer) hashRelate.get(FID.getID());
246
                resul = rowIndex.intValue();
247

    
248
                return resul;
249
        }
250

    
251
        /**
252
         * DOCUMENT ME!
253
         *
254
         * @return DOCUMENT ME!
255
         *
256
         * @throws IOException DOCUMENT ME!
257
         */
258
        public int getShapeCount() throws IOException {
259
                return numReg;
260
        }
261

    
262
        /**
263
         * DOCUMENT ME!
264
         *
265
         * @return DOCUMENT ME!
266
         */
267
        public DriverAttributes getDriverAttributes() {
268
                return null;
269
        }
270

    
271
        /**
272
         * DOCUMENT ME!
273
         *
274
         * @return DOCUMENT ME!
275
         */
276
        public String getName() {
277
                return "WFS Driver";
278
        }
279

    
280
        /**
281
         * Recorre el recordset creando una tabla Hash que usaremos para
282
         * relacionar el n?mero de un registro con su identificador ?nico. Debe
283
         * ser llamado en el setData justo despu?s de crear el recorset principal
284
         * @throws IOException
285
         */
286
        public void doRelateID_FID() throws IOException {
287
                if (ds == null) {
288
                        URL url = null;
289

    
290
                        try {
291
                                url = new URL(urlS + "?" + versionS + "&" + requestS + "&" +
292
                                                serviceS);
293
                        } catch (MalformedURLException e) {
294
                                e.printStackTrace();
295
                        }
296

    
297
                        //int timeout=10000;
298
                        //boolean protocol=true;
299
                        /* Map m = new HashMap();
300
                        m.put(WFSDataStoreFactory.URL.key, url);
301
                        m.put(WFSDataStoreFactory.TIMEOUT.key, new Integer(timeoutS));
302
                        m.put(WFSDataStoreFactory.PROTOCOL.key, new Boolean(protocolS));
303
                        m.put(WFSDataStoreFactory.PASSWORD.key, pwdS);
304
                        m.put(WFSDataStoreFactory.USERNAME.key, userS);
305
                        m.put(WFSDataStoreFactory.BUFFER_SIZE.key, new Integer(numFeaturesS)); */
306
            Boolean protocol = new Boolean(protocolS);
307

    
308
                        try {
309
                                // ds = (WFSDataStore) (new WFSDataStoreFactory()).createNewDataStore(m);
310

    
311
                ds = new WFSDataStore2(url, protocol, userS, pwdS, timeoutS, numFeaturesS);
312
                strOriginalSRID = ds.getSRID(layersS[0]);
313

    
314

    
315
                        } catch (Exception e) {
316
                                e.printStackTrace();
317
                        }
318

    
319
                        /*
320
                           String[] names=null;
321
                           try {
322
                                   names = ds.getTypeNames();
323
                           } catch (IOException e1) {
324
                                   e1.printStackTrace();
325
                           }
326
                           System.out.println("Capas :");
327

328
                           for (int i=0;i<names.length;i++){
329
                                   System.out.println("Capa "+i+" : "+names[i]);
330
                                   //Se selecciona la capa que se quiere abrir
331
                                   //if (true)//si esta seleccionada en la lista o arbol que se muestre en el wizard
332
                                   //typeNames.add(names[i]);
333
                           }
334
                         */
335
                }
336

    
337
                hashRelate = new Hashtable();
338

    
339
                ///indexFID = new Hashtable();
340
                //Query query = new DefaultQuery(layersS[0]);
341

    
342
                //FeatureReader fr=null;
343
                //fr = ds.getFeatureReader(query,Transaction.AUTO_COMMIT);
344
                int index = 0;
345
                IFeatureIterator iter = null;
346

    
347
                try {
348
                        iter = getFeatureIterator(null, null);
349
                } catch (DriverException e) {
350
                        e.printStackTrace();
351
                }
352

    
353
                try {
354
                        while (iter.hasNext()) {
355
                                IFeature feature = iter.next();
356
                                features.add(feature);
357

    
358
                                Rectangle2D rect = feature.getGeometry().getBounds2D();
359

    
360
                                if (fullExtent == null) {
361
                                        fullExtent = new Rectangle2D.Double(rect.getMinX(),
362
                                                        rect.getMinY(), rect.getWidth(), rect.getHeight());
363
                                } else {
364
                                        fullExtent.add(new Rectangle2D.Double(rect.getMinX(),
365
                                                        rect.getMinY(), rect.getWidth(), rect.getHeight()));
366
                                }
367

    
368
                                hashRelate.put(feature.getID(), new Integer(index));
369

    
370
                                ///indexFID.put(new Integer(index),feature.getID());
371
                                index++;
372
                        }
373
                } catch (DriverException e) {
374
                        e.printStackTrace();
375
                }
376

    
377
                numReg = index;
378
                //extent = (Rectangle2D)fullExtent.clone();
379
        }
380

    
381
        /**
382
         * DOCUMENT ME!
383
         *
384
         * @param url DOCUMENT ME!
385
         * @param user DOCUMENT ME!
386
         * @param pwd DOCUMENT ME!
387
         * @param layers DOCUMENT ME!
388
         * @param protocol DOCUMENT ME!
389
         * @param numFeatures DOCUMENT ME!
390
         * @param timeout DOCUMENT ME!
391
         * @param id_FID_field DOCUMENT ME!
392
         * @throws IOException
393
         */
394
        public void setData(String url, String user, String pwd, String[] layers,
395
                boolean protocol, int numFeatures, int timeout, int id_FID_field) throws IOException {
396
                idFID_FieldName = id_FID_field;
397
                urlS = url;
398
                userS = user;
399
                pwdS = pwd;
400
                layersS = layers;
401
                protocolS = protocol;
402
                numFeaturesS = numFeatures;
403
                timeoutS = timeout;
404
                doRelateID_FID();
405
        }
406

    
407
        /**
408
         * DOCUMENT ME!
409
         *
410
         * @param arg0 DOCUMENT ME!
411
         * @param arg1 DOCUMENT ME!
412
         *
413
         * @return DOCUMENT ME!
414
         *
415
         * @throws com.hardcode.gdbms.engine.data.driver.DriverException DOCUMENT ME!
416
         */
417
        public Value getFieldValue(long arg0, int arg1)
418
                throws com.hardcode.gdbms.engine.data.driver.DriverException {
419
                /*try {
420
                   ds.getTypeNames();
421
                   } catch (IOException e1) {
422
                           e1.printStackTrace();
423
                   }*/
424
                /*System.out.println("Filter");
425
                   Filter fidFilter=createFilter(new String[]{(String)indexFID.get(new Integer((int)arg0))});
426
                   System.out.println("Query");
427
                   Query query = new DefaultQuery((String)typeNames.get(0),fidFilter);
428
                   FeatureReader fr=null;
429
                   try {
430
                           System.out.println("getFeatureReader");
431
                           fr = ds.getFeatureReader(query,Transaction.AUTO_COMMIT);
432
                   } catch (IOException e) {
433
                           e.printStackTrace();
434
                   }
435
                           try {
436
                                   return getValue(fr,arg1);
437
                           } catch (NoSuchElementException e) {
438
                                   e.printStackTrace();
439
                           } catch (IOException e) {
440
                                   e.printStackTrace();
441
                           } catch (IllegalAttributeException e) {
442
                                   e.printStackTrace();
443
                           }
444
                 */
445
                return ((IFeature) features.get((int) arg0)).getAttribute(arg1);
446
        }
447

    
448
        /**
449
         * DOCUMENT ME!
450
         *
451
         * @return DOCUMENT ME!
452
         *
453
         * @throws com.hardcode.gdbms.engine.data.driver.DriverException DOCUMENT ME!
454
         */
455
        public int getFieldCount()
456
                throws com.hardcode.gdbms.engine.data.driver.DriverException {
457
                return numField;
458
        }
459

    
460
        /**
461
         * DOCUMENT ME!
462
         *
463
         * @param arg0 DOCUMENT ME!
464
         *
465
         * @return DOCUMENT ME!
466
         *
467
         * @throws com.hardcode.gdbms.engine.data.driver.DriverException DOCUMENT ME!
468
         */
469
        public String getFieldName(int arg0)
470
                throws com.hardcode.gdbms.engine.data.driver.DriverException {
471
                return attributes[arg0].getName();
472
        }
473

    
474
        /**
475
         * DOCUMENT ME!
476
         *
477
         * @return DOCUMENT ME!
478
         *
479
         * @throws com.hardcode.gdbms.engine.data.driver.DriverException DOCUMENT ME!
480
         */
481
        public long getRowCount()
482
                throws com.hardcode.gdbms.engine.data.driver.DriverException {
483
                return numReg;
484
        }
485

    
486
        /**
487
         * DOCUMENT ME!
488
         *
489
         * @param arg0 DOCUMENT ME!
490
         *
491
         * @return DOCUMENT ME!
492
         *
493
         * @throws com.hardcode.gdbms.engine.data.driver.DriverException DOCUMENT ME!
494
         */
495
        public int getFieldType(int arg0)
496
                throws com.hardcode.gdbms.engine.data.driver.DriverException {
497
                attributes[arg0].getType();
498

    
499
                return FIELD_TYPE_STRING; //fr.getFeatureType().getAttributeType(arg0).getType();
500
        }
501

    
502
    /**
503
     * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
504
     */
505
    public int[] getPrimaryKeys() throws com.hardcode.gdbms.engine.data.driver.DriverException {
506
        throw new UnsupportedOperationException();
507
    }
508

    
509
    /**
510
     * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
511
     */
512
    public void write(DataWare arg0) throws com.hardcode.gdbms.engine.data.driver.DriverException {
513
        // TODO Auto-generated method stub
514

    
515
    }
516

    
517
        public void setDataSourceFactory(DataSourceFactory arg0) {
518
                // TODO Auto-generated method stub
519

    
520
        }
521

    
522
        public String getClassName() {
523
                // TODO Auto-generated method stub
524
                return null;
525
        }
526

    
527
        public XMLEntity getXMLEntity() {
528
                XMLEntity xml=new XMLEntity();
529
                xml.putProperty("idFID_FieldName",idFID_FieldName);
530
                xml.putProperty("url",urlS);
531
                xml.putProperty("user",userS);
532
                xml.putProperty("pwd",pwdS);
533
                xml.putProperty("layers",layersS);
534
                xml.putProperty("protocol",protocolS);
535
                xml.putProperty("numFeatures",numFeaturesS);
536
                xml.putProperty("timeout",timeoutS);
537
                return xml;
538
        }
539

    
540
        public static WFSDriver createWFSDriverFromXML(XMLEntity xml) {
541
                WFSDriver wfsDriver=new WFSDriver();
542
                wfsDriver.idFID_FieldName = xml.getIntProperty("idFID_FieldName");
543
                wfsDriver.urlS=xml.getStringProperty("url");
544
                wfsDriver.userS=xml.getStringProperty("user");
545
                wfsDriver.pwdS=xml.getStringProperty("pwd");
546
                wfsDriver.layersS=xml.getStringArrayProperty("layers");
547
                wfsDriver.protocolS=xml.getBooleanProperty("protocol");
548
                wfsDriver.numFeaturesS=xml.getIntProperty("numFeatures");
549
                wfsDriver.timeoutS=xml.getIntProperty("timeout");
550

    
551
                return wfsDriver;
552
        }
553

    
554
        public void setXMLEntity(XMLEntity arg0) {
555
                // TODO Auto-generated method stub
556

    
557
        }
558

    
559
    /**
560
     * @return return SRID string.
561
     */
562
    public String getProjection()
563
    {
564
        return strOriginalSRID;
565
    }
566

    
567
        public void reLoad() throws IOException {
568
                setData(urlS, userS, pwdS, layersS, protocolS, numFeaturesS, timeoutS, idFID_FieldName);
569

    
570
        }
571

    
572
        public int getFieldWidth(int i) throws com.hardcode.gdbms.engine.data.driver.DriverException {
573
                // TODO Auto-generated method stub
574
                return 0;
575
        }
576
}