Statistics
| Revision:

root / branches / MULTITHREADING_DEVELOPMENT / libraries / libRemoteServices / src / org / gvsig / remoteClient / wms / WMSClient.java @ 5393

History | View | Annotate | Download (7.95 KB)

1

    
2
package org.gvsig.remoteClient.wms;
3

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

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

    
274
        public boolean connect() {
275
                return connect(false);
276
        }
277

    
278
        public void cancel(int opCode) {
279
                handler.cancel(opCode);
280
        }
281
}