Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libRemoteServices / src / org / gvsig / remoteclient / wms / WMSClient.java @ 38579

History | View | Annotate | Download (9.06 KB)

1

    
2
package org.gvsig.remoteclient.wms;
3

    
4
import java.awt.geom.Rectangle2D;
5
import java.io.File;
6
import java.io.IOException;
7
import java.net.ConnectException;
8
import java.net.URL;
9
import java.util.TreeMap;
10
import java.util.Vector;
11

    
12
import org.gvsig.compat.net.ICancellable;
13
import org.gvsig.remoteclient.exceptions.ServerErrorException;
14
import org.gvsig.remoteclient.exceptions.WMSException;
15
import org.gvsig.remoteclient.utils.BoundaryBox;
16

    
17

    
18
/**
19
 * <p>Represents the class the with the necessary logic to connect to a OGCWMS and interpretate the data </p>
20
 * 
21
 */
22
public class WMSClient extends org.gvsig.remoteclient.RasterClient {
23
    
24
    private org.gvsig.remoteclient.wms.WMSProtocolHandler handler;
25
//    private TreeMap layers = new TreeMap();
26
//    private WMSLayer rootLayer;
27
    
28
    /**
29
     * @return Returns the rootLayer.
30
     */
31
    public WMSLayer getRootLayer() {
32
        return handler.rootLayer;
33
    }
34

    
35
    /**
36
     * Constructor.
37
     * the parameter host, indicates the WMS host to connect.
38
     * */
39
    public WMSClient(String host) throws ConnectException, IOException 
40
    {
41
            setHost(host);
42
        try {                
43
                handler = WMSProtocolHandlerFactory.negotiate(host);
44
                handler.setHost(host);        
45
        } catch(ConnectException conE) {
46
                conE.printStackTrace();
47
                throw conE; 
48
        } catch(IOException ioE) {
49
                ioE.printStackTrace();
50
                throw ioE; 
51
        } catch(Exception e) {
52
                e.printStackTrace();               
53
        }
54
    }
55
    
56
    public String getVersion()
57
    {
58
        return handler.getVersion();
59
    }
60
    /**
61
     * <p>One of the three interfaces that OGC WMS defines. Request a map.</p> 
62
     * @throws ServerErrorException 
63
     */
64
    public File getMap(WMSStatus status, ICancellable cancel) throws WMSException, ServerErrorException{   
65
        return handler.getMap(status, cancel);
66
    } 
67
    
68
    /**
69
     * <p>Gets the GetMap URL. The final client should download the file</p> 
70
     * @throws ServerErrorException 
71
     */
72
    public URL getGetMapURL(WMSStatus status, ICancellable cancel) throws WMSException, ServerErrorException{   
73
       return handler.getMapURL(status, cancel);
74
    } 
75
    
76
    /**
77
     * Returns the exception message if the file is a XML instead of a image.
78
     * @param file
79
     * @return
80
     * @throws IOException 
81
     */
82
    public String getExceptionMessage(File file) throws IOException {
83
            return handler.getExceptionMessage(file);
84
    }
85
    
86
    /**
87
     * <p>One of the three interfaces defined by OGC WMS, it gets the service capabilities</p>
88
     * @param override, if true the previous downloaded data will be overridden
89
     */
90
    public void getCapabilities(WMSStatus status, boolean override, ICancellable cancel) {        
91
        handler.getCapabilities(status, override, cancel);
92
    } 
93
    
94
    /**
95
     * <p>One of the three interfaces defined by the OGC WMS, it gets the information about a feature requested</p>
96
     * @return 
97
     */
98
    public String getFeatureInfo(WMSStatus status, int x, int y, int featureCount, ICancellable cancel) throws WMSException{        
99
        return handler.getFeatureInfo(status, x, y, featureCount, cancel);
100
    } 
101
    
102
    /**
103
     * <p>One of the three interfaces defined by the OGC WMS, it gets legend of a layer</p>
104
     * @return 
105
     */
106
    public File getLegendGraphic(WMSStatus status, String layerName, ICancellable cancel) throws WMSException, ServerErrorException{        
107
        return handler.getLegendGraphic(status, layerName, cancel);
108
    } 
109
    
110
    /**
111
     * <p> Reads from the WMS Capabilities, the layers available in the service</p>
112
     * @return a TreeMap with the available layers in the WMS 
113
     */
114
    public TreeMap getLayers() {        
115
        return handler.layers;
116
    } 
117
    
118
    /**
119
     * <p>Reads from the WMS Capabilities the number if layers available in the service</p>
120
     * @return, number of layers available
121
     */
122
    public int getNumberOfLayers() {        
123
        if (handler.layers != null)
124
        {
125
            return handler.layers.size();
126
        }
127
        return 0;
128
    } 
129
    
130
    /**
131
     * <p>Gets the WMSLayer with this name</p>
132
     * 
133
     * @param _name, layer name
134
     * @return the layer with this name
135
     */
136
    public WMSLayer getLayer(String _name) {        
137
        if (handler.layers.get(_name) != null)
138
        {
139
            return (WMSLayer)handler.layers.get(_name);
140
        }
141
        
142
        return null;
143
    } 
144
    
145
    public String[] getLayerNames()
146
    {            
147
        WMSLayer[] lyrs;
148
        
149
        lyrs = (WMSLayer[])handler.layers.values().toArray(new WMSLayer[0]);
150
        
151
        String[] names = new String[lyrs.length];
152
        
153
        for(int i = 0; i<lyrs.length; i++)
154
        {
155
            names[i] = ((WMSLayer)lyrs[i]).getName();
156
        }
157
        return names;
158
    }
159
    
160
    public String[] getLayerTitles()
161
    {            
162
        WMSLayer[] lyrs;
163
        
164
        lyrs = (WMSLayer[])handler.layers.values().toArray(new WMSLayer[0]);
165
        
166
        String[] titles = new String[lyrs.length];
167
        
168
        for(int i = 0; i<lyrs.length; i++)
169
        {
170
            titles[i] = ((WMSLayer)lyrs[i]).getTitle();
171
        }
172
        return titles;
173
    }
174
    /**
175
     * <p>Gets the image formats available in the Service to retrieve the maps</p>
176
     * @return a vector with all the available formats
177
     */
178
    public Vector getFormats() {        
179
        return ((WMSServiceInformation)handler.getServiceInformation()).formats;         
180
    } 
181
    
182
    /**
183
     * <p>Gets the information by point formats available in the Service</p>
184
     * @return a vector with all the available formats
185
     */
186
    public Vector getInfoFormats() {        
187
        return ((WMSServiceInformation)handler.getServiceInformation()).infoformats;
188
    } 
189
    
190
    public boolean isQueryable()
191
    {
192
            return ((WMSServiceInformation)handler.getServiceInformation()).isQueryable();  
193
    }
194
    public boolean hasLegendGraphic()
195
    {
196
            return ((WMSServiceInformation)handler.getServiceInformation()).hasLegendGraphic();  
197
    }
198
    
199
    public void close() {        
200
        // your code here
201
    } 
202
    
203
    
204
    /**
205
     * Returns the max extent that envolves the requested layers
206
     * */
207
    public Rectangle2D getLayersExtent(String[]layerNames, String srs)
208
    {
209
        try
210
        {
211
                if (layerNames == null) return null;
212
            BoundaryBox bbox;
213
            WMSLayer layer = getLayer(layerNames[0]);
214
            
215
            bbox = layer.getBbox(srs);
216
            if (bbox == null) return null;
217
            double xmin = bbox.getXmin();
218
            double xmax = bbox.getXmax();
219
            double ymin = bbox.getYmin();
220
            double ymax = bbox.getYmax();
221
            
222
            for(int i=1; i<layerNames.length; i++)
223
            {
224
                layer = getLayer(layerNames[i]);
225
                bbox = layer.getBbox(srs);
226
                if (bbox == null) return null;
227
                if (bbox.getXmin() < xmin)
228
                {
229
                    xmin = bbox.getXmin();
230
                }
231
                if (bbox.getYmin() < ymin)
232
                {
233
                    ymin = bbox.getYmin();
234
                }
235
                if (bbox.getXmax() > xmax)
236
                {
237
                    xmax = bbox.getXmax();
238
                }
239
                if (bbox.getYmax() > ymax)
240
                {
241
                    ymax = bbox.getYmax();
242
                }
243
            }        
244
            
245
            Rectangle2D extent = new Rectangle2D.Double(xmin,ymin,Math.abs(xmax-xmin),Math.abs(ymax-ymin));
246
            return extent;
247
        }
248
        catch(Exception e)
249
        {
250
            e.printStackTrace();
251
            return null;
252
        }
253
    }
254
    
255
    
256
    /**
257
     * Gets the Service information included in the Capabilities
258
     * */    
259
    public WMSServiceInformation getServiceInformation()
260
    {
261
        return ((WMSServiceInformation)handler.getServiceInformation());
262
    }
263
    
264
    
265
    /**
266
     * <p>Checks the connection to de remote WMS and requests its capabilities.</p>
267
     * @param override, if true the previous downloaded data will be overridden
268
     */
269
    public boolean connect(boolean override, ICancellable cancel) 
270
    {
271
        try {            
272
            if (handler == null)
273
            {
274
                if (getHost().trim().length() > 0)
275
                {                                        
276
                    //TODO: Implement correctly the negotiate algorithm
277
                    handler = WMSProtocolHandlerFactory.negotiate(getHost());
278
                    //handler = new WMSProtocolHandler1_1_1();
279
                    handler.setHost(getHost());
280
                }
281
                else
282
                {
283
                    //must to specify host first!!!!
284
                    return false;
285
                }                
286
            }
287
            getCapabilities(null, override, cancel);
288
            return true;
289
            
290
        } catch (Exception e) {
291
            e.printStackTrace();
292
            return false;
293
        }
294
    }
295
    
296
    //TODO Check this out: Always 1 layer at first level...
297
    public WMSLayer getLayersRoot() {
298
        return handler.rootLayer;
299
    }
300

    
301
        public boolean connect(ICancellable cancel) {
302
                return connect(false, cancel);
303
        }
304
}