Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extWMS / src / com / iver / cit / gvsig / fmap / drivers / wms / FMapWMSDriver.java @ 5442

History | View | Annotate | Download (9.32 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
    
76
    protected FMapWMSDriver(URL url) throws ConnectException, IOException {
77
            client = new WMSClient(url.toString());
78
    }
79
    
80
    public String[] getLayerNames(){
81
            return client.getLayerNames();
82
    }
83
    /*
84
     *  (non-Javadoc)
85
     * @see com.iver.cit.gvsig.fmap.drivers.WMSDriver#getCapabilities(java.net.URL)
86
     */
87
        public void getCapabilities(URL server)
88
                throws WMSException {
89
//                try {
90
//                        client.connect();
91
//                } catch (Exception e) {                        
92
//                        throw new WMSException(e);
93
//                }
94
        }
95

    
96
        /*
97
         *  (non-Javadoc)
98
         * @see com.iver.cit.gvsig.fmap.drivers.WMSDriver#getMap(org.gvsig.remoteClient.wms.WMSStatus)
99
         */
100
    public File getMap(WMSStatus status, ICancellable cancel) throws WMSException {
101
        try {                
102
                        return client.getMap(status, cancel);                
103
        } catch (org.gvsig.remoteClient.exceptions.WMSException e) {
104
            throw new WMSException(e.getMessage());
105
        } catch (ServerErrorException e) {
106
            throw new WMSException("WMS Unexpected server error."+e.getMessage());      
107
        }
108
    }
109
    
110
        /**
111
         * Devuelve WMSClient a partir de su URL.
112
         *
113
         * @param url URL.
114
         *
115
         * @return WMSClient.
116
         * @throws IOException 
117
         * @throws ConnectException 
118
         *
119
         * @throws UnsupportedVersionException
120
         * @throws IOException
121
         */
122
        
123

    
124
    /**
125
     * Establishes the connection.
126
     * @param override, if true the previous downloaded data will be overridden
127
     * @return <b>true</b> if the connection was successful, or <b>false</b> if it was no possible
128
     * to establish the connection for any reason such as version negotiation.
129
     */
130
    public boolean connect(boolean override, ICancellable cancel) {
131
                return client.connect(override, cancel);
132
    }
133

    
134
    public boolean connect(ICancellable cancel) {
135
            return client.connect(false, cancel);
136
    }
137

    
138
    /**
139
     * @return the version of this client.
140
     */
141
    public String getVersion() {        
142
            return client.getVersion();
143
    }
144

    
145
    /**
146
     * @return The title of the service offered by the WMS server.
147
     */
148
    public String getServiceTitle() {
149
                return client.getServiceInformation().title;
150
    }
151

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

    
172
            return onlineResources;
173
    }
174
    /**
175
     * @return <b>Vector</b> containing strings with the formats supported by this server.
176
     */
177
    public Vector getFormats() {
178
        
179
            return client.getFormats();
180
    }
181

    
182
    /**
183
     * @return <b>Boolean</b> returns if the WMS is queryable (suppports GetFeatureInfo)
184
     */
185
    public boolean isQueryable() {
186
        return client.isQueryable();
187
    }
188
    
189
    /**
190
     * @return A tree containing the info of all layers available on this server. 
191
     */
192
    public WMSLayerNode getLayersTree() {
193
        if (fmapRootLayer == null){
194
            WMSLayer clientRoot;
195

    
196
            clientRoot = client.getLayersRoot();
197
            
198
            fmapRootLayer = parseTree(clientRoot, null);
199
        }
200
        return fmapRootLayer;
201
    }
202

    
203
    /**
204
     * Parses the client's layer node and translates it to a fmap's layer node
205
     * @param WMSLayer
206
     * @return WMSLayerNode
207
     */
