Statistics
| Revision:

root / trunk / extensions / extWFS2 / src / com / iver / cit / gvsig / fmap / drivers / wfs / FMapWFSDriver.java @ 13881

History | View | Annotate | Download (17 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.gvsig.remoteClient.wfs.WFSClient;
13
import org.gvsig.remoteClient.wfs.WFSFeature;
14
import org.gvsig.remoteClient.wfs.WFSStatus;
15
import org.gvsig.remoteClient.wfs.WFSProtocolHandler.ServiceInformation;
16
import org.gvsig.remoteClient.wms.ICancellable;
17

    
18
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
19
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
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.exceptions.layers.ConnectionErrorLayerException;
25
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
26
import com.iver.cit.gvsig.exceptions.layers.UnsupportedVersionLayerException;
27
import com.iver.cit.gvsig.fmap.core.FShape;
28
import com.iver.cit.gvsig.fmap.core.IFeature;
29
import com.iver.cit.gvsig.fmap.core.IGeometry;
30
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
31
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
32
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
33
import com.iver.cit.gvsig.fmap.drivers.WFSDriver;
34
import com.iver.cit.gvsig.fmap.drivers.WFSLayerException;
35
import com.iver.cit.gvsig.fmap.drivers.gml.GMLDriver;
36
import com.iver.cit.gvsig.fmap.drivers.gml.FMAPGeometryFactory.FeatureWithAttributes;
37
import com.iver.cit.gvsig.fmap.layers.WFSLayerNode;
38

    
39
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
40
 *
41
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
42
 *
43
 * This program is free software; you can redistribute it and/or
44
 * modify it under the terms of the GNU General Public License
45
 * as published by the Free Software Foundation; either version 2
46
 * of the License, or (at your option) any later version.
47
 *
48
 * This program is distributed in the hope that it will be useful,
49
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
50
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
51
 * GNU General Public License for more details.
52
 *
53
 * You should have received a copy of the GNU General Public License
54
 * along with this program; if not, write to the Free Software
55
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
56
 *
57
 * For more information, contact:
58
 *
59
 *  Generalitat Valenciana
60
 *   Conselleria d'Infraestructures i Transport
61
 *   Av. Blasco Ib??ez, 50
62
 *   46010 VALENCIA
63
 *   SPAIN
64
 *
65
 *      +34 963862235
66
 *   gvsig@gva.es
67
 *      www.gvsig.gva.es
68
 *
69
 *    or
70
 *
71
 *   IVER T.I. S.A
72
 *   Salamanca 50
73
 *   46005 Valencia
74
 *   Spain
75
 *
76
 *   +34 963163400
77
 *   dac@iver.es
78
 */
79
/* CVS MESSAGES:
80
 *
81
 * $Id: FMapWFSDriver.java 13881 2007-09-19 16:22:04Z jaume $
82
 * $Log$
83
 * Revision 1.26  2007-09-19 16:14:50  jaume
84
 * removed unnecessary imports
85
 *
86
 * Revision 1.25  2007/03/06 16:54:14  caballero
87
 * Exceptions
88
 *
89
 * Revision 1.24  2007/02/09 14:12:38  jorpiell
90
 * Soporte para WFS 1.1 y WFS-T
91
 *
92
 * Revision 1.23  2006/12/26 09:18:52  ppiqueras
93
 * Cambiado "atttibutes" en todas las aparaciones en atributos, m?todos, clases, paquetes o comentarios por "fields". (S?lo a aquellas que afectan a clases dentro del proyecto extWFS2).
94
 *
95
 * Revision 1.22  2006/11/16 13:31:10  jorpiell
96
 * Se ha eliminado la visivilidad del constructor
97
 *
98
 * Revision 1.21  2006/11/15 17:38:08  jorpiell
99
 * Ya se puede guardar una capa en WFS.
100
 *
101
 * Revision 1.20  2006/09/25 10:23:03  caballero
102
 * no multiType
103
 *
104
 * Revision 1.19  2006/09/18 12:07:31  jorpiell
105
 * Se ha sustituido geotools por el driver de remoteservices
106
 *
107
 * Revision 1.18  2006/07/24 08:27:35  jorpiell
108
 * Implementado el m?todo getFieldType que permite la exportaci?n a los diferentes tipos de ficheros (como shp,....)
109
 *
110
 * Revision 1.17  2006/07/21 10:02:43  caballero
111
 * soluci?n provisional a WFS
112
 *
113
 * Revision 1.16  2006/07/12 11:55:22  jmvivo
114
 * *** empty log message ***
115
 *
116
 * Revision 1.15  2006/07/12 10:58:10  jmvivo
117
 * A?adido el metodo 'reload' del ObjectDriver (falta implementar)
118
 *
119
 * Revision 1.14  2006/07/11 11:55:41  jorpiell
120
 * Se ha a?adido el fallo de tipo de dato en el log
121
 *
122
 * Revision 1.13  2006/06/15 10:34:12  jorpiell
123
 * Ya se pueden ver los atributos de las tablas
124
 *
125
 * Revision 1.12  2006/06/15 07:50:58  jorpiell
126
 * A?adida la funcionalidad de reproyectar y hechos algunos cambios en la interfaz
127
 *
128
 * Revision 1.11  2006/06/14 08:46:24  jorpiell
129
 * Se tiene en cuanta la opcion para refrescar las capabilities
130
 *
131
 * Revision 1.10  2006/06/14 07:57:19  jorpiell
132
 * Ya no se usa la estrategia ni se usa geotools para hacer el getFeature. Ahora se usa ?nicamente para el parseo de GML
133
 *
134
 * Revision 1.9  2006/06/05 16:49:42  caballero
135
 * poder editar y exportar
136
 *
137
 * Revision 1.8  2006/05/30 05:26:38  jorpiell
138
 * isWritable ha sido a?adido a la interfaz, y hay que implementarlo
139
 *
140
 * Revision 1.7  2006/05/25 10:32:11  jorpiell
141
 * Se ha renombrado la clase WFSFields por WFSAttributes porque era algo confusa
142
 *
143
 * Revision 1.6  2006/05/23 13:21:28  jorpiell
144
 * Se tiene en cuanta el online resource
145
 *
146
 * Revision 1.5  2006/05/22 10:31:35  jorpiell
147
 * Cambio producido al cambiar una interfaz del geotools driver
148
 *
149
 * Revision 1.4  2006/05/22 10:11:10  jorpiell
150
 * Eliminadas algunas lineas innecesarias
151
 *
152
 * Revision 1.3  2006/05/19 12:53:54  jorpiell
153
 * 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.
154
 *
155
 * Revision 1.2  2006/04/20 16:38:24  jorpiell
156
 * 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().
157
 *
158
 * Revision 1.1  2006/04/19 12:50:16  jorpiell
159
 * Primer commit de la aplicaci?n. Se puede hacer un getCapabilities y ver el mensaje de vienvenida del servidor
160
 *
161
 *
162
 */
163
/**
164
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
165
 */
166
public class FMapWFSDriver implements WFSDriver, BoundedShapes,ObjectDriver{
167
        private WFSClient remoteServicesClient;
168
        private WFSLayerNode[] featuresList;
169
    private Hashtable hashFeatures;
170
    private DriverAttributes fields = new DriverAttributes();
171
    private GMLDriver driver = null;
172
    private boolean hasGeometry = false;
173

    
174
        FMapWFSDriver() {
175
                super();
176
        }
177

    
178
        /*
179
         *  (non-Javadoc)
180
         * @see com.iver.cit.gvsig.fmap.drivers.WFSDriver#getCapabilities(java.net.URL)
181
         */
182
        public void getCapabilities(URL server)
183
                throws LoadLayerException {
184
                                try {
185
                                        getClient(server).connect(null);
186
                                } catch (ConnectException e) {
187
                                        throw new ConnectionErrorLayerException(getName(),e);
188
                                } catch (IOException e) {
189
                                        throw new ConnectionErrorLayerException(getName(),e);
190
                                }
191
        }
192

    
193
         /*
194
     *  (non-Javadoc)
195
     * @see com.iver.cit.gvsig.fmap.drivers.WFSDriver#describeFeatureType(java.lang.String)
196
     */
197
        public void describeFeatureType(String featureType,String nameSpace) {
198
                remoteServicesClient.describeFeatureType(new WFSStatus(featureType,nameSpace));
199
        }
200

    
201
        /*
202
         *  (non-Javadoc)
203
         * @see com.iver.cit.gvsig.fmap.drivers.WFSDriver#getFeature(org.gvsig.remoteClient.wfs.WFSStatus)
204
         */
205
        public void getFeature(WFSStatus wfsStatus) throws WFSLayerException {
206
                File file = remoteServicesClient.getFeature(wfsStatus);
207
                driver = new GMLDriver();
208
                driver.open(file);
209
                try {
210
                        driver.initialize();
211
                } catch (InitializeDriverException e) {
212
                        throw new WFSLayerException(getName(),e);
213
                }
214
                hasGeometry = WFSUtils.getHasGeometry(featuresList,wfsStatus);
215
        }
216

    
217
        /*
218
         *  (non-Javadoc)
219
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeType()
220
         */
221
        public int getShapeType() {
222
                return getShapeType(0);
223
        }
224

    
225
        /*
226
         *  (non-Javadoc)
227
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeCount()
228
         */
229
        public int getShapeCount(){
230
                return getNumReg();
231
        }
232

    
233
        /*
234
         *  (non-Javadoc)
235
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
236
         */
237
        public DriverAttributes getDriverAttributes() {
238
                 return fields;
239
        }
240

    
241
        /*
242
         *  (non-Javadoc)
243
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getFullExtent()
244
         */
245
        public Rectangle2D getFullExtent() throws ReadDriverException{
246
                return driver.getFullExtent();
247
        }
248

    
249
        /*
250
         *  (non-Javadoc)
251
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShape(int)
252
         */
253
        public IGeometry getShape(int index) {
254
                return ((FeatureWithAttributes) getFeatures().get(index)).getFeature().getGeometry();
255
        }
256

    
257
        /*
258
         *  (non-Javadoc)
259
         * @see com.hardcode.driverManager.Driver#getName()
260
         */
261
        public String getName() {
262
                return "WFS Driver";
263
        }
264

    
265

    
266

    
267
        /**
268
         * Devuelve WFSClient a partir de su URL.
269
         *
270
         * @param url URL.
271
         *
272
         * @return WMSClient.
273
         * @throws IOException
274
         * @throws ConnectException
275
         *
276
         * @throws UnsupportedVersionLayerException
277
         * @throws IOException
278
         */
279
        public WFSClient getClient(URL url) throws ConnectException, IOException {
280
                if (remoteServicesClient == null) {
281
                        remoteServicesClient = new WFSClient(url.toString());
282
                }
283
                return remoteServicesClient;
284
        }
285

    
286
        /**
287
     * Creates a new instance of a WFSClient.
288
     * @param
289
     * host
290
         * @throws IOException
291
         * @throws ConnectException
292
     */
293
    public void createClient(URL host) throws ConnectException, IOException {
294
        remoteServicesClient = new WFSClient(host.toString());
295
    }
296

    
297
    /**
298
         * Establishes the connection to the WfS server. Connecting to a WFS is
299
         * an abstraction.<br>
300
         * <p>
301
         * Actually, it sends a GetCapabilities request to read the necessary
302
         * data for building further DescribeFeatureType requests.
303
         * </p>
304
         * @throws IOException, DriverException.
305
         */
306
        public boolean connect(boolean override,ICancellable cancel) {
307
                return remoteServicesClient.connect(override,cancel);
308
        }
309

    
310
        public boolean connect(ICancellable cancel) {
311
           return remoteServicesClient.connect(false, cancel);
312
        }
313

    
314
        /**
315
         * Returns an array of WFSLayerNode's with the descriptors of
316
         * all features (retrieved using the getCapabilities operation)
317
         * @return WFSLayerNode[]
318
         */
319
        public WFSLayerNode[] getLayerList(){
320
                if (hashFeatures == null) {
321
                        hashFeatures = new Hashtable();
322
                        Hashtable wfsFeatures  = remoteServicesClient.getFeatures();
323

    
324
                        Iterator keys = wfsFeatures.keySet().iterator();
325
                        featuresList = new WFSLayerNode[wfsFeatures.size()];
326

    
327
                        for (int i=0 ; i<wfsFeatures.size() ; i++){
328
                                WFSLayerNode lyr = new WFSLayerNode();
329
                                WFSFeature feature = (WFSFeature)wfsFeatures.get(keys.next());
330

    
331
                                lyr.setName(feature.getName());
332
                                if (feature.getNamespace() != null){
333
                                        lyr.setNameSpace(feature.getNamespace().getLocation());
334
                                }else{
335
                                        lyr.setNameSpace(null);
336
                                }
337
                                lyr.setTitle(feature.getTitle());
338
                                lyr.setAbstract(feature.getAbstract());
339
                                lyr.setFields(feature.getFields());
340
                                lyr.setSrs(feature.getSrs());
341
                                lyr.setLatLonBbox(feature.getLatLonBbox());
342

    
343
                                featuresList[i] = lyr;
344
                                hashFeatures.put(lyr.getName(), lyr);
345
                        }
346
                }
347
                return featuresList;
348
        }
349

    
350
        /**
351
         * Returns all the feature information retrieved using a
352
         * describeFeatureTypeOpearion
353
         * @param layerName
354
         * Feature name
355
         * @return
356
         */
357
        public WFSLayerNode getLayerInfo(String layerName,String nameSpace){
358
                WFSLayerNode lyr = (WFSLayerNode)hashFeatures.get(layerName);
359
                describeFeatureType(layerName,nameSpace);
360
                WFSFeature feature = (WFSFeature) remoteServicesClient.getFeatures().get(layerName);
361
                lyr.setFields(feature.getFields());
362
                return lyr;
363
        }
364

    
365

    
366
        /**
367
     * @return The title of the service offered by the WMS server.
368
     */
369
    public String getServiceTitle() {
370
        return remoteServicesClient.getServiceInformation().title;
371
    }
372

    
373
    /**
374
     * @return The abstract of the service offered by the WMS server.
375
     */
376
    public String getServiceAbstract() {
377
        return remoteServicesClient.getServiceInformation().abstr;
378
    }
379

    
380
    /**
381
     * Returns a Hash table containing the values for each online resource.
382
     * Using as key a String with name of the WMS request and the value returned
383
     * by the hash is another string containing the corresponding Url
384
     * @return HashTable
385
     */
386
    public Hashtable getOnlineResources() {
387
            Hashtable onlineResources = new Hashtable();
388
            ServiceInformation si = remoteServicesClient.getServiceInformation();
389
            Iterator it = si.operations.keySet().iterator();
390
            while (it.hasNext()) {
391
                    String key = (String) it.next();
392
                    String val = (String) si.operations.get(key);
393
                    if (val==null && (si.online_resource!=null || si.online_resource!= ""))
394
                            val = si.online_resource;
395
                    if (val!=null) {
396
                            onlineResources.put(key, val);
397
                    }
398
            }
399
            return onlineResources;
400
    }
401

    
402
    public String getOnlineResource(){
403
            ServiceInformation si = remoteServicesClient.getServiceInformation();
404
            return si.getOnline_resource();
405
    }
406

    
407
    /**
408
     *
409
     * @return the host
410
     */
411
    public String getHost(){
412
        return remoteServicesClient.getHost();
413
    }
414

    
415
    /**
416
     * @return the version of this client.
417
     */
418
    public String getVersion() {
419
        return remoteServicesClient.getVersion();
420
    }
421

    
422
        /**
423
         * @return Returns the features.
424
         */
425
        public ArrayList getFeatures() {
426
                return driver.getFeatures();
427
        }
428

    
429
        /**
430
         * @return Returns the hashRelate.
431
         */
432
        public Hashtable getHashRelate() {
433
                return driver.getHashRelate();
434
        }
435

    
436

    
437
         /**
438
         * @return Returns the numReg.
439
         */
440
        public int getNumReg() {
441
                return driver.getHashRelate().size();
442
        }
443

    
444
         /**
445
         * @return Returns the numFiled.
446
         */
447
        public int getNumField() {
448
                return driver.getAttributeCount();
449
        }
450

    
451
        /*
452
         *  (non-Javadoc)
453
         * @see com.iver.cit.gvsig.fmap.drivers.WFSDriver#close()
454
         */
455
        public void close() {
456
                // TODO Auto-generated method stub
457

    
458
        }
459

    
460
        /*
461
         *  (non-Javadoc)
462
         * @see com.iver.cit.gvsig.fmap.drivers.WFSDriver#open()
463
         */
464
        public void open() {
465
                fields.setLoadedInMemory(true);
466

    
467
        }
468

    
469
        /*
470
         *  (non-Javadoc)
471
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
472
         */
473
        public int[] getPrimaryKeys() {
474
                throw new UnsupportedOperationException();
475
        }
476

    
477
        /*
478
         *  (non-Javadoc)
479
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
480
         */
481
        public void write(DataWare dataWare) {
482
                // TODO Auto-generated method stub
483

    
484
        }
485

    
486
        /*
487
         *  (non-Javadoc)
488
         * @see com.hardcode.gdbms.engine.data.driver.GDBMSDriver#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
489
         */
490
        public void setDataSourceFactory(DataSourceFactory dsf) {
491
                // TODO Auto-generated method stub
492

    
493
        }
494

    
495
        /*
496
         *  (non-Javadoc)
497
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long, int)
498
         */
499
        public Value getFieldValue(long rowIndex, int fieldId) {
500
                FeatureWithAttributes feature = (FeatureWithAttributes)getFeatures().get((int)rowIndex);
501
                return feature.getFeature().getAttribute(fieldId);
502
        }
503

    
504
        /*
505
         *  (non-Javadoc)
506
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldCount()
507
         */
508
        public int getFieldCount(){
509
                return getNumField();
510
        }
511

    
512
        /*
513
         *  (non-Javadoc)
514
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldName(int)
515
         */
516
        public String getFieldName(int fieldId) throws ReadDriverException{
517
                return driver.getFieldName(fieldId);
518
        }
519

    
520
        /*
521
         *  (non-Javadoc)
522
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
523
         */
524
        public long getRowCount(){
525
                return getNumReg();
526
        }
527

    
528
        /*
529
         *  (non-Javadoc)
530
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldType(int)
531
         */
532
        public int getFieldType(int i) {
533
                return ((Value)driver.getAttributeValue(i)).getSQLType();
534
        }
535

    
536
        /*
537
         *  (non-Javadoc)
538
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldWidth(int)
539
         */
540
        public int getFieldWidth(int i) {
541
                // TODO Auto-generated method stub
542
                return 100;
543
        }
544

    
545
        /*
546
         *  (non-Javadoc)
547
         * @see com.iver.cit.gvsig.fmap.drivers.WFSDriver#getFeatureIterator(java.awt.geom.Rectangle2D, java.lang.String)
548
         */
549
        public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) {
550
                return new WFSFeaturesIterator(driver.getIterator(),strEPSG);
551
        }
552

    
553
        /*
554
         *  (non-Javadoc)
555
         * @see com.iver.cit.gvsig.fmap.drivers.WFSDriver#getRowIndexByFID(com.iver.cit.gvsig.fmap.core.IFeature)
556
         */
557
        public int getRowIndexByFID(IFeature FID) {
558
                int resul;
559
                Integer rowIndex = (Integer) getHashRelate().get(FID.getID());
560
                resul = rowIndex.intValue();
561
                return resul;
562
        }
563

    
564
        /*
565
         *  (non-Javadoc)
566
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#isWritable()
567
         */
568
        public boolean isWritable() {
569
                // TODO Auto-generated method stub
570
                return false;
571
        }
572

    
573
        /*
574
         *  (non-Javadoc)
575
         * @see com.iver.cit.gvsig.fmap.drivers.BoundedShapes#getShapeBounds(int)
576
         */
577
        public Rectangle2D getShapeBounds(int index) throws ReadDriverException {
578
                return getShape(index).getBounds2D();
579
        }
580

    
581
        /*
582
         *  (non-Javadoc)
583
         * @see com.iver.cit.gvsig.fmap.drivers.BoundedShapes#getShapeType(int)
584
         */
585
        public int getShapeType(int index) {
586
                if (getShapeCount() > 0){
587
                        return getShape(0).getGeometryType();
588
                }
589

    
590
                return FShape.MULTI;
591
        }
592

    
593
        /*
594
         *  (non-Javadoc)
595
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#reload()
596
         */
597
        public void reload(){
598

    
599
        }
600

    
601

    
602
}