Statistics
| Revision:

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

History | View | Annotate | Download (9.2 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
        // Keywords
225
        myNode.setKeywords(node.getKeywords());
226
        
227
        // Queryable
228
        
229
        myNode.setQueryable(node.isQueryable() && client.getServiceInformation().isQueryable());
230
        
231
        // Parent layer
232
        myNode.setParent(parentNode);
233
        
234
        // Abstract
235
        myNode.setAbstract(node.getAbstract());
236
        
237
        // Fixed Size
238
        myNode.setFixedSize(node.getfixedWidth(), node.getfixedHeight());
239
        
240
        // LatLonBox
241
        if (node.getLatLonBox()!=null)
242
            myNode.setLatLonBox(node.getLatLonBox().toString());
243
        
244
        // Styles
245
        ArrayList styles = node.getStyles();
246
        for (int i = 0; i < styles.size(); i++) {
247
            WMSStyle style = (WMSStyle) styles.get(i);
248
            myNode.addStyle(style.getName(), style.getTitle(), style.getAbstract());
249
        }
250
        
251
        // Dimensions
252
        ArrayList dimensions = node.getDimensions();
253
        for (int i = 0; i < dimensions.size(); i++) {
254
            WMSDimension d = (WMSDimension) dimensions.get(i);
255
            myNode.addDimension(d.getName(), d.getUnits(), d.getUnitSymbol(), d.getDimensionExpression());
256
        }
257
        
258
        // Childs
259
        int children = node.getChildren().size();
260
        myNode.setChildren(new ArrayList());
261
        for (int i = 0; i < children; i++) {
262
            myNode.getChildren().add(parseTree((WMSLayer)node.getChildren().get(i), myNode));
263
        }
264
        
265
        
266
        if (myNode.getName()!=null)
267
            layers.put(myNode.getName(), myNode);
268
        
269
        return myNode;
270
    }
271

    
272
    /**
273
     * @return
274
     */
275
    public String getAbstract() {
276
        
277
            return client.getServiceInformation().abstr;
278
                
279
    }
280

    
281
    /**
282
     * @param layerName
283
     * @param srs
284
     * @return
285
     */
286
    public Rectangle2D getLayersExtent(String[] layerName, String srs) {
287
        
288
            return client.getLayersExtent(layerName, srs);
289
                
290
    }
291

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

    
304
    /**
305
     * @return
306
     */
307
    private TreeMap getLayers() {
308
        if (fmapRootLayer == null){
309
            fmapRootLayer = getLayersTree();
310
        }
311
        return layers;
312
    }
313

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

    
333
    }
334
}