Statistics
| Revision:

root / trunk / extensions / extWMS / src / com / iver / cit / gvsig / fmap / drivers / wms / FMapWMSDriver.java @ 37961

History | View | Annotate | Download (10.5 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.drivers.wms;
42

    
43
import java.awt.geom.Rectangle2D;
44
import java.io.File;
45
import java.io.IOException;
46
import java.net.ConnectException;
47
import java.net.URL;
48
import java.util.ArrayList;
49
import java.util.Hashtable;
50
import java.util.Iterator;
51
import java.util.TreeMap;
52
import java.util.Vector;
53

    
54
import org.gvsig.remoteClient.exceptions.ServerErrorException;
55
import org.gvsig.remoteClient.wms.ICancellable;
56
import org.gvsig.remoteClient.wms.WMSClient;
57
import org.gvsig.remoteClient.wms.WMSDimension;
58
import org.gvsig.remoteClient.wms.WMSLayer;
59
import org.gvsig.remoteClient.wms.WMSStatus;
60
import org.gvsig.remoteClient.wms.WMSStyle;
61
import org.gvsig.remoteClient.wms.WMSProtocolHandler.ServiceInformation;
62

    
63
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode;
64
/**
65
 * Driver WMS.
66
 *
67
 * @author Jaume Dominguez Faus
68
 */
69
public class FMapWMSDriver  {
70
        private WMSClient client;
71
    private WMSLayerNode fmapRootLayer;
72
    private TreeMap layers = new TreeMap();
73
    private URL url;
74

    
75
    private FMapWMSDriver() {}
76

    
77
    protected FMapWMSDriver(URL url) throws ConnectException, IOException {
78
            client = new WMSClient(url.toString());
79
    }
80

    
81
    public String[] getLayerNames(){
82
            return client.getLayerNames();
83
    }
84
    public String[] getLayerTitles(){
85
            return client.getLayerTitles();
86
    }
87
    /*
88
     *  (non-Javadoc)
89
     * @see com.iver.cit.gvsig.fmap.drivers.WMSDriver#getCapabilities(java.net.URL)
90
     */
91
        public void getCapabilities(URL server)
92
                throws WMSException {
93
//                try {
94
//                        client.connect();
95
//                } catch (Exception e) {
96
//                        throw new WMSException(e);
97
//                }
98
        }
99

    
100
        /*
101
         *  (non-Javadoc)
102
         * @see com.iver.cit.gvsig.fmap.drivers.WMSDriver#getMap(org.gvsig.remoteClient.wms.WMSStatus)
103
         */
104
    public File getMap(WMSStatus status, ICancellable cancel) throws WMSException {
105
        try {
106
                        return client.getMap(status, cancel);
107
        } catch (org.gvsig.remoteClient.exceptions.WMSException e) {
108
            throw new WMSException(e.getMessage());
109
        } catch (ServerErrorException e) {
110
            throw new WMSException("WMS Unexpected server error."+e.getMessage());
111
        }
112
    }
113

    
114
    /**
115
     * Gets the legend graphic of one layer
116
     */
117
    public File getLegendGraphic(WMSStatus status, String layerName, ICancellable cancel) throws WMSException {
118
        try {
119
                File f = client.getLegendGraphic(status, layerName, cancel);
120
                        return f;
121
        } catch (org.gvsig.remoteClient.exceptions.WMSException e) {
122
            throw new WMSException(e.getMessage());
123
        } catch (ServerErrorException e) {
124
            throw new WMSException("WMS Unexpected server error."+e.getMessage());
125
        }
126
    }
127

    
128
        /**
129
         * Devuelve WMSClient a partir de su URL.
130
         *
131
         * @param url URL.
132
         *
133
         * @return WMSClient.
134
         * @throws IOException
135
         * @throws ConnectException
136
         *
137
         * @throws UnsupportedVersionException
138
         * @throws IOException
139
         */
140

    
141

    
142
    /**
143
     * Establishes the connection.
144
     * @param override, if true the previous downloaded data will be overridden
145
     * @return <b>true</b> if the connection was successful, or <b>false</b> if it was no possible
146
     * to establish the connection for any reason such as version negotiation.
147
     */
148
    public boolean connect(boolean override, ICancellable cancel) {
149
            if (override) {
150
                    fmapRootLayer = null;
151
                    layers.clear();
152
            }
153
                return client.connect(override, cancel);
154
    }
155

    
156
    public boolean connect(ICancellable cancel) {
157
            return client.connect(false, cancel);
158
    }
159

    
160
    /**
161
     * @return the version of this client.
162
     */
163
    public String getVersion() {
164
            return client.getVersion();
165
    }
166

    
167
    /**
168
     * @return The title of the service offered by the WMS server.
169
     */
170
    public String getServiceTitle() {
171
                return client.getServiceInformation().title;
172
    }
173

    
174
    /**
175
     * Returns a Hash table containing the values for each online resource.
176
     * Using as key a String with name of the WMS request and the value returned
177
     * by the hash is another string containing the corresponding Url
178
     * @return HashTable
179
     */
180
    public Hashtable getOnlineResources() {
181
            Hashtable onlineResources = new Hashtable();
182
            ServiceInformation si = client.getServiceInformation();
183
            Iterator it = si.operations.keySet().iterator();
184
            while (it.hasNext()) {
185
                    String key = (String) it.next();
186
                    String val = (String) si.operations.get(key);
187
                    if (val==null && (si.online_resource!=null || si.online_resource!= ""))
188
                            val = si.online_resource;
189
                    if (val!=null) {
190
                            onlineResources.put(key, val);
191
                    }
192
            }
193

    
194
            return onlineResources;
195
    }
196
    /**
197
     * @return <b>Vector</b> containing strings with the formats supported by this server.
198
     */
199
    public Vector getFormats() {
200

    
201
            return client.getFormats();
202
    }
203

    
204
    public Vector getInfoFormats() {
205

    
206
            return client.getServiceInformation().infoFormats;
207
    }
208

    
209
    /**
210
     * @return <b>Boolean</b> returns if the WMS is queryable (suppports GetFeatureInfo)
211
     */
212
    public boolean isQueryable() {
213
        return client.isQueryable();
214
    }
215
    /**
216
     * @return <b>Boolean</b> returns if the WMS supports getLegendGraphic operation (suppports GetFeatureInfo)
217
     */
218
    public boolean hasLegendGraphic() {
219
        return client.hasLegendGraphic();
220
    }
221
    /**
222
     * @return A tree containing the info of all layers available on this server.
223
     */
224
    public WMSLayerNode getLayersTree() {
225
        if (fmapRootLayer == null){
226
            WMSLayer clientRoot;
227
            if (client.getLayersRoot() == null) {
228
                    client.connect(false, null);
229
            }
230
            clientRoot = client.getLayersRoot();
231

    
232

    
233
            fmapRootLayer = parseTree(clientRoot, null);
234
        }
235
        return fmapRootLayer;
236
    }
237

    
238
    /**
239
     * Parses the client's layer node and translates it to a fmap's layer node
240
     * @param WMSLayer
241
     * @return WMSLayerNode
242
     */
243
    private WMSLayerNode parseTree(WMSLayer node, WMSLayerNode parentNode) {
244

    
245
        WMSLayerNode myNode = new WMSLayerNode();
246

    
247
        // Name
248
        myNode.setName(node.getName());
249

    
250
        // Title
251
        myNode.setTitle(node.getTitle());
252

    
253
        // Transparency
254
        myNode.setTransparency(node.hasTransparency());
255

    
256
        // SRS
257
        myNode.setSrs(node.getAllSrs());
258

    
259

    
260
        // Queryable
261

    
262
        myNode.setQueryable(node.isQueryable() && client.getServiceInformation().isQueryable());
263

    
264
        // Parent layer
265
        myNode.setParent(parentNode);
266

    
267
        // Abstract
268
        myNode.setAbstract(node.getAbstract());
269
        
270
        myNode.setScaleMax(node.getScaleMax());
271
        myNode.setScaleMin(node.getScaleMin());
272

    
273
        // Fixed Size
274
        myNode.setFixedSize(node.getfixedWidth(), node.getfixedHeight());
275

    
276
        // LatLonBox
277
        if (node.getLatLonBox()!=null)
278
            myNode.setLatLonBox(node.getLatLonBox().toString());
279

    
280
        // Keywords
281
        ArrayList keywords = node.getKeywords();
282
        for (int i = 0; i < keywords.size(); i++) {
283
                myNode.addKeyword((String) keywords.get(i));
284
                }
285

    
286
        // Styles
287
        ArrayList styles = node.getStyles();
288
        for (int i = 0; i < styles.size(); i++) {
289
            WMSStyle style = (WMSStyle) styles.get(i);
290
            myNode.addStyle(style);
291
        }
292

    
293
        // Dimensions
294
        ArrayList dimensions = node.getDimensions();
295
        for (int i = 0; i < dimensions.size(); i++) {
296
            WMSDimension d = (WMSDimension) dimensions.get(i);
297
            myNode.addDimension(d.getName(), d.getUnits(), d.getUnitSymbol(), d.getDimensionExpression());
298
        }
299

    
300
        // Children
301
        int children = node.getChildren().size();
302
        myNode.setChildren(new ArrayList());
303
        for (int i = 0; i < children; i++) {
304
            myNode.getChildren().add(parseTree((WMSLayer)node.getChildren().get(i), myNode));
305
        }
306

    
307
        if (myNode.getName()!=null)
308
            layers.put(myNode.getName(), myNode);
309

    
310
        return myNode;
311
    }
312

    
313
    /**
314
     * @return
315
     */
316
    public String getAbstract() {
317

    
318
            return client.getServiceInformation().abstr;
319

    
320
    }
321

    
322
    /**
323
     * @param layerName
324
     * @param srs
325
     * @return
326
     */
327
    public Rectangle2D getLayersExtent(String[] layerName, String srs) {
328
            return client.getLayersExtent(layerName, srs);
329
    }
330

    
331
    /**
332
     * @param string
333
     * @return
334
     */
335
    public WMSLayerNode getLayer(String layerName) {
336
        if (getLayers().get(layerName) != null)
337
        {
338
            return (WMSLayerNode)layers.get(layerName);
339
        }
340
        return null;
341
    }
342

    
343
    /**
344
     * @return
345
     */
346
    private TreeMap getLayers() {
347
        if (fmapRootLayer == null){
348
            fmapRootLayer = getLayersTree();
349
        }
350
        return layers;
351
    }
352

    
353
    /**
354
     * @param wmsStatus
355
     * @param i
356
     * @param j
357
     * @param max_value
358
     * @return
359
     * @throws WMSException
360
     */
361
    public String getFeatureInfo(WMSStatus _wmsStatus, int i, int j, int max_value, ICancellable cancellable) throws WMSException {
362
        try {
363
            return client.getFeatureInfo(_wmsStatus, i, j, max_value, cancellable);
364
        } catch (org.gvsig.remoteClient.exceptions.WMSException e) {
365
            throw new WMSException();
366
                }
367
    }
368

    
369
    public String getHost(){
370
            return client.getHost();
371

    
372
    }
373

    
374
    /**
375
     * @return <b>Boolean</b> returns true if the layer has LegendUrl in the "style" section (capabilities)
376
     * 
377
     */
378
    public boolean hasLegendUrl(WMSStatus wmsStatus, String layerQuery) {
379
            return client.hasLegendUrl(wmsStatus,layerQuery);
380
    }
381

    
382
}