Statistics
| Revision:

root / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / wms / WMSClient.java @ 7945

History | View | Annotate | Download (7.92 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.util.TreeMap;
9
import java.util.Vector;
10

    
11
import org.gvsig.remoteClient.exceptions.ServerErrorException;
12
import org.gvsig.remoteClient.exceptions.WMSException;
13
import org.gvsig.remoteClient.utils.BoundaryBox;
14

    
15

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

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

    
270
        public boolean connect(ICancellable cancel) {
271
                return connect(false, cancel);
272
        }
273
}