Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extWFS2 / src / com / iver / cit / gvsig / fmap / drivers / wfs / FMapWFSDriver.java @ 6324

History | View | Annotate | Download (16.5 KB)

1
package com.iver.cit.gvsig.fmap.drivers.wfs;
2

    
3
import java.awt.geom.Rectangle2D;
4
import java.io.File;
5
import java.io.IOException;
6
import java.net.ConnectException;
7
import java.net.URL;
8
import java.util.ArrayList;
9
import java.util.Hashtable;
10
import java.util.Iterator;
11

    
12
import org.apache.log4j.Logger;
13
import org.geotools.feature.AttributeType;
14
import org.gvsig.remoteClient.wfs.WFSClient;
15
import org.gvsig.remoteClient.wfs.WFSFeature;
16
import org.gvsig.remoteClient.wfs.WFSStatus;
17
import org.gvsig.remoteClient.wfs.WFSProtocolHandler.ServiceInformation;
18
import org.gvsig.remoteClient.wms.ICancellable;
19

    
20
import com.hardcode.gdbms.engine.data.DataSourceFactory;
21
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
22
import com.hardcode.gdbms.engine.data.edition.DataWare;
23
import com.hardcode.gdbms.engine.values.Value;
24
import com.iver.cit.gvsig.fmap.DriverException;
25
import com.iver.cit.gvsig.fmap.core.FShape;
26
import com.iver.cit.gvsig.fmap.core.IFeature;
27
import com.iver.cit.gvsig.fmap.core.IGeometry;
28
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
29
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
30
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
31
import com.iver.cit.gvsig.fmap.drivers.UnsupportedVersionException;
32
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
33
import com.iver.cit.gvsig.fmap.drivers.WFSDriver;
34
import com.iver.cit.gvsig.fmap.drivers.WFSException;
35
import com.iver.cit.gvsig.fmap.drivers.geotools.WFSFeaturesIterator;
36
import com.iver.cit.gvsig.fmap.drivers.geotools.WFSFeatureLayer;
37
import com.iver.cit.gvsig.fmap.drivers.geotools.WFSGetFeatureParser;
38
import com.iver.cit.gvsig.fmap.layers.WFSLayerNode;
39

    
40
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
41
 *
42
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
43
 *
44
 * This program is free software; you can redistribute it and/or
45
 * modify it under the terms of the GNU General Public License
46
 * as published by the Free Software Foundation; either version 2
47
 * of the License, or (at your option) any later version.
48
 *
49
 * This program is distributed in the hope that it will be useful,
50
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
51
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
52
 * GNU General Public License for more details.
53
 *
54
 * You should have received a copy of the GNU General Public License
55
 * along with this program; if not, write to the Free Software
56
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
57
 *
58
 * For more information, contact:
59
 *
60
 *  Generalitat Valenciana
61
 *   Conselleria d'Infraestructures i Transport
62
 *   Av. Blasco Ib??ez, 50
63
 *   46010 VALENCIA
64
 *   SPAIN
65
 *
66
 *      +34 963862235
67
 *   gvsig@gva.es
68
 *      www.gvsig.gva.es
69
 *
70
 *    or
71
 *
72
 *   IVER T.I. S.A
73
 *   Salamanca 50
74
 *   46005 Valencia
75
 *   Spain
76
 *
77
 *   +34 963163400
78
 *   dac@iver.es
79
 */
