Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / extensions / extWFS2 / src / com / iver / cit / gvsig / fmap / drivers / wfs / FMapWFSDriver.java @ 7355

History | View | Annotate | Download (16.7 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.engine.data.DataSourceFactory;
20
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
21
import com.hardcode.gdbms.engine.data.edition.DataWare;
22
import com.hardcode.gdbms.engine.values.Value;
23
import com.iver.cit.gvsig.fmap.DriverException;
24
import com.iver.cit.gvsig.fmap.core.FShape;
25
import com.iver.cit.gvsig.fmap.core.IFeature;
26
import com.iver.cit.gvsig.fmap.core.IGeometry;
27
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
28
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
29
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
30
import com.iver.cit.gvsig.fmap.drivers.UnsupportedVersionException;
31
import com.iver.cit.gvsig.fmap.drivers.WFSDriver;
32
import com.iver.cit.gvsig.fmap.drivers.WFSException;
33
import com.iver.cit.gvsig.fmap.drivers.gml.GMLDriver;
34
import com.iver.cit.gvsig.fmap.drivers.gml.FMAPGeometryFactory.FeatureWithAttributes;
35
import com.iver.cit.gvsig.fmap.layers.WFSLayerNode;
36

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

    
158
        /*
159
         *  (non-Javadoc)
160
         * @see com.iver.cit.gvsig.fmap.drivers.WFSDriver#getCapabilities(java.net.URL)
161
         */
162
        public void getCapabilities(URL server)
163
                throws WFSException {
164
                        try {
165
                                getClient(server).connect(null);
166
                        } catch (Exception e) {
167
                                throw new WFSException(e);
168
                        }
169
        }
170

    
171
         /*
172
     *  (non-Javadoc)
173
     * @see com.iver.cit.gvsig.fmap.drivers.WFSDriver#describeFeatureType(java.lang.String)
174
     */
175
        public void describeFeatureType(String featureType) throws WFSException {
176
                remoteServicesClient.describeFeatureType(new WFSStatus(featureType));
177
        }
178

    
179
        /*
180
         *  (non-Javadoc)
181
         * @see com.iver.cit.gvsig.fmap.drivers.WFSDriver#getFeature(org.gvsig.remoteClient.wfs.WFSStatus)
182
         */
183
        public void getFeature(WFSStatus wfsStatus) throws WFSException {
184
                File file = remoteServicesClient.getFeature(wfsStatus);
185
                try {
186
                        driver = new GMLDriver();
187
                        driver.open(file);
188
                        driver.initialize();        
189
                        hasGeometry = WFSUtils.getHasGeometry(featuresList,wfsStatus);
190
                } catch (Exception e) {
191
                        Logger.getLogger(getClass().getName()).error(e.getMessage());
192
                        throw new WFSException(e);
193
                }
194
        }
195

    
196
        /*
197
         *  (non-Javadoc)
198
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeType()
199
         */
200
        public int getShapeType() {
201
                return FShape.MULTI;
202
        }
203

    
204
        /*
205
         *  (non-Javadoc)
206
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeCount()
207
         */
208
        public int getShapeCount() throws IOException {
209
                return getNumReg();
210
        }
211

    
212
        /*
213
         *  (non-Javadoc)
214
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
215
         */
216
        public DriverAttributes getDriverAttributes() {
217
                 return attr;
218
        }
219

    
220
        /*
221
         *  (non-Javadoc)
222
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getFullExtent()
223
         */
224
        public Rectangle2D getFullExtent() throws IOException {
225
                return driver.getFullExtent();
226
        }
227

    
228
        /*
229
         *  (non-Javadoc)
230
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShape(int)
231
         */
232
        public IGeometry getShape(int index) throws IOException {
233
                return ((FeatureWithAttributes) getFeatures().get(index)).getFeature().getGeometry();
234
        }
235

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

    
244

    
245

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

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

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

    
289
        public boolean connect(ICancellable cancel) {
290
           return remoteServicesClient.connect(false, cancel);
291
        }
292

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

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

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

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

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

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

    
346

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

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

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

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

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

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

    
403
        /**
404
         * @return Returns the features.
405
         */
406
        public ArrayList getFeatures() {
407
                return driver.getFeatures();
408
        }
409

    
410
        /**
411
         * @return Returns the hashRelate.
412
         */
413
        public Hashtable getHashRelate() {
414
                return driver.getHashRelate();
415
        }
416

    
417

    
418
         /**
419
         * @return Returns the numReg.
420
         */
421
        public int getNumReg() {
422
                return driver.getHashRelate().size();
423
        }
424

    
425
         /**
426
         * @return Returns the numFiled.
427
         */
428
        public int getNumField() {
429
                return driver.getAttributeCount();
430
        }
431

    
432
        /*
433
         *  (non-Javadoc)
434
         * @see com.iver.cit.gvsig.fmap.drivers.WFSDriver#close()
435
         */
436
        public void close() {
437
                // TODO Auto-generated method stub
438

    
439
        }
440

    
441
        /*
442
         *  (non-Javadoc)
443
         * @see com.iver.cit.gvsig.fmap.drivers.WFSDriver#open()
444
         */
445
        public void open() throws DriverException {
446
                attr.setLoadedInMemory(true);
447

    
448
        }
449

    
450
        /*
451
         *  (non-Javadoc)
452
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
453
         */
454
        public int[] getPrimaryKeys() throws com.hardcode.gdbms.engine.data.driver.DriverException {
455
                throw new UnsupportedOperationException();
456
        }
457

    
458
        /*
459
         *  (non-Javadoc)
460
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
461
         */
462
        public void write(DataWare dataWare) throws com.hardcode.gdbms.engine.data.driver.DriverException {
463
                // TODO Auto-generated method stub
464

    
465
        }
466

    
467
        /*
468
         *  (non-Javadoc)
469
         * @see com.hardcode.gdbms.engine.data.driver.GDBMSDriver#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
470
         */
471
        public void setDataSourceFactory(DataSourceFactory dsf) {
472
                // TODO Auto-generated method stub
473

    
474
        }
475

    
476
        /*
477
         *  (non-Javadoc)
478
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long, int)
479
         */
480
        public Value getFieldValue(long rowIndex, int fieldId) throws com.hardcode.gdbms.engine.data.driver.DriverException {
481
                FeatureWithAttributes feature = (FeatureWithAttributes)getFeatures().get((int)rowIndex);
482
                return feature.getFeature().getAttribute(fieldId);
483
        }
484

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

    
493
        /*
494
         *  (non-Javadoc)
495
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldName(int)
496
         */
497
        public String getFieldName(int fieldId) throws com.hardcode.gdbms.engine.data.driver.DriverException {
498
                return driver.getAttributeName(fieldId);
499
        }
500

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

    
509
        /*
510
         *  (non-Javadoc)
511
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldType(int)
512
         */
513
        public int getFieldType(int i) throws com.hardcode.gdbms.engine.data.driver.DriverException {
514
                return ((Value)driver.getAttributeValue(i)).getSQLType();
515
        }
516

    
517
        /*
518
         *  (non-Javadoc)
519
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldWidth(int)
520
         */
521
        public int getFieldWidth(int i) throws com.hardcode.gdbms.engine.data.driver.DriverException {
522
                // TODO Auto-generated method stub
523
                return 100;
524
        }
525

    
526
        /*
527
         *  (non-Javadoc)
528
         * @see com.iver.cit.gvsig.fmap.drivers.WFSDriver#getFeatureIterator(java.awt.geom.Rectangle2D, java.lang.String)
529
         */
530
        public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) throws DriverException, IOException {
531
                return new WFSFeaturesIterator(driver.getIterator(),strEPSG);
532
        }
