Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_907 / extensions / extWMS / src / com / iver / cit / gvsig / fmap / drivers / wms / FMapWMSDriver.java @ 11015

History | View | Annotate | Download (10.1 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
    /**
205
     * @return <b>Boolean</b> returns if the WMS is queryable (suppports GetFeatureInfo)
206
     */
207
    public boolean isQueryable() {
208
        return client.isQueryable();
209
    }
210
    /**
211
     * @return <b>Boolean</b> returns if the WMS supports getLegendGraphic operation (suppports GetFeatureInfo)
212
     */
213
    public boolean hasLegendGraphic() {
214
        return client.hasLegendGraphic();
215
    }
216
    /**
217
     * @return A tree containing the info of all layers available on this server.
218
     */
219
    public WMSLayerNode getLayersTree() {
220
        if (fmapRootLayer == null){
221
            WMSLayer clientRoot;
222
            if (client.getLayersRoot() == null) {
223
                    client.connect(false, null);
224
            }
225
            clientRoot = client.getLayersRoot();
226

    
227

    
228
            fmapRootLayer = parseTree(clientRoot, null);
229
        }
230
        return fmapRootLayer;
231
    }
232

    
233
    /**
234
     * Parses the client's layer node and translates it to a fmap's layer node
235
     * @param WMSLayer
236
     * @return WMSLayerNode
237
     */
238
    private WMSLayerNode parseTree(WMSLayer node, WMSLayerNode parentNode) {
239

    
240
        WMSLayerNode myNode = new WMSLayerNode();
241

    
242
        // Name
243
        myNode.setName(node.getName());
244

    
245
        // Title
246
        myNode.setTitle(node.getTitle());
247

    
248
        // Transparency
249
        myNode.setTransparency(node.hasTransparency());
250

    
251
        // SRS
252
        myNode.setSrs(node.getAllSrs());
253

    
254

    
255
        // Queryable
256

    
257
        myNode.setQueryable(node.isQueryable() && client.getServiceInformation().isQueryable());
258

    
259
        // Parent layer
260
        myNode.setParent(parentNode);
261

    
262
        // Abstract
263
        myNode.setAbstract(node.getAbstract());
264

    
265
        // Fixed Size
266
        myNode.setFixedSize(node.getfixedWidth(), node.getfixedHeight());
267

    
268
        // LatLonBox
269
        if (node.getLatLonBox()!=null)
270
            myNode.setLatLonBox(node.getLatLonBox().toString());
271

    
272
        // Keywords
273
        ArrayList keywords = node.getKeywords();
274
        for (int i = 0; i < keywords.size(); i++) {
275
                myNode.addKeyword((String) keywords.get(i));
276
                }
277

    
278
        // Styles
279
        ArrayList styles = node.getStyles();
280
        for (int i = 0; i < styles.size(); i++) {
281
            WMSStyle style = (WMSStyle) styles.get(i);
282
            myNode.addStyle(style.getName(), style.getTitle(), style.getAbstract());
283
        }
284

    
285
        // Dimensions
286
        ArrayList dimensions = node.getDimensions();
287
        for (int i = 0; i < dimensions.size(); i++) {
288
            WMSDimension d = (WMSDimension) dimensions.get(i);
289
            myNode.addDimension(d.getName(), d.getUnits(), d.getUnitSymbol(), d.getDimensionExpression());
290
        }
291

    
292
        // Childs
293
        int children = node.getChildren().size();
294
        myNode.setChildren(new ArrayList());
295
        for (int i = 0; i < children; i++) {
296
            myNode.getChildren().add(parseTree((WMSLayer)node.getChildren().get(i), myNode));
297
        }
298

    
299

    
300
        if (myNode.getName()!=null)
301
            layers.put(myNode.getName(), myNode);
302

    
303
        return myNode;
304
    }
305

    
306
    /**
307
     * @return
308
     */
309
    public String getAbstract() {
310

    
311
            return client.getServiceInformation().abstr;
312

    
313
    }
314

    
315
    /**
316
     * @param layerName
317
     * @param srs
318
     * @return
319
     */
320
    public Rectangle2D getLayersExtent(String[] layerName, String srs) {
321
            return client.getLayersExtent(layerName, srs);
322
    }
323

    
324
    /**
325
     * @param string
326
     * @return
327
     */
328
    public WMSLayerNode getLayer(String layerName) {
329
        if (getLayers().get(layerName) != null)
330
        {
331
            return (WMSLayerNode)layers.get(layerName);
332
        }
333
        return null;
334
    }
335

    
336
    /**
337
     * @return
338
     */
339
    private TreeMap getLayers() {
340
        if (fmapRootLayer == null){
341
            fmapRootLayer = getLayersTree();
342
        }
343
        return layers;
344
    }
345

    
346
    /**
347
     * @param wmsStatus
348
     * @param i
349
     * @param j
350
     * @param max_value
351
     * @return
352
     * @throws WMSException
353
     */
354
    public String getFeatureInfo(WMSStatus _wmsStatus, int i, int j, int max_value, ICancellable cancellable) throws WMSException {
355
        try {
356
            return client.getFeatureInfo(_wmsStatus, i, j, max_value, cancellable);
357
        } catch (org.gvsig.remoteClient.exceptions.WMSException e) {
358
            throw new WMSException();
359
                }
360
    }
361

    
362
    public String getHost(){
363
            return client.getHost();
364

    
365
    }
366
}