80
/* CVS MESSAGES:
81
 *
82
 * $Id: FMapWFSDriver.java 6324 2006-07-12 11:55:22Z jmvivo $
83
 * $Log$
84
 * Revision 1.16  2006-07-12 11:55:22  jmvivo
85
 * *** empty log message ***
86
 *
87
 * Revision 1.15  2006/07/12 10:58:10  jmvivo
88
 * A?adido el metodo 'reload' del ObjectDriver (falta implementar)
89
 *
90
 * Revision 1.14  2006/07/11 11:55:41  jorpiell
91
 * Se ha a?adido el fallo de tipo de dato en el log
92
 *
93
 * Revision 1.13  2006/06/15 10:34:12  jorpiell
94
 * Ya se pueden ver los atributos de las tablas
95
 *
96
 * Revision 1.12  2006/06/15 07:50:58  jorpiell
97
 * A?adida la funcionalidad de reproyectar y hechos algunos cambios en la interfaz
98
 *
99
 * Revision 1.11  2006/06/14 08:46:24  jorpiell
100
 * Se tiene en cuanta la opcion para refrescar las capabilities
101
 *
102
 * Revision 1.10  2006/06/14 07:57:19  jorpiell
103
 * Ya no se usa la estrategia ni se usa geotools para hacer el getFeature. Ahora se usa ?nicamente para el parseo de GML
104
 *
105
 * Revision 1.9  2006/06/05 16:49:42  caballero
106
 * poder editar y exportar
107
 *
108
 * Revision 1.8  2006/05/30 05:26:38  jorpiell
109
 * isWritable ha sido a?adido a la interfaz, y hay que implementarlo
110
 *
111
 * Revision 1.7  2006/05/25 10:32:11  jorpiell
112
 * Se ha renombrado la clase WFSFields por WFSAttributes porque era algo confusa
113
 *
114
 * Revision 1.6  2006/05/23 13:21:28  jorpiell
115
 * Se tiene en cuanta el online resource
116
 *
117
 * Revision 1.5  2006/05/22 10:31:35  jorpiell
118
 * Cambio producido al cambiar una interfaz del geotools driver
119
 *
120
 * Revision 1.4  2006/05/22 10:11:10  jorpiell
121
 * Eliminadas algunas lineas innecesarias
122
 *
123
 * Revision 1.3  2006/05/19 12:53:54  jorpiell
124
 * Se le ha a?adido un driver de geottols, por lo que tiene dos drivers. Para hacer el getFeatureInfo y el capablities utilizar? elremoteServices y para hacer el getFeature usar? el de geotools.
125
 *
126
 * Revision 1.2  2006/04/20 16:38:24  jorpiell
127
 * Ahora mismo ya se puede hacer un getCapabilities y un getDescribeType de la capa seleccionada para ver los atributos a dibujar. Queda implementar el panel de opciones y hacer el getFeature().
128
 *
129
 * Revision 1.1  2006/04/19 12:50:16  jorpiell
130
 * Primer commit de la aplicaci?n. Se puede hacer un getCapabilities y ver el mensaje de vienvenida del servidor
131
 *
132
 *
133
 */
134
/**
135
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
136
 */
