Statistics
| Revision:

svn-gvsig-desktop / branches / MULTITHREADING_DEVELOPMENT / extensions / extWMS / src / com / iver / cit / gvsig / fmap / drivers / wms / FMapWMSDriver.java @ 5273

History | View | Annotate | Download (9.62 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.taskplanning.retrieving.RetrieveEvent;
56
import org.gvsig.remoteClient.wms.WMSClient;
57
import org.gvsig.remoteClient.wms.WMSDimension;
58
import org.gvsig.remoteClient.wms.WMSEventListener;
59
import org.gvsig.remoteClient.wms.WMSLayer;
60
import org.gvsig.remoteClient.wms.WMSStatus;
61
import org.gvsig.remoteClient.wms.WMSStyle;
62
import org.gvsig.remoteClient.wms.WMSProtocolHandler.ServiceInformation;
63

    
64
import com.iver.cit.gvsig.fmap.drivers.UnsupportedVersionException;
65
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode;
66
/**
67
 * Driver WMS.
68
 *
69
 * @author Jaume Dominguez Faus
70
 */
71
public class FMapWMSDriver implements WMSEventListener {
72
        
73
        
74
        private WMSClient client;
75
    private WMSLayerNode fmapRootLayer;
76
    private TreeMap layers = new TreeMap();
77
    private Vector listeners = new Vector();
78
    
79
   
80
    public void newEvent(int idRequest, RetrieveEvent event) {
81
            int eventType = event.getType();
82
            if (idRequest == WMSOperations.GET_CAPABILITIES) {
83
                    
84
                    if (eventType == WMSOperations.REQUEST_FINISHED) {
85
                            WMSLayer clientRoot = client.getLayersRoot();
86
                            fmapRootLayer = parseTree(clientRoot, null);
87
                            
88
                    }
89
                    for (int i = 0; i < listeners.size(); i++) {
90
                            WMSOperations l = (WMSOperations) listeners.get(i);
91
                            l.getCapabilities(eventType, event.getMessage());
92
                    }
93
            } else if (idRequest == WMSOperations.GET_MAP) {
94
                    String fileName = event.getFileName();
95
                    System.out.println("GetMap al driver");
96
                    for (int i = 0; i < listeners.size(); i++) {
97
                            System.out.println("getMap enviat al listener: "+i);
98
                            WMSOperations l = (WMSOperations) listeners.get(i);
99
                            l.getMap(eventType, new File(fileName), event.getMessage());
100
                    }
101
            }
102
    }
103
    
104
        FMapWMSDriver(URL host) {
105
                try {
106
                        client = new WMSClient(host.toString(), this);
107
                } catch (ConnectException e) {
108
                        e.printStackTrace();
109
                } catch (IOException e) {
110
                        e.printStackTrace();
111
                }
112
        }
113

    
114
        /*
115
         *  (non-Javadoc)
116
         * @see com.iver.cit.gvsig.fmap.drivers.WMSDriver#getMap(org.gvsig.remoteClient.wms.WMSStatus)
117
         */
118
    public void getMap(WMSStatus status) throws WMSException {
119
        try {
120
            client.getMap(status);
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
     * 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) {
135
        return client.connect(override);
136
    }
137

    
138
    public boolean connect() {
139
        return client.connect(false);
140
    }
141

    
142
    /**
143
     * @return the version of this client.
144
     */
145
    public String getVersion() {
146
        return client.getVersion();
147
    }
148

    
149
    /**
150
     * @return The title of the service offered by the WMS server.
151
     */
152
    public String getServiceTitle() {
153
        return client.getServiceInformation().title;
154
    }
155

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

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

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

    
263
    /**
264
     * @return
265
     */
266
    public String getAbstract() {
267
        return client.getServiceInformation().abstr;
268
    }
269

    
270
    /**
271
     * @param layerName
272
     * @param srs
273
     * @return
274
     */
275
    public Rectangle2D getLayersExtent(String[] layerName, String srs) {
276
        return client.getLayersExtent(layerName, srs);
277
    }
278

    
279
    /**
280
     * @param string
281
     * @return
282
     */
283
    public WMSLayerNode getLayer(String layerName) {
284
        if (getLayers().get(layerName) != null)
285
        {
286
            return (WMSLayerNode)layers.get(layerName);
287
        }
288
        return null;
289
    }
290

    
291
    /**
292
     * @return
293
     */
294
    private TreeMap getLayers() {
295
        if (fmapRootLayer == null){
296
            fmapRootLayer = getLayersTree();
297
        }
298
        return layers;
299
    }
300

    
301
    /**
302
     * @param wmsStatus
303
     * @param i
304
     * @param j
305
     * @param max_value
306
     * @return
307
     * @throws WMSException 
308
     */
309
    public String getFeatureInfo(WMSStatus _wmsStatus, int i, int j, int max_value) throws WMSException {
310
        try {
311
            return client.getFeatureInfo(_wmsStatus, i, j, max_value);
312
        } catch (org.gvsig.remoteClient.exceptions.WMSException e) {
313
            throw new WMSException();
314
        }
315
    }
316
    
317
    public String getHost(){
318
        return client.getHost();
319
    }
320
    
321
    public void addListener(WMSOperations listener) {
322
            if (!listeners.contains(listener))
323
                    listeners.add(listener);
324
    }
325
    
326
        public void cancel(int opCode) {
327
                client.cancel(WMSOperations.GET_CAPABILITIES);
328
        }
329
    
330
}
331