Statistics
| Revision:

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

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

    
19
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
20
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
21
import com.hardcode.gdbms.engine.data.DataSourceFactory;
22
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
23
import com.hardcode.gdbms.engine.data.edition.DataWare;
24
import com.hardcode.gdbms.engine.values.Value;
25
import com.iver.cit.gvsig.exceptions.layers.ConnectionErrorLayerException;
26
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
27
import com.iver.cit.gvsig.exceptions.layers.UnsupportedVersionLayerException;
28
import com.iver.cit.gvsig.fmap.core.FShape;
29
import com.iver.cit.gvsig.fmap.core.IFeature;
30
import com.iver.cit.gvsig.fmap.core.IGeometry;
31
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
32
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
33
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
34
import com.iver.cit.gvsig.fmap.drivers.WFSDriver;
35
import com.iver.cit.gvsig.fmap.drivers.WFSLayerException;
36
import com.iver.cit.gvsig.fmap.drivers.gml.GMLDriver;
37
import com.iver.cit.gvsig.fmap.drivers.gml.FMAPGeometryFactory.FeatureWithAttributes;
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 10626 2007-03-06 16:55:54Z caballero $
83
 * $Log$
84
 * Revision 1.25  2007-03-06 16:54:14  caballero
85
 * Exceptions
86
 *
87
 * Revision 1.24  2007/02/09 14:12:38  jorpiell
88
 * Soporte para WFS 1.1 y WFS-T
89
 *
90
 * Revision 1.23  2006/12/26 09:18:52  ppiqueras
91
 * 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).
92
 *
93
 * Revision 1.22  2006/11/16 13:31:10  jorpiell
94
 * Se ha eliminado la visivilidad del constructor
95
 *
96
 * Revision 1.21  2006/11/15 17:38:08  jorpiell
97
 * Ya se puede guardar una capa en WFS.
98
 *
99
 * Revision 1.20  2006/09/25 10:23:03  caballero
100
 * no multiType
101
 *
102
 * Revision 1.19  2006/09/18 12:07:31  jorpiell
103
 * Se ha sustituido geotools por el driver de remoteservices
104
 *
105
 * Revision 1.18  2006/07/24 08:27:35  jorpiell
106
 * Implementado el m?todo getFieldType que permite la exportaci?n a los diferentes tipos de ficheros (como shp,....)
107
 *
108
 * Revision 1.17  2006/07/21 10:02:43  caballero
109
 * soluci?n provisional a WFS
110
 *
111
 * Revision 1.16  2006/07/12 11:55:22  jmvivo
112
 * *** empty log message ***
113
 *
114
 * Revision 1.15  2006/07/12 10:58:10  jmvivo
115
 * A?adido el metodo 'reload' del ObjectDriver (falta implementar)
116
 *
117
 * Revision 1.14  2006/07/11 11:55:41  jorpiell
118
 * Se ha a?adido el fallo de tipo de dato en el log
119
 *
120
 * Revision 1.13  2006/06/15 10:34:12  jorpiell
121
 * Ya se pueden ver los atributos de las tablas
122
 *
123
 * Revision 1.12  2006/06/15 07:50:58  jorpiell
124
 * A?adida la funcionalidad de reproyectar y hechos algunos cambios en la interfaz
125
 *
126
 * Revision 1.11  2006/06/14 08:46:24  jorpiell
127
 * Se tiene en cuanta la opcion para refrescar las capabilities
128
 *
129
 * Revision 1.10  2006/06/14 07:57:19  jorpiell
130
 * Ya no se usa la estrategia ni se usa geotools para hacer el getFeature. Ahora se usa ?nicamente para el parseo de GML
131
 *
132
 * Revision 1.9  2006/06/05 16:49:42  caballero
133
 * poder editar y exportar
134
 *
135
 * Revision 1.8  2006/05/30 05:26:38  jorpiell
136
 * isWritable ha sido a?adido a la interfaz, y hay que implementarlo
137
 *
138
 * Revision 1.7  2006/05/25 10:32:11  jorpiell
139
 * Se ha renombrado la clase WFSFields por WFSAttributes porque era algo confusa
140
 *
141
 * Revision 1.6  2006/05/23 13:21:28  jorpiell
142
 * Se tiene en cuanta el online resource
143
 *
144
 * Revision 1.5  2006/05/22 10:31:35  jorpiell
145
 * Cambio producido al cambiar una interfaz del geotools driver
146
 *
147
 * Revision 1.4  2006/05/22 10:11:10  jorpiell
148
 * Eliminadas algunas lineas innecesarias
149
 *
150
 * Revision 1.3  2006/05/19 12:53:54  jorpiell
151
 * 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.
152
 *
153
 * Revision 1.2  2006/04/20 16:38:24  jorpiell
154
 * 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().
155
 *
156
 * Revision 1.1  2006/04/19 12:50:16  jorpiell
157
 * Primer commit de la aplicaci?n. Se puede hacer un getCapabilities y ver el mensaje de vienvenida del servidor
158
 *
159
 *
160
 */
161
/**
162
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
163
 */
164
public class FMapWFSDriver implements WFSDriver, BoundedShapes,ObjectDriver{
165
        private WFSClient remoteServicesClient;
166
        private WFSLayerNode[] featuresList;
167
    private Hashtable hashFeatures;
168
    private DriverAttributes fields = new DriverAttributes();
169
    private GMLDriver driver = null;
170
    private boolean hasGeometry = false;
171

    
172
        FMapWFSDriver() {
173
                super();
174
        }
175

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

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

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

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

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

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

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

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

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

    
263

    
264

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

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

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

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

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

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

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

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

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

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

    
363

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

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

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

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

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

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

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

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

    
434

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

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

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

    
456
        }
457

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

    
465
        }
466

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

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

    
482
        }
483

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

    
491
        }
492

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

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

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

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

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

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

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

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

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

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

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

    
588
                return FShape.MULTI;
589
        }
590

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

    
597
        }
598

    
599

    
600
}