137
public class FMapWFSDriver implements WFSDriver, BoundedShapes,ObjectDriver{
138
        private WFSClient remoteServicesClient;
139
        private WFSLayerNode[] featuresList;
140
    private Hashtable hashFeatures;
141
    private WFSFeatureLayer featureIterator= null;
142
    private DriverAttributes attr = new DriverAttributes();
143

    
144
        public FMapWFSDriver() {
145
                super();
146
        }
147

    
148
        /*
149
         *  (non-Javadoc)
150
         * @see com.iver.cit.gvsig.fmap.drivers.WFSDriver#getCapabilities(java.net.URL)
151
         */
152
        public void getCapabilities(URL server)
153
                throws WFSException {
154
                        try {
155
                                getClient(server).connect(null);
156
                        } catch (Exception e) {
157
                                throw new WFSException(e);
158
                        }
159
        }
160

    
161
         /*
162
     *  (non-Javadoc)
163
     * @see com.iver.cit.gvsig.fmap.drivers.WFSDriver#describeFeatureType(java.lang.String)
164
     */
165
        public void describeFeatureType(String featureType) throws WFSException {
166
                remoteServicesClient.describeFeatureType(new WFSStatus(featureType));
167
        }
168

    
169
        /*
170
         *  (non-Javadoc)
171
         * @see com.iver.cit.gvsig.fmap.drivers.WFSDriver#getFeature(org.gvsig.remoteClient.wfs.WFSStatus)
172
         */
173
        public void getFeature(WFSStatus wfsStatus) throws WFSException {
174
                File file = remoteServicesClient.getFeature(wfsStatus);
175
                try {
176
                        featureIterator = new WFSFeatureLayer(file,
177
                                        featuresList,
178
                                        wfsStatus);
179
                } catch (Exception e) {
180
//                         TODO Auto-generated catch block
181
                        Logger.getLogger(getClass().getName()).error(e.getMessage());
182
                        throw new WFSException(e);
183
                }
184
        }
185

    
186
        /*
187
         *  (non-Javadoc)
188
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeType()
189
         */
190
        public int getShapeType() {
191
                return getShapeType(0);
192
        }
193

    
194
        /*
195
         *  (non-Javadoc)
196
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeCount()
197
         */
198
        public int getShapeCount() throws IOException {
199
                return getNumReg();
200
        }
201

    
202
        /*
203
         *  (non-Javadoc)
204
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
205
         */
206
        public DriverAttributes getDriverAttributes() {
207
                 return attr;
208
        }
209

    
210
        /*
211
         *  (non-Javadoc)
212
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getFullExtent()
213
         */
214
        public Rectangle2D getFullExtent() throws IOException {
215
                return featureIterator.getFullExtent();
216
        }
217

    
218
        /*
219
         *  (non-Javadoc)
220
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShape(int)
221
         */
222
        public IGeometry getShape(int index) throws IOException {
223
                return ((IFeature) getFeatures().get(index)).getGeometry();
224
        }
225

    
226
        /*
227
         *  (non-Javadoc)
228
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#reLoad()
229
         */
230
        public void reLoad() throws IOException {
231
                featureIterator.reload();
232
        }
233

    
234
        /*
235
         *  (non-Javadoc)
236
         * @see com.hardcode.driverManager.Driver#getName()
237
         */
238
        public String getName() {
239
                return "WFS Driver";
240
        }
241

    
242

    
243

    
244
        /**
245
         * Devuelve WFSClient a partir de su URL.
246
         *
247
         * @param url URL.
248
         *
249
         * @return WMSClient.
250
         * @throws IOException
251
         * @throws ConnectException
252
         *
253
         * @throws UnsupportedVersionException
254
         * @throws IOException
255
         */
256
        public WFSClient getClient(URL url) throws ConnectException, IOException {
257
                if (remoteServicesClient == null) {
258
                        remoteServicesClient = new WFSClient(url.toString());
259
                }
260
                return remoteServicesClient;
261
        }
262

    
263
        /**
264
     * Creates a new instance of a WFSClient.
265
     * @param
266
     * host
267
         * @throws IOException
268
         * @throws ConnectException
269
     */
270
    public void createClient(URL host) throws ConnectException, IOException {
271
        remoteServicesClient = new WFSClient(host.toString());
272
    }
273

    
274
    /**
275
         * Establishes the connection to the WfS server. Connecting to a WFS is
276
         * an abstraction.<br>
277
         * <p>
278
         * Actually, it sends a GetCapabilities request to read the necessary
279
         * data for building further DescribeFeatureType requests.
280
         * </p>
281
         * @throws IOException, DriverException.
282
         */
283
        public boolean connect(boolean override,ICancellable cancel) throws IOException, DriverException {
284
                return remoteServicesClient.connect(override,cancel);
285
        }
286
        
287
        public boolean connect(ICancellable cancel) {
288
           return remoteServicesClient.connect(false, cancel);
289
        }
290

    
291
        /**
292
         * Returns an array of WFSLayerNode's with the descriptors of
293
         * all features (retrieved using the getCapabilities operation)
294
         * @return WFSLayerNode[]
295
         */
296
        public WFSLayerNode[] getLayerList(){
297
                if (hashFeatures == null) {
298
                        hashFeatures = new Hashtable();
299
                        Hashtable wfsFeatures  = remoteServicesClient.getFeatures();
300

    
301
                        Iterator keys = wfsFeatures.keySet().iterator();
302
                        featuresList = new WFSLayerNode[wfsFeatures.size()];
303

    
304
                        for (int i=0 ; i<wfsFeatures.size() ; i++){
305
                                WFSLayerNode lyr = new WFSLayerNode();
306
                                WFSFeature feature = (WFSFeature)wfsFeatures.get(keys.next());
307

    
308
                                lyr.setName(feature.getName());
309
                                lyr.setTitle(feature.getTitle());
310
                                lyr.setAbstract(feature.getAbstract());
311
                                lyr.setFields(feature.getFields());
312
                                lyr.setSrs(feature.getSrs());
313
                                lyr.setLatLonBbox(feature.getLatLonBbox());
314

    
315
                                featuresList[i] = lyr;
316
                                hashFeatures.put(lyr.getName(), lyr);
317
                        }
318
                }
319
                return featuresList;
320
        }
321

    
322
        /**
323
         * Returns all the feature information retrieved using a
324
         * describeFeatureTypeOpearion
325
         * @param layerName
326
         * Feature name
327
         * @return
328
         */
329
        public WFSLayerNode getLayerInfo(String layerName){
330
                WFSLayerNode lyr = (WFSLayerNode)hashFeatures.get(layerName);
331
                if (lyr.getFields().size() == 0){
332
                        try {
333
                                describeFeatureType(layerName);
334
                                WFSFeature feature = (WFSFeature) remoteServicesClient.getFeatures().get(layerName);
335
                                lyr.setFields(feature.getFields());
336
                        } catch (WFSException e) {
337
                                // The feature doesn't has fields
338
                                e.printStackTrace();
339
                        }
340
                }
341
                return lyr;
342
        }
343

    
344

    
345
        /**
346
     * @return The title of the service offered by the WMS server.
347
     */
348
    public String getServiceTitle() {
349
        return remoteServicesClient.getServiceInformation().title;
350
    }
351

    
352
    /**
353
     * @return The abstract of the service offered by the WMS server.
354
     */
355
    public String getServiceAbstract() {
356
        return remoteServicesClient.getServiceInformation().abstr;
357
    }
358

    
359
    /**
360
     * Returns a Hash table containing the values for each online resource.
361
     * Using as key a String with name of the WMS request and the value returned
362
     * by the hash is another string containing the corresponding Url
363
     * @return HashTable
364
     */
365
    public Hashtable getOnlineResources() {
366
            Hashtable onlineResources = new Hashtable();
367
            ServiceInformation si = remoteServicesClient.getServiceInformation();
368
            Iterator it = si.operations.keySet().iterator();
369
            while (it.hasNext()) {
370
                    String key = (String) it.next();
371
                    String val = (String) si.operations.get(key);
372
                    if (val==null && (si.online_resource!=null || si.online_resource!= ""))
373
                            val = si.online_resource;
374
                    if (val!=null) {
375
                            onlineResources.put(key, val);
376
                    }
377
            }
378
            return onlineResources;
379
    }
380

    
381
    public String getOnlineResource(){
382
            ServiceInformation si = remoteServicesClient.getServiceInformation();
383
            return si.getOnline_resource();
384
    }
385

    
386
    /**
387
     *
388
     * @return the host
389
     */
390
    public String getHost(){
391
        return remoteServicesClient.getHost();
392
    }
393

    
394
    /**
395
     * @return the version of this client.
396
     */
397
    public String getVersion() {
398
        return remoteServicesClient.getVersion();
399
    }
400

    
401
        /**
402
         * @return Returns the features.
403
         */
404
        public ArrayList getFeatures() {
405
                return featureIterator.getFeatures();
406
        }
407

    
408
        /**
409
         * @return Returns the hashRelate.
410
         */
411
        public Hashtable getHashRelate() {
412
                return featureIterator.getHashRelate();
413
        }
414

    
415

    
416
         /**
417
         * @return Returns the numReg.
418
         */
419
        public int getNumReg() {
420
                return featureIterator.getNumReg();
421
        }
422

    
423
         /**
424
         * @return Returns the numFiled.
425
         */
426
        public int getNumField() {
427
                return featureIterator.getNumField();
428
        }
429

    
430
        /**
431
         * @return Returns the attributes.
432
         */
433
        public AttributeType[] getAttributes() {
434
                return featureIterator.getAttributes();
435
        }
436

    
437
        /*
438
         *  (non-Javadoc)
439
         * @see com.iver.cit.gvsig.fmap.drivers.WFSDriver#close()
440
         */
441
        public void close() {
442
                // TODO Auto-generated method stub
443

    
444
        }
445

    
446
        /*
447
         *  (non-Javadoc)
448
         * @see com.iver.cit.gvsig.fmap.drivers.WFSDriver#open()
449
         */
450
        public void open() throws DriverException {
451
                attr.setLoadedInMemory(true);
452

    
453
        }
454

    
455
        /*
456
         *  (non-Javadoc)
457
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
458
         */
459
        public int[] getPrimaryKeys() throws com.hardcode.gdbms.engine.data.driver.DriverException {
460
                throw new UnsupportedOperationException();
461
        }
462

    
463
        /*
464
         *  (non-Javadoc)
465
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
466
         */
467
        public void write(DataWare dataWare) throws com.hardcode.gdbms.engine.data.driver.DriverException {
468
                // TODO Auto-generated method stub
469

    
470
        }
471

    
472
        /*
473
         *  (non-Javadoc)
474
         * @see com.hardcode.gdbms.engine.data.driver.GDBMSDriver#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
475
         */
476
        public void setDataSourceFactory(DataSourceFactory dsf) {
477
                // TODO Auto-generated method stub
478

    
479
        }
480

    
481
        /*
482
         *  (non-Javadoc)
483
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long, int)
484
         */
485
        public Value getFieldValue(long rowIndex, int fieldId) throws com.hardcode.gdbms.engine.data.driver.DriverException {
486
                return ((IFeature) getFeatures().get((int) rowIndex)).getAttribute(fieldId);
487
        }
488

    
489
        /*
490
         *  (non-Javadoc)
491
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldCount()
492
         */
493
        public int getFieldCount() throws com.hardcode.gdbms.engine.data.driver.DriverException {
494
                return getNumField();
495
        }
496

    
497
        /*
498
         *  (non-Javadoc)
499
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldName(int)
500
         */
501
        public String getFieldName(int fieldId) throws com.hardcode.gdbms.engine.data.driver.DriverException {
502
                return getAttributes()[fieldId].getName();
503
        }
504

    
505
        /*
506
         *  (non-Javadoc)
507
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
508
         */
509
        public long getRowCount() throws com.hardcode.gdbms.engine.data.driver.DriverException {
510
                return getNumReg();
511
        }
512

    
513
        /*
514
         *  (non-Javadoc)
515
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldType(int)
516
         */
517
        public int getFieldType(int i) throws com.hardcode.gdbms.engine.data.driver.DriverException {
518
                getAttributes()[i].getType();
519

    
520
                return FIELD_TYPE_STRING;
521
        }
522

    
523
        /*
524
         *  (non-Javadoc)
525
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldWidth(int)
526
         */
527
        public int getFieldWidth(int i) throws com.hardcode.gdbms.engine.data.driver.DriverException {
528
                // TODO Auto-generated method stub
529
                return 0;
530
        }
531

    
532
        /*
533
         *  (non-Javadoc)
534
         * @see com.iver.cit.gvsig.fmap.drivers.WFSDriver#getFeatureIterator(java.awt.geom.Rectangle2D, java.lang.String)
535
         */
536
        public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) throws DriverException, IOException {
537
                return featureIterator.getIterator();
538
        }
