Statistics
| Revision:

svn-gvsig-desktop / tags / v10_RC2c / extensions / extWFS2 / src / com / iver / cit / gvsig / fmap / layers / FLyrWFS.java @ 8745

History | View | Annotate | Download (14.9 KB)

1
package com.iver.cit.gvsig.fmap.layers;
2

    
3
import java.awt.geom.Rectangle2D;
4
import java.io.IOException;
5
import java.net.ConnectException;
6
import java.net.MalformedURLException;
7
import java.net.URL;
8
import java.util.HashMap;
9
import java.util.Map;
10

    
11
import javax.swing.ImageIcon;
12

    
13
import org.cresques.cts.ProjectionPool;
14
import org.gvsig.remoteClient.wfs.WFSAttribute;
15
import org.gvsig.remoteClient.wfs.WFSStatus;
16

    
17
import com.iver.andami.PluginServices;
18
import com.iver.cit.gvsig.fmap.DriverException;
19
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
20
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
21
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
22
import com.iver.cit.gvsig.fmap.drivers.WFSException;
23
import com.iver.cit.gvsig.fmap.drivers.wfs.FMapWFSDriver;
24
import com.iver.cit.gvsig.fmap.drivers.wfs.FMapWFSDriverFactory;
25
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
26
import com.iver.utiles.StringUtilities;
27
import com.iver.utiles.XMLEntity;
28

    
29
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
30
 *
31
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
32
 *
33
 * This program is free software; you can redistribute it and/or
34
 * modify it under the terms of the GNU General Public License
35
 * as published by the Free Software Foundation; either version 2
36
 * of the License, or (at your option) any later version.
37
 *
38
 * This program is distributed in the hope that it will be useful,
39
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
41
 * GNU General Public License for more details.
42
 *
43
 * You should have received a copy of the GNU General Public License
44
 * along with this program; if not, write to the Free Software
45
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
46
 *
47
 * For more information, contact:
48
 *
49
 *  Generalitat Valenciana
50
 *   Conselleria d'Infraestructures i Transport
51
 *   Av. Blasco Ib??ez, 50
52
 *   46010 VALENCIA
53
 *   SPAIN
54
 *
55
 *      +34 963862235
56
 *   gvsig@gva.es
57
 *      www.gvsig.gva.es
58
 *
59
 *    or
60
 *
61
 *   IVER T.I. S.A
62
 *   Salamanca 50
63
 *   46005 Valencia
64
 *   Spain
65
 *
66
 *   +34 963163400
67
 *   dac@iver.es
68
 */
69
/* CVS MESSAGES:
70
 *
71
 * $Id: FLyrWFS.java 8745 2006-11-14 13:14:23Z  $
72
 * $Log$
73
 * Revision 1.10.2.4  2006-10-02 12:46:17  jorpiell
74
 * A?adidos los m?todos para escribirse y recuperarse del XMLEntity
75
 *
76
 * Revision 1.10.2.2  2006/09/28 08:54:01  jorpiell
77
 * Ya se puede reproyectar
78
 *
79
 * Revision 1.10.2.1  2006/09/26 07:36:24  jorpiell
80
 * El WFS no reproyectaba porque no se le asignaba a la capa un sistema de referencia. Ahora ya se hace.
81
 *
82
 * Revision 1.10  2006/09/05 15:41:52  jorpiell
83
 * A?adida la funcionalidad de cargar WFS desde el cat?logo
84
 *
85
 * Revision 1.9  2006/07/05 12:05:41  jorpiell
86
 * Se ha modificado para que avise si se han recuperado las mismas features que marca el campo buffer
87
 *
88
 * Revision 1.8  2006/06/21 12:52:10  jorpiell
89
 * A?adido un icono para la capa WFS
90
 *
91
 * Revision 1.7  2006/06/21 12:35:45  jorpiell
92
 * Se ha a?adido la ventana de propiedades. Esto implica a?adir listeners por todos los paneles. Adem?s no se muestra la geomatr?a en la lista de atributos y se muestran ?nicamnete los que se van a descargar
93
 *
94
 * Revision 1.6  2006/06/15 11:17:06  jorpiell
95
 * Se ha encontrado la forma de comprobar cuando se prodicia un error de parseo al hacer un hasnext (en la feature). Se atrapa y se lanza la excepci?n hacia arriba
96
 *
97
 * Revision 1.5  2006/06/14 07:57:19  jorpiell
98
 * Ya no se usa la estrategia ni se usa geotools para hacer el getFeature. Ahora se usa ?nicamente para el parseo de GML
99
 *
100
 * Revision 1.4  2006/06/05 16:49:31  caballero
101
 * poder editar y exportar
102
 *
103
 * Revision 1.3  2006/05/25 10:31:55  jorpiell
104
 * Se ha renombrado la clase WFSFields por WFSAttributes porque era algo confusa
105
 *
106
 * Revision 1.2  2006/05/23 13:21:59  jorpiell
107
 * Si hay alg?n problema en la carga se muestra un mensaje de error
108
 *
109
 * Revision 1.1  2006/05/19 12:54:11  jorpiell
110
 * Creada la capa WFS
111
 *
112
 *
113
 */
