Statistics
| Revision:

root / tags / v2_0_0_Build_2020 / libraries / libRemoteServices / src / org / gvsig / remoteclient / wms / WMSClient.java @ 33910

History | View | Annotate | Download (8.18 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.compat.net.ICancellable;
12
import org.gvsig.remoteclient.exceptions.ServerErrorException;
13
import org.gvsig.remoteclient.exceptions.WMSException;
14
import org.gvsig.remoteclient.utils.BoundaryBox;
15

    
16

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

    
34
    /**
35
     * Constructor.
36
     * the parameter host, indicates the WMS host to connect.
37
     * */
38
    public WMSClient(String host) throws ConnectException, IOException 
39
    {
40
            setHost(host);
41
        try {                
42
                handler = WMSProtocolHandlerFactory.negotiate(host);
43
                handler.setHost(host);        
44
        } catch(ConnectException conE) {
45
                conE.printStackTrace();
46
                throw conE; 
47
        } catch(IOException ioE) {
48
                ioE.printStackTrace();
49
                throw ioE; 
50
        } catch(Exception e) {
51
                e.printStackTrace();               
52
        }
53
    }
54
    
55
    public String getVersion()
56
    {
57
        return handler.getVersion();
58
    }
59
    /**
60
     * <p>One of the three interfaces that OGC WMS defines. Request a map.</p> 
61
     * @throws ServerErrorException 
62
     */
63
    public File getMap(WMSStatus status, ICancellable cancel) throws WMSException, ServerErrorException{   
64
        return handler.getMap(status, cancel);
65
    } 
66
    
67
    /**
68
     * <p>One of the three interfaces defined by OGC WMS, it gets the service capabilities</p>
69
     * @param override, if true the previous downloaded data will be overridden
70
     */
71
    public void getCapabilities(WMSStatus status, boolean override, ICancellable cancel) {        
72
        handler.getCapabilities(status, override, cancel);
73
    } 
74
    
75
    /**
76
     * <p>One of the three interfaces defined by the OGC WMS, it gets the information about a feature requested</p>
77
     * @return 
78
     */
79
    public String getFeatureInfo(WMSStatus status, int x, int y, int featureCount, ICancellable cancel) throws WMSException{        
80
        return handler.getFeatureInfo(status, x, y, featureCount, cancel);
81
    } 
82
    
83
    /**
84
     * <p>One of the three interfaces defined by the OGC WMS, it gets legend of a layer</p>
85
     * @return 
86
     */
87
    public File getLegendGraphic(WMSStatus status, String layerName, ICancellable cancel) throws WMSException, ServerErrorException{        
88
        return handler.getLegendGraphic(status, layerName, cancel);
89
    } 
90
    
91
    /**
92
     * <p> Reads from the WMS Capabilities, the layers available in the service</p>
93
     * @return a TreeMap with the available layers in the WMS 
94
     */
95
    public TreeMap getLayers() {        
96
        return handler.layers;
97
    } 
98
    
99
    /**
100
     * <p>Reads from the WMS Capabilities the number if layers available in the service</p>
101
     * @return, number of layers available
102
     */
103
    public int getNumberOfLayers() {        
104
        if (handler.layers != null)
105
        {
106
            return handler.layers.size();
107
        }
108
        return 0;
109
    } 
110
    
111
    /**
112
     * <p>Gets the WMSLayer with this name</p>
113
     * 
114
     * @param _name, layer name
115
     * @return the layer with this name
116
     */
117
    public WMSLayer getLayer(String _name) {        
118
        if (handler.layers.get(_name) != null)
119
        {
120
            return (WMSLayer)handler.layers.get(_name);
121
        }
122
        
123
        return null;
124
    } 
125
    
126
    public String[] getLayerNames()
127
    {            
128
        WMSLayer[] lyrs;
129
        
130
        lyrs = (WMSLayer[])handler.layers.values().toArray(new WMSLayer[0]);
131
        
132
        String[] names = new String[lyrs.length];
133
        
134
        for(int i = 0; i<lyrs.length; i++)
135
        {
136
            names[i] = ((WMSLayer)lyrs[i]).getName();
137
        }
138
        return names;
139
    }
140
    
141
    public String[] getLayerTitles()
142
    {            
143
        WMSLayer[] lyrs;
144
        
145
        lyrs = (WMSLayer[])handler.layers.values().toArray(new WMSLayer[0]);
146
        
147
        String[] titles = new String[lyrs.length];
148
        
149
        for(int i = 0; i<lyrs.length; i++)
150
        {
151
            titles[i] = ((WMSLayer)lyrs[i]).getTitle();
152
        }
153
        return titles;
154
    }
155
    /**
156
     * <p>Gets the image formats available in the Service to retrieve the maps</p>
157
     * @return a vector with all the available formats
158
     */
159
    public Vector getFormats() {        
160
        return ((WMSServiceInformation)handler.getServiceInformation()).formats;         
161
    } 
162
    
163
    public boolean isQueryable()
164
    {
165
            return ((WMSServiceInformation)handler.getServiceInformation()).isQueryable();  
166
    }
167
    public boolean hasLegendGraphic()
168
    {
169
            return ((WMSServiceInformation)handler.getServiceInformation()).hasLegendGraphic();  
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
                if (layerNames == null) return null;
185
            BoundaryBox bbox;
186
            WMSLayer layer = getLayer(layerNames[0]);
187
            
188
            bbox = layer.getBbox(srs);
189
            if (bbox == null) return null;
190
            double xmin = bbox.getXmin();
191
            double xmax = bbox.getXmax();
192
            double ymin = bbox.getYmin();
193
            double ymax = bbox.getYmax();
194
            
195
            for(int i=1; i<layerNames.length; i++)
196
            {
197
                layer = getLayer(layerNames[i]);
198
                bbox = layer.getBbox(srs);
199
                if (bbox == null) return null;
200
                if (bbox.getXmin() < xmin)
201
                {
202
                    xmin = bbox.getXmin();
203
                }
204
                if (bbox.getYmin() < ymin)
205
                {
206
                    ymin = bbox.getYmin();
207
                }
208
                if (bbox.getXmax() > xmax)
209
                {
210
                    xmax = bbox.getXmax();
211
                }
212
                if (bbox.getYmax() > ymax)
213
                {
214
                    ymax = bbox.getYmax();
215
                }
216
            }        
217
            
218
            Rectangle2D extent = new Rectangle2D.Double(xmin,ymin,Math.abs(xmax-xmin),Math.abs(ymax-ymin));
219
            return extent;
220
        }
221
        catch(Exception e)
222
        {
223
            e.printStackTrace();
224
            return null;
225
        }
226
    }
227
    
228
    
229
    /**
230
     * Gets the Service information included in the Capabilities
231
     * */    
232
    public WMSServiceInformation getServiceInformation()
233
    {
234
        return ((WMSServiceInformation)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
}