Statistics
| Revision:

svn-gvsig-desktop / branches / MULTITHREADING_DEVELOPMENT / libraries / libRemoteServices / src / org / gvsig / remoteClient / wms / WMSClient.java @ 5231

History | View | Annotate | Download (7.9 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
    private org.gvsig.remoteClient.wms.WMSProtocolHandler handler;
22
    private TreeMap layers = new TreeMap();
23
    private WMSLayer rootLayer;
24
    
25
    /**
26
     * @return Returns the rootLayer.
27
     */
28
    public WMSLayer getRootLayer() {
29
        return rootLayer;
30
    }
31

    
32
    /**
33
     * @param rootLayer The rootLayer to set.
34
     */
35
    public void setRootLayer(WMSLayer rootLayer) {
36
        this.rootLayer = rootLayer;
37
    }
38

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

    
280
        public boolean connect() {
281
                return connect(false);
282
        }
283
}