Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libRemoteServices / src / org / gvsig / remoteclient / wms / WMSClient.java @ 38579

History | View | Annotate | Download (9.06 KB)

1 3323 ldiaz
2 29658 jpiera
package org.gvsig.remoteclient.wms;
3 3323 ldiaz
4 3377 ldiaz
import java.awt.geom.Rectangle2D;
5 4222 jaume
import java.io.File;
6 3803 ldiaz
import java.io.IOException;
7
import java.net.ConnectException;
8 35758 nbrodin
import java.net.URL;
9 3323 ldiaz
import java.util.TreeMap;
10
import java.util.Vector;
11 4222 jaume
12 33738 jpiera
import org.gvsig.compat.net.ICancellable;
13 29658 jpiera
import org.gvsig.remoteclient.exceptions.ServerErrorException;
14
import org.gvsig.remoteclient.exceptions.WMSException;
15
import org.gvsig.remoteclient.utils.BoundaryBox;
16 3323 ldiaz
17 3377 ldiaz
18 3323 ldiaz
/**
19 3345 ldiaz
 * <p>Represents the class the with the necessary logic to connect to a OGCWMS and interpretate the data </p>
20 3323 ldiaz
 *
21
 */
22 29658 jpiera
public class WMSClient extends org.gvsig.remoteclient.RasterClient {
23 5239 ldiaz
24 29658 jpiera
    private org.gvsig.remoteclient.wms.WMSProtocolHandler handler;
25 5343 jaume
//    private TreeMap layers = new TreeMap();
26
//    private WMSLayer rootLayer;
27 3323 ldiaz
28 3345 ldiaz
    /**
29 3483 jaume
     * @return Returns the rootLayer.
30
     */
31
    public WMSLayer getRootLayer() {
32 5343 jaume
        return handler.rootLayer;
33 3483 jaume
    }
34
35
    /**
36 3345 ldiaz
     * Constructor.
37
     * the parameter host, indicates the WMS host to connect.
38
     * */
39 3803 ldiaz
    public WMSClient(String host) throws ConnectException, IOException
40 3483 jaume
    {
41 4409 jaume
            setHost(host);
42
        try {
43 5239 ldiaz
                handler = WMSProtocolHandlerFactory.negotiate(host);
44 3803 ldiaz
                handler.setHost(host);
45 4409 jaume
        } catch(ConnectException conE) {
46 3803 ldiaz
                conE.printStackTrace();
47
                throw conE;
48 4409 jaume
        } catch(IOException ioE) {
49 3803 ldiaz
                ioE.printStackTrace();
50
                throw ioE;
51 4409 jaume
        } catch(Exception e) {
52 3803 ldiaz
                e.printStackTrace();
53 3687 ldiaz
        }
54 3483 jaume
    }
55
56
    public String getVersion()
57
    {
58
        return handler.getVersion();
59
    }
60
    /**
61
     * <p>One of the three interfaces that OGC WMS defines. Request a map.</p>
62 3516 jaume
     * @throws ServerErrorException
63 3483 jaume
     */
64 5409 jaume
    public File getMap(WMSStatus status, ICancellable cancel) throws WMSException, ServerErrorException{
65
        return handler.getMap(status, cancel);
66 3323 ldiaz
    }
67 3483 jaume
68
    /**
69 35758 nbrodin
     * <p>Gets the GetMap URL. The final client should download the file</p>
70
     * @throws ServerErrorException
71
     */
72
    public URL getGetMapURL(WMSStatus status, ICancellable cancel) throws WMSException, ServerErrorException{
73 35928 nbrodin
       return handler.getMapURL(status, cancel);
74 35758 nbrodin
    }
75
76
    /**
77
     * Returns the exception message if the file is a XML instead of a image.
78
     * @param file
79
     * @return
80
     * @throws IOException
81
     */
82
    public String getExceptionMessage(File file) throws IOException {
83
            return handler.getExceptionMessage(file);
84
    }
85
86
    /**
87 3483 jaume
     * <p>One of the three interfaces defined by OGC WMS, it gets the service capabilities</p>
88 4355 jaume
     * @param override, if true the previous downloaded data will be overridden
89 3483 jaume
     */
90 5409 jaume
    public void getCapabilities(WMSStatus status, boolean override, ICancellable cancel) {
91
        handler.getCapabilities(status, override, cancel);
92 3323 ldiaz
    }
93 3483 jaume
94
    /**
95
     * <p>One of the three interfaces defined by the OGC WMS, it gets the information about a feature requested</p>
96
     * @return
97
     */
98 8559 ldiaz
    public String getFeatureInfo(WMSStatus status, int x, int y, int featureCount, ICancellable cancel) throws WMSException{
99
        return handler.getFeatureInfo(status, x, y, featureCount, cancel);
100 3323 ldiaz
    }
101 3483 jaume
102
    /**
103 7835 ldiaz
     * <p>One of the three interfaces defined by the OGC WMS, it gets legend of a layer</p>
104
     * @return
105
     */
106 7945 ldiaz
    public File getLegendGraphic(WMSStatus status, String layerName, ICancellable cancel) throws WMSException, ServerErrorException{
107
        return handler.getLegendGraphic(status, layerName, cancel);
108 7835 ldiaz
    }
109
110
    /**
111 3483 jaume
     * <p> Reads from the WMS Capabilities, the layers available in the service</p>
112
     * @return a TreeMap with the available layers in the WMS
113
     */
114 3323 ldiaz
    public TreeMap getLayers() {
115 5343 jaume
        return handler.layers;
116 3323 ldiaz
    }
117 3483 jaume
118
    /**
119
     * <p>Reads from the WMS Capabilities the number if layers available in the service</p>
120
     * @return, number of layers available
121
     */
122 3323 ldiaz
    public int getNumberOfLayers() {
123 5343 jaume
        if (handler.layers != null)
124 3483 jaume
        {
125 5343 jaume
            return handler.layers.size();
126 3483 jaume
        }
127
        return 0;
128 3323 ldiaz
    }
129 3483 jaume
130
    /**
131
     * <p>Gets the WMSLayer with this name</p>
132
     *
133
     * @param _name, layer name
134
     * @return the layer with this name
135
     */
136 3323 ldiaz
    public WMSLayer getLayer(String _name) {
137 5343 jaume
        if (handler.layers.get(_name) != null)
138 3483 jaume
        {
139 5343 jaume
            return (WMSLayer)handler.layers.get(_name);
140 3483 jaume
        }
141
142 3323 ldiaz
        return null;
143
    }
144 3483 jaume
145 3341 ldiaz
    public String[] getLayerNames()
146
    {
147 3483 jaume
        WMSLayer[] lyrs;
148
149 5343 jaume
        lyrs = (WMSLayer[])handler.layers.values().toArray(new WMSLayer[0]);
150 3483 jaume
151
        String[] names = new String[lyrs.length];
152
153
        for(int i = 0; i<lyrs.length; i++)
154
        {
155
            names[i] = ((WMSLayer)lyrs[i]).getName();
156
        }
157
        return names;
158 3341 ldiaz
    }
159
160 5708 ldiaz
    public String[] getLayerTitles()
161
    {
162
        WMSLayer[] lyrs;
163
164
        lyrs = (WMSLayer[])handler.layers.values().toArray(new WMSLayer[0]);
165
166
        String[] titles = new String[lyrs.length];
167
168
        for(int i = 0; i<lyrs.length; i++)
169
        {
170
            titles[i] = ((WMSLayer)lyrs[i]).getTitle();
171
        }
172
        return titles;
173
    }
174 3483 jaume
    /**
175
     * <p>Gets the image formats available in the Service to retrieve the maps</p>
176
     * @return a vector with all the available formats
177
     */
178 3323 ldiaz
    public Vector getFormats() {
179 27881 jpiera
        return ((WMSServiceInformation)handler.getServiceInformation()).formats;
180 3323 ldiaz
    }
181 3483 jaume
182 38579 nbrodin
    /**
183
     * <p>Gets the information by point formats available in the Service</p>
184
     * @return a vector with all the available formats
185
     */
186
    public Vector getInfoFormats() {
187
        return ((WMSServiceInformation)handler.getServiceInformation()).infoformats;
188
    }
189
190 4222 jaume
    public boolean isQueryable()
191
    {
192 27881 jpiera
            return ((WMSServiceInformation)handler.getServiceInformation()).isQueryable();
193 4222 jaume
    }
194 8290 ldiaz
    public boolean hasLegendGraphic()
195
    {
196 27881 jpiera
            return ((WMSServiceInformation)handler.getServiceInformation()).hasLegendGraphic();
197 8290 ldiaz
    }
198 3483 jaume
199 3323 ldiaz
    public void close() {
200
        // your code here
201
    }
202 3377 ldiaz
203
204 3483 jaume
    /**
205
     * Returns the max extent that envolves the requested layers
206
     * */
207
    public Rectangle2D getLayersExtent(String[]layerNames, String srs)
208
    {
209
        try
210
        {
211 7485 ldiaz
                if (layerNames == null) return null;
212 3483 jaume
            BoundaryBox bbox;
213
            WMSLayer layer = getLayer(layerNames[0]);
214
215
            bbox = layer.getBbox(srs);
216 4222 jaume
            if (bbox == null) return null;
217 3483 jaume
            double xmin = bbox.getXmin();
218
            double xmax = bbox.getXmax();
219
            double ymin = bbox.getYmin();
220
            double ymax = bbox.getYmax();
221
222
            for(int i=1; i<layerNames.length; i++)
223
            {
224
                layer = getLayer(layerNames[i]);
225
                bbox = layer.getBbox(srs);
226 4222 jaume
                if (bbox == null) return null;
227 3483 jaume
                if (bbox.getXmin() < xmin)
228
                {
229
                    xmin = bbox.getXmin();
230
                }
231
                if (bbox.getYmin() < ymin)
232
                {
233
                    ymin = bbox.getYmin();
234
                }
235
                if (bbox.getXmax() > xmax)
236
                {
237
                    xmax = bbox.getXmax();
238
                }
239
                if (bbox.getYmax() > ymax)
240
                {
241
                    ymax = bbox.getYmax();
242
                }
243
            }
244 4222 jaume
245 3483 jaume
            Rectangle2D extent = new Rectangle2D.Double(xmin,ymin,Math.abs(xmax-xmin),Math.abs(ymax-ymin));
246
            return extent;
247
        }
248
        catch(Exception e)
249
        {
250
            e.printStackTrace();
251
            return null;
252
        }
253
    }
254
255
256
    /**
257
     * Gets the Service information included in the Capabilities
258 27881 jpiera
     * */
259
    public WMSServiceInformation getServiceInformation()
260 3483 jaume
    {
261 27881 jpiera
        return ((WMSServiceInformation)handler.getServiceInformation());
262 3483 jaume
    }
263 4355 jaume
264
265 3483 jaume
    /**
266
     * <p>Checks the connection to de remote WMS and requests its capabilities.</p>
267 4355 jaume
     * @param override, if true the previous downloaded data will be overridden
268 3483 jaume
     */
269 5409 jaume
    public boolean connect(boolean override, ICancellable cancel)
270 3483 jaume
    {
271 3687 ldiaz
        try {
272 3483 jaume
            if (handler == null)
273
            {
274
                if (getHost().trim().length() > 0)
275
                {
276
                    //TODO: Implement correctly the negotiate algorithm
277 5239 ldiaz
                    handler = WMSProtocolHandlerFactory.negotiate(getHost());
278 3687 ldiaz
                    //handler = new WMSProtocolHandler1_1_1();
279 3483 jaume
                    handler.setHost(getHost());
280
                }
281
                else
282
                {
283
                    //must to specify host first!!!!
284
                    return false;
285 3687 ldiaz
                }
286 3483 jaume
            }
287 5409 jaume
            getCapabilities(null, override, cancel);
288 3483 jaume
            return true;
289
290
        } catch (Exception e) {
291
            e.printStackTrace();
292
            return false;
293
        }
294
    }
295
296 3377 ldiaz
    //TODO Check this out: Always 1 layer at first level...
297
    public WMSLayer getLayersRoot() {
298 5343 jaume
        return handler.rootLayer;
299 3377 ldiaz
    }
300 4355 jaume
301 5409 jaume
        public boolean connect(ICancellable cancel) {
302
                return connect(false, cancel);
303 4355 jaume
        }
304 3377 ldiaz
}