539

    
540
        /*
541
         *  (non-Javadoc)
542
         * @see com.iver.cit.gvsig.fmap.drivers.WFSDriver#getRowIndexByFID(com.iver.cit.gvsig.fmap.core.IFeature)
543
         */
544
        public int getRowIndexByFID(IFeature FID) {
545
                int resul;
546
                Integer rowIndex = (Integer) getHashRelate().get(FID.getID());
547
                resul = rowIndex.intValue();
548
                return resul;
549
        }
550

    
551
        /*
552
         *  (non-Javadoc)
553
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#isWritable()
554
         */
555
        public boolean isWritable() {
556
                // TODO Auto-generated method stub
557
                return false;
558
        }
559

    
560
        /*
561
         *  (non-Javadoc)
562
         * @see com.iver.cit.gvsig.fmap.drivers.BoundedShapes#getShapeBounds(int)
563
         */
564
        public Rectangle2D getShapeBounds(int index) throws IOException {
565
                return getShape(index).getBounds2D();
566
        }
567

    
568
        /*
569
         *  (non-Javadoc)
570
         * @see com.iver.cit.gvsig.fmap.drivers.BoundedShapes#getShapeType(int)
571
         */
572
        public int getShapeType(int index) {
573
                try {
574
                        if (getShapeCount() > 0){
575
                                return getShape(0).getGeometryType();
576
                        }
577

    
578
                } catch (IOException e) {
579
                        e.printStackTrace();
580
                }
581
                return FShape.MULTI;
582
        }
583

    
584
        public void reload() throws IOException, com.hardcode.gdbms.engine.data.driver.DriverException {
585
                // TODO Auto-generated method stub
586
                
587
        }
588

    
589

    
590
}