114
/**
115
 *  FMap's WFS Layer class.
116
 *
117
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
118
 */
119
public class FLyrWFS extends FLyrVect{
120
        private URL host = null;
121
        private String onlineResource = null;
122
        private String name = null;
123
        private String layerName = null;
124
    private String userName = null;
125
    private String password = null;
126
    private WFSAttribute[] fields = null;
127
    private int numfeatures = 100;
128
    private int timeout = 10000;
129
    private String srs = null;
130
    private FMapWFSDriver wfsDriver = null;
131
    private VectorialAdapter vectorialAdapter = null;
132
    private WFSLayerNode wfsLayerNode = null;
133
    
134
    /**
135
     * Constructor
136
     */
137
    public FLyrWFS(){
138
            super();
139
    }
140
    
141
    /**
142
     * Constructor to load a new layer from a catalog
143
     * search
144
     * @param args
145
     * Params to load a new layer
146
     * @throws WFSException 
147
     * @throws IOException 
148
     * @throws MalformedURLException 
149
     * @throws ConnectException 
150
     * @throws DriverIOException 
151
     */
152
    public FLyrWFS(Map args) throws WFSException, ConnectException, MalformedURLException, IOException, DriverIOException{
153
            String[] sLayer = (String[])args.get("layer");
154
                String user = (String)args.get("user");
155
                String pwd = (String)args.get("pwd");
156
                String host = (String)args.get("host");        
157
                String projection = (String)args.get("projection");
158
                
159
                FMapWFSDriver driver = FMapWFSDriverFactory.getFMapDriverForURL(new URL(host));
160
                WFSLayerNode[] layers = driver.getLayerList();
161
                WFSLayerNode layer = driver.getLayerInfo(sLayer[0]);
162
                
163
                layer.setSelectedFields(layer.getFields());
164
                
165
                WFSAttribute[] atts = new WFSAttribute[layer.getFields().size()];
166
                for (int i=0 ; i<atts.length ; i++){
167
                        atts[i] = (WFSAttribute)layer.getFields().get(i);
168
                }
169
        
170
                setHost(host);
171
                setName(sLayer[0]);
172
                setLayerName(sLayer[0]);
173
                setWfsLayerNode(layer);
174
                setFields(atts);
175
                setUserName(user);
176
                setPassword(pwd);
177
                setNumfeatures(10000);
178
                setTimeout(10000);
179
                setWfsDriver(driver);                        
180
                setProjection(CRSFactory.getCRS(projection));
181
                
182
            load();            
183
    }
184
    
185

    
186
    /**
187
     * Loads the features from the server
188
     */
189
    public void load() throws DriverIOException{
190
            WFSAdapter adapter = new WFSAdapter();
191
            try {
192
                    wfsDriver.getFeature(getWFSStatus());
193

    
194
                    adapter.setDriver((VectorialDriver) wfsDriver);
195
                    setSource(adapter);
196

    
197
                    setLegend(LegendFactory.createSingleSymbolLegend(
198
                                    getShapeType()));
199
            } catch (Exception e){
200
                    e.printStackTrace();
201
                    throw new DriverIOException(e.getMessage());
202
            }
203
    }
204

    
205
    /**
206
     * Gets the WFS Status
207
     * @return
208
     */
209
    private WFSStatus getWFSStatus(){
210
            WFSStatus status = new WFSStatus(getLayerName());
211
            status.setUserName(getUserName());
212
            status.setPassword(getPassword());
213
            status.setBuffer(getNumfeatures());
214
            status.setTimeout(getTimeout());
215
            status.setFields(getFieldNames());
216
            status.setOnlineResource(getOnlineResource());
217
            status.setSrs(getSrs());
218
                   return status;
219
    }
220

    
221
    /**
222
     * Adds the vectorial adapter to the layer
223
     * @param vectorialAdapter
224
     */
225
    public void setSource(VectorialAdapter vectorialAdapter) {
226
            this.vectorialAdapter = vectorialAdapter;
227
        super.setSource(vectorialAdapter);
228
    }
229

    
230
        /**
231
         * @return Returns the layerName.
232
         */
233
        public String getLayerName() {
234
                return layerName;
235
        }
236
        /**
237
         * @param layerName The layerName to set.
238
         */
239
        public void setLayerName(String layerName) {
240
                this.layerName = layerName;
241
        }
242
        /**
243
         * @return Returns the numfeatures.
244
         */
245
        public int getNumfeatures() {
246
                return numfeatures;
247
        }
248
        /**
249
         * @param numfeatures The numfeatures to set.
250
         */
251
        public void setNumfeatures(int numfeatures) {
252
                this.numfeatures = numfeatures;
253
        }
254

    
255
        /**
256
         * @return Returns the pwd.
257
         */
258
        public String getPassword() {
259
                return password;
260
        }
261
        /**
262
         * @param pwd The pwd to set.
263
         */
264
        public void setPassword(String password) {
265
                this.password = password;
266
        }
267
        /**
268
         * @return Returns the timeout.
269
         */
270
        public int getTimeout() {
271
                return timeout;
272
        }
273
        /**
274
         * @param timeout The timeout to set.
275
         */
276
        public void setTimeout(int timeout) {
277
                this.timeout = timeout;
278
        }
279
        /**
280
         * @return Returns the user.
281
         */
282
        public String getUserName() {
283
                return userName;
284
        }
285
        /**
286
         * @param user The user to set.
287
         */
288
        public void setUserName(String userName) {
289
                this.userName = userName;
290
        }
291
        /**
292
         * @return Returns the fields.
293
         */
294
        public WFSAttribute[] getFields() {
295
                if (fields == null){
296
                        return new WFSAttribute[0];
297
                }
298
                return fields;
299
        }
300

    
301
        /**
302
         * Return the attributes name
303
         * @return
304
         */
305
        public String[] getFieldNames(){
306
                String[] attributes = new String[getFields().length];
307
                for (int i=0 ; i<getFields().length ; i++){
308
                        attributes[i] = getFields()[i].getName();
309
                }
310
                return attributes;
311
        }
312

    
313
        /**
314
         * @param fields The fields to set.
315
         */
316
        public void setFields(WFSAttribute[] fields) {
317
                this.fields = fields;
318
        }
319
        /**
320
         * @return Returns the name.
321
         */
322
        public String getName() {
323
                return name;
324
        }
325
        /**
326
         * @param name The name to set.
327
         */
328
        public void setName(String name) {
329
                this.name = name;
330
        }
331
        /**
332
         * @return Returns the host.
333
         */
334
        public URL getHost() {
335
                return host;
336
        }
337
        /**
338
         * @param host The host to set.
339
         */
340
        public void setHost(URL host) {
341
                this.host = host;
342
        }
343

    
344
        /**
345
         * @return Returns the wfs.
346
         */
347
        public FMapWFSDriver getWfsDriver() {
348
                return wfsDriver;
349
        }
350

    
351
        /**
352
         * @param wfs The wfs to set.
353
         */
354
        public void setWfsDriver(FMapWFSDriver wfs) {
355
                this.wfsDriver = wfs;
356
        }
357

    
358

    
359
        /*
360
         *  (non-Javadoc)
361
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getStrategy()
362
         */
363
//    public Strategy getStrategy() {
364
//        if (wfsStrategy == null){
365
//                wfsStrategy = new WFSStrategy(this);
366
//        }
367
//            return wfsStrategy;
368
//    }
369

    
370
    /*
371
     *  (non-Javadoc)
372
     * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
373
     */
374
    public Rectangle2D getFullExtent() throws DriverException {
375
            try {
376
                    Rectangle2D extent = wfsDriver.getFullExtent();
377
                    if (getCoordTrans() != null){
378
                            return getCoordTrans().convert(extent);
379
                    }
380
                        return extent;
381
                } catch (IOException e) {
382
                        throw new DriverException(e.toString());
383
                }
384
        }
385

    
386
        /**
387
         * @return Returns the onlineResource.
388
         */
389
        public String getOnlineResource() {
390
                return onlineResource;
391
        }
392

    
393
        /**
394
         * @param onlineResource The onlineResource to set.
395
         */
396
        public void setOnlineResource(String onlineResource) {
397
                this.onlineResource = onlineResource;
398
        }
399

    
400
        public HashMap getProperties() {
401
                HashMap info = new HashMap();
402
                info.put("name", getName());
403
                info.put("layerName", getLayerName());
404
                info.put("layerText", getLayerText());
405
                info.put("attributes", getFields());
406
                info.put("host", getHost());        
407
                info.put("wfsLayerNode", getWfsLayerNode());        
408
                WFSStatus status = new WFSStatus(getLayerName());
409
                status.setBuffer(getNumfeatures());
410
                status.setTimeout(getTimeout());
411
                status.setUserName(getUserName());
412
                status.setPassword(getPassword());
413
                info.put("status",status);                        
414
                return info;
415

    
416
        }
417

    
418
        /**
419
         * @return Returns the wfsLayerNode.
420
         */
421
        public WFSLayerNode getWfsLayerNode() {
422
                return wfsLayerNode;
423
        }
424

    
425
        /**
426
         * @param wfsLayerNode The wfsLayerNode to set.
427
         */
428
        public void setWfsLayerNode(WFSLayerNode wfsLayerNode) {
429
                this.wfsLayerNode = wfsLayerNode;
430
        }
431

    
432
        public void setHost(String host2) {
433
                try {
434
                        setHost(new URL(host2));
435
                } catch (MalformedURLException e) {
436
                        // TODO Auto-generated catch block
437
                        e.printStackTrace();
438
                }                
439
        }
440
        
441
        public ImageIcon getTocImageIcon() {                        
442
                return new ImageIcon(PluginServices.getPluginServices("com.iver.cit.gvsig.wfs2").getClassLoader().getResource("images/icoLayer.png"));
443
        }
444
        
445
        /*
446
         *  (non-Javadoc)
447
         * @see com.iver.cit.gvsig.fmap.layers.FLyrVect#isPropertiesMenuVisible()
448
         */
449
        public boolean isPropertiesMenuVisible(){
450
                return false;
451
        }
452

    
453
        public String getSrs() {
454
                return srs;
455
        }
456

    
457
        public void setSrs(String srs) {
458
                this.srs = srs;
459
        }
460
        
461
        /**
462
         * @return XMLEntity.
463
         * @throws XMLException
464
         */
465
        public XMLEntity getXMLEntity() throws XMLException {
466
                XMLEntity xml = super.getXMLEntity();                
467
                
468
                // Full extent
469
                try {
470
                        xml.putProperty("fullExtent", StringUtilities.rect2String(getFullExtent()));
471
                } catch (DriverException e) {
472
                        // TODO Auto-generated catch block
473
                        e.printStackTrace();
474
                }
475

    
476
                // Host
477
                xml.putProperty("host", host.toExternalForm());
478
                
479
                // Layer name
480
                xml.putProperty("layerName", getLayerName());
481

    
482
                //Layer fields
483
                String strFields = "";
484
                WFSAttribute[] fields = getFields();
485
                for (int i=0 ; i<fields.length ; i++){
486
                        strFields = strFields + fields[i].getName();
487
                        if (i < fields.length - 1){
488
                                strFields = strFields + "~##SEP1##~";
489
                        }
490
                }
491
                xml.putProperty("fields", strFields);
492
                
493
                // User name
494
                xml.putProperty("user", getUserName());
495
        
496
                // SRS
497
                xml.putProperty("srs", getSrs());                
498

    
499
        // OnlineResources
500
        xml.putProperty("onlineResource", getOnlineResource());
501
      
502
            // TimeOut
503
                xml.putProperty("timeout", getTimeout());                
504

    
505
        // Buffer
506
        xml.putProperty("buffer", getNumfeatures());
507
        
508
        //Projection
509
        xml.putProperty("projection",getProjection().getAbrev());
510
        
511
        return xml;
512
        }
513
        
514
        /**
515
         * @param xml XMLEntity
516
         *
517
         * @throws XMLException
518
         * @throws DriverException
519
         * @throws DriverIOException
520
         */
521
        public void setXMLEntity(XMLEntity xml)throws XMLException {
522
                super.setXMLEntity(xml);
523
                
524
                // Host
525
                try {
526
                        host = new URL(xml.getStringProperty("host"));
527
                } catch (MalformedURLException e) {
528
                        throw new XMLException(e);
529
                }
530
                
531
                // Layer name
532
                setLayerName(xml.getStringProperty("layerName"));                
533
                
534
                // User name
535
                if (xml.contains("user")){
536
                        setUserName(xml.getStringProperty("user"));
537
                }
538
                
539
                // SRS
540
                if (xml.contains("srs")){
541
                        setSrs(xml.getStringProperty("srs"));
542
                }
543
                
544
                // OnlineResources
545
                if (xml.contains("onlineResource")){
546
                        setOnlineResource(xml.getStringProperty("onlineResource"));
547
                }
548
                
549
                // TimeOut
550
                if (xml.contains("timeout")){
551
                        setTimeout(xml.getIntProperty("timeout"));
552
                }
553
                
554
                // Buffer
555
                if (xml.contains("buffer")){
556
                        setNumfeatures(xml.getIntProperty("buffer"));
557
                }
558
                
559
                 //Projection
560
                if (xml.contains("projection")){
561
                        setProjection(CRSFactory.getCRS(xml.getStringProperty("projection")));
562
                }              
563
                
564
                // Layer fields
565
                FMapWFSDriver driver;
566
                try {
567
                        driver = FMapWFSDriverFactory.getFMapDriverForURL(host);
568
                } catch (Exception e) {
569
                        throw new XMLException(e);        
570
                }
571
                WFSLayerNode[] layers = driver.getLayerList();
572
                WFSLayerNode layer = driver.getLayerInfo(getLayerName());                        
573
                layer.setSelectedFields(layer.getFields());                                        
574
                WFSAttribute[] atts = null;
575
                
576
                if (xml.contains("fields")) {
577
                        String[] fields = xml.getStringProperty("fields").split("~##SEP1##~");
578
                        atts = new WFSAttribute[fields.length];
579
                        for (int i=0 ; i<fields.length ; i++){
580
                                for (int j=0 ; j<layer.getFields().size() ; j++){
581
                                        WFSAttribute attribute = (WFSAttribute)layer.getFields().get(j);
582
                                        if (attribute.getName().equals(fields[i])){
583
                                                atts[i] = attribute;
584
                                                break;
585
                                        }
586
                                }                                        
587
                        }
588
                }else{
589
                        atts = new WFSAttribute[0];
590
                }
591
                layer.setSelectedFields(atts);
592
                setWfsDriver(driver);        
593
                setWfsLayerNode(layer);
594
                setFields(atts);        
595
                setAvailable(true);
596
        }
597
}