Statistics
| Revision:

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

History | View | Annotate | Download (9.36 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
         * Devuelve WMSClient a partir de su URL.
116
         *
117
         * @param url URL.
118
         *
119
         * @return WMSClient.
120
         * @throws IOException
121
         * @throws ConnectException
122
         *
123
         * @throws UnsupportedVersionException
124
         * @throws IOException
125
         */
126

    
127

    
128
    /**
129
     * Establishes the connection.
130
     * @param override, if true the previous downloaded data will be overridden
131
     * @return <b>true</b> if the connection was successful, or <b>false</b> if it was no possible
132
     * to establish the connection for any reason such as version negotiation.
133
     */
134
    public boolean connect(boolean override, ICancellable cancel) {
135
            if (override) {
136
                    fmapRootLayer = null;
137
                    layers.clear();
138
            }
139
                return client.connect(override, cancel);
140
    }
141

    
142
    public boolean connect(ICancellable cancel) {
143
            return client.connect(false, cancel);
144
    }
145

    
146
    /**
147
     * @return the version of this client.
148
     */
149
    public String getVersion() {
150
            return client.getVersion();
151
    }
152

    
153
    /**
154
     * @return The title of the service offered by the WMS server.
155
     */
156
    public String getServiceTitle() {
157
                return client.getServiceInformation().title;
158
    }
159

    
160
    /**
161
     * Returns a Hash table containing the values for each online resource.
162
     * Using as key a String with name of the WMS request and the value returned
163
     * by the hash is another string containing the corresponding Url
164
     * @return HashTable
165
     */
166
    public Hashtable getOnlineResources() {
167
            Hashtable onlineResources = new Hashtable();
168
            ServiceInformation si = client.getServiceInformation();
169
            Iterator it = si.operations.keySet().iterator();
170
            while (it.hasNext()) {
171
                    String key = (String) it.next();
172
                    String val = (String) si.operations.get(key);
173
                    if (val==null && (si.online_resource!=null || si.online_resource!= ""))
174
                            val = si.online_resource;
175
                    if (val!=null) {
176
                            onlineResources.put(key, val);
177
                    }
178
            }
179

    
180
            return onlineResources;
181
    }
182
    /**
183
     * @return <b>Vector</b> containing strings with the formats supported by this server.
184
     */
185
    public Vector getFormats() {
186

    
187
            return client.getFormats();
188
    }
189

    
190
    /**
191
     * @return <b>Boolean</b> returns if the WMS is queryable (suppports GetFeatureInfo)
192
     */
193
    public boolean isQueryable() {
194
        return client.isQueryable();
195
    }
196

    
197
    /**
198
     * @return A tree containing the info of all layers available on this server.
199
     */
200
    public WMSLayerNode getLayersTree() {
201
        if (fmapRootLayer == null){
202
            WMSLayer clientRoot;
203
            if (client.getLayersRoot() == null) {
204
                    client.connect(false, null);
205
            }
206
            clientRoot = client.getLayersRoot();
207

    
208
            fmapRootLayer = parseTree(clientRoot, null);
209
        }
210
        return fmapRootLayer;
211
    }
212

    
213
    /**
214
     * Parses the client's layer node and translates it to a fmap's layer node
215
     * @param WMSLayer
216
     * @return WMSLayerNode
217
     */
218
    private WMSLayerNode parseTree(WMSLayer node, WMSLayerNode parentNode) {
219

    
220
        WMSLayerNode myNode = new WMSLayerNode();
221

    
222
        // Name
223
        myNode.setName(node.getName());
224

    
225
        // Title
226
        myNode.setTitle(node.getTitle());
227

    
228
        // Transparency
229
        myNode.setTransparency(node.hasTransparency());
230

    
231
        // SRS
232
        myNode.setSrs(node.getAllSrs());
233

    
234

    
235
        // Queryable
236

    
237
        myNode.setQueryable(node.isQueryable() && client.getServiceInformation().isQueryable());
238

    
239
        // Parent layer
240
        myNode.setParent(parentNode);
241

    
242
        // Abstract
243
        myNode.setAbstract(node.getAbstract());
244

    
245
        // Fixed Size
246
        myNode.setFixedSize(node.getfixedWidth(), node.getfixedHeight());
247

    
248
        // LatLonBox
249
        if (node.getLatLonBox()!=null)
250
            myNode.setLatLonBox(node.getLatLonBox().toString());
251

    
252
        // Keywords
253
        ArrayList keywords = node.getKeywords();
254
        for (int i = 0; i < keywords.size(); i++) {
255
                myNode.addKeyword((String) keywords.get(i));
256
                }
257

    
258
        // Styles
259
        ArrayList styles = node.getStyles();
260
        for (int i = 0; i < styles.size(); i++) {
261
            WMSStyle style = (WMSStyle) styles.get(i);
262
            myNode.addStyle(style.getName(), style.getTitle(), style.getAbstract());
263
        }
264

    
265
        // Dimensions
266
        ArrayList dimensions = node.getDimensions();
267
        for (int i = 0; i < dimensions.size(); i++) {
268
            WMSDimension d = (WMSDimension) dimensions.get(i);
269
            myNode.addDimension(d.getName(), d.getUnits(), d.getUnitSymbol(), d.getDimensionExpression());
270
        }
271

    
272
        // Childs
273
        int children = node.getChildren().size();
274
        myNode.setChildren(new ArrayList());
275
        for (int i = 0; i < children; i++) {
276
            myNode.getChildren().add(parseTree((WMSLayer)node.getChildren().get(i), myNode));
277
        }
278

    
279

    
280
        if (myNode.getName()!=null)
281
            layers.put(myNode.getName(), myNode);
282

    
283
        return myNode;
284
    }
285

    
286
    /**
287
     * @return
288
     */
289
    public String getAbstract() {
290

    
291
            return client.getServiceInformation().abstr;
292

    
293
    }
294

    
295
    /**
296
     * @param layerName
297
     * @param srs
298
     * @return
299
     */
300
    public Rectangle2D getLayersExtent(String[] layerName, String srs) {
301
            return client.getLayersExtent(layerName, srs);
302
    }
303

    
304
    /**
305
     * @param string
306
     * @return
307
     */
308
    public WMSLayerNode getLayer(String layerName) {
309
        if (getLayers().get(layerName) != null)
310
        {
311
            return (WMSLayerNode)layers.get(layerName);
312
        }
313
        return null;
314
    }
315

    
316
    /**
317
     * @return
318
     */
319
    private TreeMap getLayers() {
320
        if (fmapRootLayer == null){
321
            fmapRootLayer = getLayersTree();
322
        }
323
        return layers;
324
    }
325

    
326
    /**
327
     * @param wmsStatus
328
     * @param i
329
     * @param j
330
     * @param max_value
331
     * @return
332
     * @throws WMSException
333
     */
334
    public String getFeatureInfo(WMSStatus _wmsStatus, int i, int j, int max_value) throws WMSException {
335
        try {
336
            return client.getFeatureInfo(_wmsStatus, i, j, max_value);
337
        } catch (org.gvsig.remoteClient.exceptions.WMSException e) {
338
            throw new WMSException();
339
                }
340
    }
341

    
342
    public String getHost(){
343
            return client.getHost();
344

    
345
    }
346
}