Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_898 / libraries / libRemoteServices / src / org / gvsig / remoteClient / wms / WMSClient.java @ 10513

History | View | Annotate | Download (8.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.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, ICancellable cancel) throws WMSException{        
79
        return handler.getFeatureInfo(status, x, y, featureCount, cancel);
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
    public boolean hasLegendGraphic()
167
    {
168
            return handler.getServiceInformation().hasLegendGraphic();  
169
    }
170
    
171
    public void close() {        
172
        // your code here
173
    } 
174
    
175
    
176
    /**
177
     * Returns the max extent that envolves the requested layers
178
     * */
179
    public Rectangle2D getLayersExtent(String[]layerNames, String srs)
180
    {
181
        try
182
        {
183
                if (layerNames == null) return null;
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, ICancellable cancel) 
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());
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, cancel);
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(ICancellable cancel) {
275
                return connect(false, cancel);
276
        }
277
}