533

    
534
        /*
535
         *  (non-Javadoc)
536
         * @see com.iver.cit.gvsig.fmap.drivers.WFSDriver#getRowIndexByFID(com.iver.cit.gvsig.fmap.core.IFeature)
537
         */
538
        public int getRowIndexByFID(IFeature FID) {
539
                int resul;
540
                Integer rowIndex = (Integer) getHashRelate().get(FID.getID());
541
                resul = rowIndex.intValue();
542
                return resul;
543
        }
544

    
545
        /*
546
         *  (non-Javadoc)
547
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#isWritable()
548
         */
549
        public boolean isWritable() {
550
                // TODO Auto-generated method stub
551
                return false;
552
        }
553

    
554
        /*
555
         *  (non-Javadoc)
556
         * @see com.iver.cit.gvsig.fmap.drivers.BoundedShapes#getShapeBounds(int)
557
         */
558
        public Rectangle2D getShapeBounds(int index) throws IOException {
559
                return getShape(index).getBounds2D();
560
        }
561

    
562
        /*
563
         *  (non-Javadoc)
564
         * @see com.iver.cit.gvsig.fmap.drivers.BoundedShapes#getShapeType(int)
565
         */
566
        public int getShapeType(int index) {
567
                try {
568
                        if (getShapeCount() > 0){
569
                                return getShape(0).getGeometryType();
570
                        }
571

    
572
                } catch (IOException e) {
573
                        e.printStackTrace();
574
                }
575
                return FShape.MULTI;
576
        }
577

    
578
        /*
579
         *  (non-Javadoc)
580
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#reload()
581
         */
582
        public void reload() throws IOException, com.hardcode.gdbms.engine.data.driver.DriverException {
583
                
584
        }
585

    
586

    
587
}