Statistics
| Revision:

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

History | View | Annotate | Download (10.1 KB)

1 4503 jaume
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2 3746 jaume
 *
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 4503 jaume
 *   Av. Blasco Ib??ez, 50
24 3746 jaume
 *   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 4222 jaume
import java.io.File;
45 3746 jaume
import java.io.IOException;
46 3804 ldiaz
import java.net.ConnectException;
47 3746 jaume
import java.net.URL;
48
import java.util.ArrayList;
49 4222 jaume
import java.util.Hashtable;
50
import java.util.Iterator;
51 3746 jaume
import java.util.TreeMap;
52
import java.util.Vector;
53
54
import org.gvsig.remoteClient.exceptions.ServerErrorException;
55 5409 jaume
import org.gvsig.remoteClient.wms.ICancellable;
56 3746 jaume
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 4222 jaume
import org.gvsig.remoteClient.wms.WMSProtocolHandler.ServiceInformation;
62 3746 jaume
63
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode;
64
/**
65
 * Driver WMS.
66
 *
67 4503 jaume
 * @author Jaume Dominguez Faus
68 3746 jaume
 */
69 5409 jaume
public class FMapWMSDriver  {
70 3746 jaume
        private WMSClient client;
71
    private WMSLayerNode fmapRootLayer;
72
    private TreeMap layers = new TreeMap();
73 5277 jmvivo
    private URL url;
74 6087 jaume
75
    private FMapWMSDriver() {}
76
77 5277 jmvivo
    protected FMapWMSDriver(URL url) throws ConnectException, IOException {
78
            client = new WMSClient(url.toString());
79
    }
80 6087 jaume
81 5435 ldiaz
    public String[] getLayerNames(){
82
            return client.getLayerNames();
83
    }
84 5709 ldiaz
    public String[] getLayerTitles(){
85
            return client.getLayerTitles();
86
    }
87 4222 jaume
    /*
88
     *  (non-Javadoc)
89
     * @see com.iver.cit.gvsig.fmap.drivers.WMSDriver#getCapabilities(java.net.URL)
90
     */
91 3774 jaume
        public void getCapabilities(URL server)
92 3746 jaume
                throws WMSException {
93 5409 jaume
//                try {
94
//                        client.connect();
95 6087 jaume
//                } catch (Exception e) {
96 5409 jaume
//                        throw new WMSException(e);
97
//                }
98 3746 jaume
        }
99
100 4222 jaume
        /*
101
         *  (non-Javadoc)
102
         * @see com.iver.cit.gvsig.fmap.drivers.WMSDriver#getMap(org.gvsig.remoteClient.wms.WMSStatus)
103 3746 jaume
         */
104 5409 jaume
    public File getMap(WMSStatus status, ICancellable cancel) throws WMSException {
105 6087 jaume
        try {
106
                        return client.getMap(status, cancel);
107 3746 jaume
        } catch (org.gvsig.remoteClient.exceptions.WMSException e) {
108
            throw new WMSException(e.getMessage());
109
        } catch (ServerErrorException e) {
110 6087 jaume
            throw new WMSException("WMS Unexpected server error."+e.getMessage());
111 3746 jaume
        }
112
    }
113 8765 jjdelcerro
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 6087 jaume
128 3746 jaume
        /**
129
         * Devuelve WMSClient a partir de su URL.
130
         *
131
         * @param url URL.
132
         *
133
         * @return WMSClient.
134 6087 jaume
         * @throws IOException
135
         * @throws ConnectException
136 3746 jaume
         *
137
         * @throws UnsupportedVersionException
138
         * @throws IOException
139
         */
140
141 6087 jaume
142 3746 jaume
    /**
143
     * Establishes the connection.
144 4503 jaume
     * @param override, if true the previous downloaded data will be overridden
145 3746 jaume
     * @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 5409 jaume
    public boolean connect(boolean override, ICancellable cancel) {
149 5539 jaume
            if (override) {
150
                    fmapRootLayer = null;
151
                    layers.clear();
152
            }
153 5409 jaume
                return client.connect(override, cancel);
154 4503 jaume
    }
155
156 5409 jaume
    public boolean connect(ICancellable cancel) {
157
            return client.connect(false, cancel);
158 3746 jaume
    }
159
160
    /**
161
     * @return the version of this client.
162
     */
163 6087 jaume
    public String getVersion() {
164 5277 jmvivo
            return client.getVersion();
165 3746 jaume
    }
166
167
    /**
168
     * @return The title of the service offered by the WMS server.
169
     */
170
    public String getServiceTitle() {
171 5277 jmvivo
                return client.getServiceInformation().title;
172 3746 jaume
    }
173
174
    /**
175 4222 jaume
     * 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 6087 jaume
     * by the hash is another string containing the corresponding Url
178 4222 jaume
     * @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 5277 jmvivo
194 4222 jaume
            return onlineResources;
195
    }
196
    /**
197 3746 jaume
     * @return <b>Vector</b> containing strings with the formats supported by this server.
198
     */
199
    public Vector getFormats() {
200 6087 jaume
201 5277 jmvivo
            return client.getFormats();
202 3746 jaume
    }
203
204
    /**
205 4222 jaume
     * @return <b>Boolean</b> returns if the WMS is queryable (suppports GetFeatureInfo)
206
     */
207
    public boolean isQueryable() {
208
        return client.isQueryable();
209
    }
210
    /**
211 8765 jjdelcerro
     * @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 6087 jaume
     * @return A tree containing the info of all layers available on this server.
218 3746 jaume
     */
219
    public WMSLayerNode getLayersTree() {
220
        if (fmapRootLayer == null){
221 5277 jmvivo
            WMSLayer clientRoot;
222 7388 jaume
            if (client.getLayersRoot() == null) {
223
                    client.connect(false, null);
224
            }
225 5277 jmvivo
            clientRoot = client.getLayersRoot();
226 6087 jaume
227 8765 jjdelcerro
228 3746 jaume
            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 6087 jaume
240 3746 jaume
        WMSLayerNode myNode = new WMSLayerNode();
241 6087 jaume
242 4503 jaume
        // Name
243 3746 jaume
        myNode.setName(node.getName());
244 6087 jaume
245 4503 jaume
        // Title
246 3746 jaume
        myNode.setTitle(node.getTitle());
247 6087 jaume
248 4503 jaume
        // Transparency
249 3746 jaume
        myNode.setTransparency(node.hasTransparency());
250 6087 jaume
251 4503 jaume
        // SRS
252 3746 jaume
        myNode.setSrs(node.getAllSrs());
253 6087 jaume
254
255 4503 jaume
        // Queryable
256 6087 jaume
257 3797 ldiaz
        myNode.setQueryable(node.isQueryable() && client.getServiceInformation().isQueryable());
258 6087 jaume
259 4503 jaume
        // Parent layer
260 3746 jaume
        myNode.setParent(parentNode);
261 6087 jaume
262 4503 jaume
        // Abstract
263 3746 jaume
        myNode.setAbstract(node.getAbstract());
264 6087 jaume
265 4503 jaume
        // Fixed Size
266 4222 jaume
        myNode.setFixedSize(node.getfixedWidth(), node.getfixedHeight());
267 6087 jaume
268 4503 jaume
        // LatLonBox
269 3746 jaume
        if (node.getLatLonBox()!=null)
270
            myNode.setLatLonBox(node.getLatLonBox().toString());
271 6087 jaume
272 5442 jaume
        // Keywords
273
        ArrayList keywords = node.getKeywords();
274
        for (int i = 0; i < keywords.size(); i++) {
275
                myNode.addKeyword((String) keywords.get(i));
276
                }
277 6087 jaume
278 4503 jaume
        // Styles
279 3746 jaume
        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 6087 jaume
285 4503 jaume
        // Dimensions
286 3746 jaume
        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 6087 jaume
292 4503 jaume
        // Childs
293 3746 jaume
        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 6087 jaume
299
300 3746 jaume
        if (myNode.getName()!=null)
301
            layers.put(myNode.getName(), myNode);
302 6087 jaume
303 3746 jaume
        return myNode;
304
    }
305
306
    /**
307
     * @return
308
     */
309
    public String getAbstract() {
310 6087 jaume
311 5277 jmvivo
            return client.getServiceInformation().abstr;
312 6087 jaume
313 3746 jaume
    }
314
315
    /**
316
     * @param layerName
317
     * @param srs
318
     * @return
319
     */
320
    public Rectangle2D getLayersExtent(String[] layerName, String srs) {
321 5277 jmvivo
            return client.getLayersExtent(layerName, srs);
322 3746 jaume
    }
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 6087 jaume
     * @throws WMSException
353 3746 jaume
     */
354 8765 jjdelcerro
    public String getFeatureInfo(WMSStatus _wmsStatus, int i, int j, int max_value, ICancellable cancellable) throws WMSException {
355 3746 jaume
        try {
356 8765 jjdelcerro
            return client.getFeatureInfo(_wmsStatus, i, j, max_value, cancellable);
357 3746 jaume
        } catch (org.gvsig.remoteClient.exceptions.WMSException e) {
358 6087 jaume
            throw new WMSException();
359 5277 jmvivo
                }
360 3746 jaume
    }
361 6087 jaume
362 3746 jaume
    public String getHost(){
363 5277 jmvivo
            return client.getHost();
364
365 3746 jaume
    }
366
}