Statistics
| Revision:

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

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.sql.Types;
9
import java.util.ArrayList;
10
import java.util.Hashtable;
11
import java.util.Iterator;
12

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

    
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.hardcode.gdbms.engine.values.ValueFactory;
26
import com.iver.cit.gvsig.fmap.DriverException;
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.UnsupportedVersionException;
34
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
35
import com.iver.cit.gvsig.fmap.drivers.WFSDriver;
36
import com.iver.cit.gvsig.fmap.drivers.WFSException;
37
import com.iver.cit.gvsig.fmap.drivers.geotools.WFSFeaturesIterator;
38
import com.iver.cit.gvsig.fmap.drivers.geotools.WFSFeatureLayer;
39
import com.iver.cit.gvsig.fmap.drivers.geotools.WFSGetFeatureParser;
40
import com.iver.cit.gvsig.fmap.drivers.geotools.WFSTypesConversion;
41
import com.iver.cit.gvsig.fmap.layers.WFSLayerNode;
42

    
43
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
44
 *
45
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
46
 *
47
 * This program is free software; you can redistribute it and/or
48
 * modify it under the terms of the GNU General Public License
49
 * as published by the Free Software Foundation; either version 2
50
 * of the License, or (at your option) any later version.
51
 *
52
 * This program is distributed in the hope that it will be useful,
53
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
54
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
55
 * GNU General Public License for more details.
56
 *
57
 * You should have received a copy of the GNU General Public License
58
 * along with this program; if not, write to the Free Software
59
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
60
 *
61
 * For more information, contact:
62
 *
63
 *  Generalitat Valenciana
64
 *   Conselleria d'Infraestructures i Transport
65
 *   Av. Blasco Ib??ez, 50
66
 *   46010 VALENCIA
67
 *   SPAIN
68
 *
69
 *      +34 963862235
70
 *   gvsig@gva.es
71
 *      www.gvsig.gva.es
72
 *
73
 *    or
74
 *
75
 *   IVER T.I. S.A
76
 *   Salamanca 50
77
 *   46005 Valencia
78
 *   Spain
79
 *
80
 *   +34 963163400
81
 *   dac@iver.es
82
 */
83
/* CVS MESSAGES:
84
 *
85
 * $Id: FMapWFSDriver.java 6508 2006-07-24 08:27:35Z jorpiell $
86
 * $Log$
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 WFSFeatureLayer featureIterator= null;
151
    private DriverAttributes attr = new DriverAttributes();
152

    
153
        public FMapWFSDriver() {
154
                super();
155
        }
156

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

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

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

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

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

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

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

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

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

    
243

    
244

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

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

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

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

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

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

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

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

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

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

    
345

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

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

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

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

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

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

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

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

    
416

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

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

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

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

    
445
        }
446

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

    
454
        }
455

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

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

    
471
        }
472

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

    
480
        }
481

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

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

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

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

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

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

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

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

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

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

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

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

    
583
        /*
584
         *  (non-Javadoc)
585
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#reload()
586
         */
587
        public void reload() throws IOException, com.hardcode.gdbms.engine.data.driver.DriverException {
588
                featureIterator.reload();
589
        }
590

    
591

    
592
}