208
    private WMSLayerNode parseTree(WMSLayer node, WMSLayerNode parentNode) {
209
        
210
        WMSLayerNode myNode = new WMSLayerNode();
211
        
212
        // Name
213
        myNode.setName(node.getName());
214
        
215
        // Title
216
        myNode.setTitle(node.getTitle());
217
        
218
        // Transparency
219
        myNode.setTransparency(node.hasTransparency());
220
        
221
        // SRS
222
        myNode.setSrs(node.getAllSrs());
223
        
224
        
225
        // Queryable
226
        
227
        myNode.setQueryable(node.isQueryable() && client.getServiceInformation().isQueryable());
228
        
229
        // Parent layer
230
        myNode.setParent(parentNode);
231
        
232
        // Abstract
233
        myNode.setAbstract(node.getAbstract());
234
        
235
        // Fixed Size
236
        myNode.setFixedSize(node.getfixedWidth(), node.getfixedHeight());
237
        
238
        // LatLonBox
239
        if (node.getLatLonBox()!=null)
240
            myNode.setLatLonBox(node.getLatLonBox().toString());
241
        
242
        // Keywords
243
        ArrayList keywords = node.getKeywords();
244
        for (int i = 0; i < keywords.size(); i++) {
245
                myNode.addKeyword((String) keywords.get(i));
246
                }
247
        
248
        // Styles
249
        ArrayList styles = node.getStyles();
250
        for (int i = 0; i < styles.size(); i++) {
251
            WMSStyle style = (WMSStyle) styles.get(i);
252
            myNode.addStyle(style.getName(), style.getTitle(), style.getAbstract());
253
        }
254
        
255
        // Dimensions
256
        ArrayList dimensions = node.getDimensions();
257
        for (int i = 0; i < dimensions.size(); i++) {
258
            WMSDimension d = (WMSDimension) dimensions.get(i);
259
            myNode.addDimension(d.getName(), d.getUnits(), d.getUnitSymbol(), d.getDimensionExpression());
260
        }
261
        
262
        // Childs
263
        int children = node.getChildren().size();
264
        myNode.setChildren(new ArrayList());
265
        for (int i = 0; i < children; i++) {
266
            myNode.getChildren().add(parseTree((WMSLayer)node.getChildren().get(i), myNode));
267
        }
268
        
269
        
270
        if (myNode.getName()!=null)
271
            layers.put(myNode.getName(), myNode);
272
        
273
        return myNode;
274
    }
275

    
276
    /**
277
     * @return
278
     */
279
    public String getAbstract() {
280
        
281
            return client.getServiceInformation().abstr;
282
                
283
    }
284

    
285
    /**
286
     * @param layerName
287
     * @param srs
288
     * @return
289
     */
290
    public Rectangle2D getLayersExtent(String[] layerName, String srs) {
291
        
292
            return client.getLayersExtent(layerName, srs);
293
                
294
    }
295

    
296
    /**
297
     * @param string
298
     * @return
299
     */
300
    public WMSLayerNode getLayer(String layerName) {
301
        if (getLayers().get(layerName) != null)
302
        {
303
            return (WMSLayerNode)layers.get(layerName);
304
        }
305
        return null;
306
    }
307

    
308
    /**
309
     * @return
310
     */
311
    private TreeMap getLayers() {
312
        if (fmapRootLayer == null){
313
            fmapRootLayer = getLayersTree();
314
        }
315
        return layers;
316
    }
317

    
318
    /**
319
     * @param wmsStatus
320
     * @param i
321
     * @param j
322
     * @param max_value
323
     * @return
324
     * @throws WMSException 
325
     */
326
    public String getFeatureInfo(WMSStatus _wmsStatus, int i, int j, int max_value) throws WMSException {
327
        try {
328
            return client.getFeatureInfo(_wmsStatus, i, j, max_value);
329
        } catch (org.gvsig.remoteClient.exceptions.WMSException e) {
330
            throw new WMSException();                        
331
                }
332
    }
333
    
334
    public String getHost(){
335
            return client.getHost();
336

    
337
    }
338
}