Statistics
| Revision:

root / trunk / extensions / extWFS2 / src / com / iver / cit / gvsig / fmap / layers / FLyrWFS.java @ 7734

History | View | Annotate | Download (15.2 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
import java.util.Vector;
11

    
12
import javax.swing.ImageIcon;
13

    
14
import org.cresques.cts.IProjection;
15
import org.cresques.cts.ProjectionPool;
16
import org.cresques.geo.Projection;
17
import org.gvsig.remoteClient.wfs.WFSAttribute;
18
import org.gvsig.remoteClient.wfs.WFSStatus;
19

    
20
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
21
import com.iver.andami.PluginServices;
22
import com.iver.cit.gvsig.fmap.DriverException;
23
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
24
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
25
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
26
import com.iver.cit.gvsig.fmap.drivers.WFSException;
27
import com.iver.cit.gvsig.fmap.drivers.wfs.FMapWFSDriver;
28
import com.iver.cit.gvsig.fmap.drivers.wfs.FMapWFSDriverFactory;
29
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
30
import com.iver.utiles.StringUtilities;
31
import com.iver.utiles.XMLEntity;
32

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

    
196
    /**
197
     * Loads the features from the server
198
     */
199
    public void load() throws DriverIOException{
200
            WFSAdapter adapter = new WFSAdapter();
201
            try {
202
                    wfsDriver.getFeature(getWFSStatus());
203

    
204
                    adapter.setDriver((VectorialDriver) wfsDriver);
205
                    setSource(adapter);
206

    
207

    
208
                    setLegend(LegendFactory.createSingleSymbolLegend(
209
                                    getShapeType()));
210
            } catch (Exception e){
211
                    e.printStackTrace();
212
                    throw new DriverIOException(e.getMessage());
213
            }
214
    }
215

    
216
    /**
217
     * Gets the WFS Status
218
     * @return
219
     */
220
    private WFSStatus getWFSStatus(){
221
            WFSStatus status = new WFSStatus(getLayerName());
222
            status.setUserName(getUserName());
223
            status.setPassword(getPassword());
224
            status.setBuffer(getNumfeatures());
225
            status.setTimeout(getTimeout());
226
            status.setFields(getFieldNames());
227
            status.setOnlineResource(getOnlineResource());
228
            status.setSrs(getSrs());
229
                   return status;
230
    }
231

    
232
    /**
233
     * Adds the vectorial adapter to the layer
234
     * @param vectorialAdapter
235
     */
236
    public void setSource(VectorialAdapter vectorialAdapter) {
237
            this.vectorialAdapter = vectorialAdapter;
238
        super.setSource(vectorialAdapter);
239
    }
240

    
241
        /**
242
         * @return Returns the layerName.
243
         */
244
        public String getLayerName() {
245
                return layerName;
246
        }
247
        /**
248
         * @param layerName The layerName to set.
249
         */
250
        public void setLayerName(String layerName) {
251
                this.layerName = layerName;
252
        }
253
        /**
254
         * @return Returns the numfeatures.
255
         */
256
        public int getNumfeatures() {
257
                return numfeatures;
258
        }
259
        /**
260
         * @param numfeatures The numfeatures to set.
261
         */
262
        public void setNumfeatures(int numfeatures) {
263
                this.numfeatures = numfeatures;
264
        }
265

    
266
        /**
267
         * @return Returns the pwd.
268
         */
269
        public String getPassword() {
270
                return password;
271
        }
272
        /**
273
         * @param pwd The pwd to set.
274
         */
275
        public void setPassword(String password) {
276
                this.password = password;
277
        }
278
        /**
279
         * @return Returns the timeout.
280
         */
281
        public int getTimeout() {
282
                return timeout;
283
        }
284
        /**
285
         * @param timeout The timeout to set.
286
         */
287
        public void setTimeout(int timeout) {
288
                this.timeout = timeout;
289
        }
290
        /**
291
         * @return Returns the user.
292
         */
293
        public String getUserName() {
294
                return userName;
295
        }
296
        /**
297
         * @param user The user to set.
298
         */
299
        public void setUserName(String userName) {
300
                this.userName = userName;
301
        }
302
        /**
303
         * @return Returns the fields.
304
         */
305
        public WFSAttribute[] getFields() {
306
                if (fields == null){
307
                        return new WFSAttribute[0];
308
                }
309
                return fields;
310
        }
311

    
312
        /**
313
         * Return the attributes name
314
         * @return
315
         */
316
        public String[] getFieldNames(){
317
                String[] attributes = new String[getFields().length];
318
                for (int i=0 ; i<getFields().length ; i++){
319
                        attributes[i] = getFields()[i].getName();
320
                }
321
                return attributes;
322
        }
323

    
324
        /**
325
         * @param fields The fields to set.
326
         */
327
        public void setFields(WFSAttribute[] fields) {
328
                this.fields = fields;
329
        }
330
        /**
331
         * @return Returns the name.
332
         */
333
        public String getName() {
334
                return name;
335
        }
336
        /**
337
         * @param name The name to set.
338
         */
339
        public void setName(String name) {
340
                this.name = name;
341
        }
342
        /**
343
         * @return Returns the host.
344
         */
345
        public URL getHost() {
346
                return host;
347
        }
348
        /**
349
         * @param host The host to set.
350
         */
351
        public void setHost(URL host) {
352
                this.host = host;
353
        }
354

    
355
        /**
356
         * @return Returns the wfs.
357
         */
358
        public FMapWFSDriver getWfsDriver() {
359
                return wfsDriver;
360
        }
361

    
362
        /**
363
         * @param wfs The wfs to set.
364
         */
365
        public void setWfsDriver(FMapWFSDriver wfs) {
366
                this.wfsDriver = wfs;
367
        }
368

    
369

    
370
        /*
371
         *  (non-Javadoc)
372
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getStrategy()
373
         */
374
//    public Strategy getStrategy() {
375
//        if (wfsStrategy == null){
376
//                wfsStrategy = new WFSStrategy(this);
377
//        }
378
//            return wfsStrategy;
379
//    }
380

    
381
    /*
382
     *  (non-Javadoc)
383
     * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
384
     */
385
    public Rectangle2D getFullExtent() throws DriverException {
386
            try {
387
                    Rectangle2D extent = wfsDriver.getFullExtent();
388
                    if (getCoordTrans() != null){
389
                            return getCoordTrans().convert(extent);
390
                    }
391
                        return extent;
392
                } catch (IOException e) {
393
                        throw new DriverException(e.toString());
394
                }
395
        }
396

    
397
        /**
398
         * @return Returns the onlineResource.
399
         */
400
        public String getOnlineResource() {
401
                return onlineResource;
402
        }
403

    
404
        /**
405
         * @param onlineResource The onlineResource to set.
406
         */
407
        public void setOnlineResource(String onlineResource) {
408
                this.onlineResource = onlineResource;
409
        }
410

    
411
        public HashMap getProperties() {
412
                HashMap info = new HashMap();
413
                info.put("name", getName());
414
                info.put("layerName", getLayerName());
415
                info.put("layerText", getLayerText());
416
                info.put("attributes", getFields());
417
                info.put("host", getHost());        
418
                info.put("wfsLayerNode", getWfsLayerNode());        
419
                WFSStatus status = new WFSStatus(getLayerName());
420
                status.setBuffer(getNumfeatures());
421
                status.setTimeout(getTimeout());
422
                status.setUserName(getUserName());
423
                status.setPassword(getPassword());
424
                info.put("status",status);                        
425
                return info;
426

    
427
        }
428

    
429
        /**
430
         * @return Returns the wfsLayerNode.
431
         */
432
        public WFSLayerNode getWfsLayerNode() {
433
                return wfsLayerNode;
434
        }
435

    
436
        /**
437
         * @param wfsLayerNode The wfsLayerNode to set.
438
         */
439
        public void setWfsLayerNode(WFSLayerNode wfsLayerNode) {
440
                this.wfsLayerNode = wfsLayerNode;
441
        }
442

    
443
        public void setHost(String host2) {
444
                try {
445
                        setHost(new URL(host2));
446
                } catch (MalformedURLException e) {
447
                        // TODO Auto-generated catch block
448
                        e.printStackTrace();
449
                }                
450
        }
451
        
452
        public ImageIcon getTocImageIcon() {                        
453
                return new ImageIcon(PluginServices.getPluginServices("com.iver.cit.gvsig.wfs2").getClassLoader().getResource("images/icoLayer.png"));
454
        }
455
        
456
        /*
457
         *  (non-Javadoc)
458
         * @see com.iver.cit.gvsig.fmap.layers.FLyrVect#isPropertiesMenuVisible()
459
         */
460
        public boolean isPropertiesMenuVisible(){
461
                return false;
462
        }
463

    
464
        public String getSrs() {
465
                return srs;
466
        }
467

    
468
        public void setSrs(String srs) {
469
                this.srs = srs;
470
        }
471
        
472
        /**
473
         * @return XMLEntity.
474
         * @throws XMLException
475
         */
476
        public XMLEntity getXMLEntity() throws XMLException {
477
                XMLEntity xml = super.getXMLEntity();                
478
                
479
                // Full extent
480
                try {
481
                        xml.putProperty("fullExtent", StringUtilities.rect2String(getFullExtent()));
482
                } catch (DriverException e) {
483
                        // TODO Auto-generated catch block
484
                        e.printStackTrace();
485
                }
486

    
487
                // Host
488
                xml.putProperty("host", host.toExternalForm());
489
                
490
                // Layer name
491
                xml.putProperty("layerName", getLayerName());
492

    
493
                //Layer fields
494
                String strFields = "";
495
                WFSAttribute[] fields = getFields();
496
                for (int i=0 ; i<fields.length ; i++){
497
                        strFields = strFields + fields[i].getName();
498
                        if (i < fields.length - 1){
499
                                strFields = strFields + "~##SEP1##~";
500
                        }
501
                }
502
                xml.putProperty("fields", strFields);
503
                
504
                // User name
505
                xml.putProperty("user", getUserName());
506
        
507
                // SRS
508
                xml.putProperty("srs", getSrs());                
509

    
510
        // OnlineResources
511
        xml.putProperty("onlineResource", getOnlineResource());
512
      
513
            // TimeOut
514
                xml.putProperty("timeout", getTimeout());                
515

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