Statistics
| Revision:

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

History | View | Annotate | Download (8.06 KB)

1 3323 ldiaz
2
package org.gvsig.remoteClient.wms;
3
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 3323 ldiaz
import java.util.TreeMap;
9
import java.util.Vector;
10 4222 jaume
11 3516 jaume
import org.gvsig.remoteClient.exceptions.ServerErrorException;
12 3377 ldiaz
import org.gvsig.remoteClient.exceptions.WMSException;
13
import org.gvsig.remoteClient.utils.BoundaryBox;
14 3323 ldiaz
15 3377 ldiaz
16 3323 ldiaz
/**
17 3345 ldiaz
 * <p>Represents the class the with the necessary logic to connect to a OGCWMS and interpretate the data </p>
18 3323 ldiaz
 *
19
 */
20
public class WMSClient extends org.gvsig.remoteClient.RasterClient {
21 5239 ldiaz
22 3323 ldiaz
    private org.gvsig.remoteClient.wms.WMSProtocolHandler handler;
23 5343 jaume
//    private TreeMap layers = new TreeMap();
24
//    private WMSLayer rootLayer;
25 3323 ldiaz
26 3345 ldiaz
    /**
27 3483 jaume
     * @return Returns the rootLayer.
28
     */
29
    public WMSLayer getRootLayer() {
30 5343 jaume
        return handler.rootLayer;
31 3483 jaume
    }
32
33
    /**
34 3345 ldiaz
     * Constructor.
35
     * the parameter host, indicates the WMS host to connect.
36
     * */
37 3803 ldiaz
    public WMSClient(String host) throws ConnectException, IOException
38 3483 jaume
    {
39 4409 jaume
            setHost(host);
40
        try {
41 5239 ldiaz
                handler = WMSProtocolHandlerFactory.negotiate(host);
42 3803 ldiaz
                handler.setHost(host);
43 4409 jaume
        } catch(ConnectException conE) {
44 3803 ldiaz
                conE.printStackTrace();
45
                throw conE;
46 4409 jaume
        } catch(IOException ioE) {
47 3803 ldiaz
                ioE.printStackTrace();
48
                throw ioE;
49 4409 jaume
        } catch(Exception e) {
50 3803 ldiaz
                e.printStackTrace();
51 3687 ldiaz
        }
52 3483 jaume
    }
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 3516 jaume
     * @throws ServerErrorException
61 3483 jaume
     */
62 5409 jaume
    public File getMap(WMSStatus status, ICancellable cancel) throws WMSException, ServerErrorException{
63
        return handler.getMap(status, cancel);
64 3323 ldiaz
    }
65 3483 jaume
66
    /**
67
     * <p>One of the three interfaces defined by OGC WMS, it gets the service capabilities</p>
68 4355 jaume
     * @param override, if true the previous downloaded data will be overridden
69 3483 jaume
     */
70 5409 jaume
    public void getCapabilities(WMSStatus status, boolean override, ICancellable cancel) {
71
        handler.getCapabilities(status, override, cancel);
72 3323 ldiaz
    }
73 3483 jaume
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 8765 jjdelcerro
    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 3323 ldiaz
    }
81 3483 jaume
82
    /**
83 8765 jjdelcerro
     * <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 3483 jaume
     * <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 3323 ldiaz
    public TreeMap getLayers() {
95 5343 jaume
        return handler.layers;
96 3323 ldiaz
    }
97 3483 jaume
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 3323 ldiaz
    public int getNumberOfLayers() {
103 5343 jaume
        if (handler.layers != null)
104 3483 jaume
        {
105 5343 jaume
            return handler.layers.size();
106 3483 jaume
        }
107
        return 0;
108 3323 ldiaz
    }
109 3483 jaume
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 3323 ldiaz
    public WMSLayer getLayer(String _name) {
117 5343 jaume
        if (handler.layers.get(_name) != null)
118 3483 jaume
        {
119 5343 jaume
            return (WMSLayer)handler.layers.get(_name);
120 3483 jaume
        }
121
122 3323 ldiaz
        return null;
123
    }
124 3483 jaume
125 3341 ldiaz
    public String[] getLayerNames()
126
    {
127 3483 jaume
        WMSLayer[] lyrs;
128
129 5343 jaume
        lyrs = (WMSLayer[])handler.layers.values().toArray(new WMSLayer[0]);
130 3483 jaume
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 3341 ldiaz
    }
139
140 5708 ldiaz
    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 3483 jaume
    /**
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 3323 ldiaz
    public Vector getFormats() {
159
        return handler.getServiceInformation().formats;
160
    }
161 3483 jaume
162 4222 jaume
    public boolean isQueryable()
163
    {
164
            return handler.getServiceInformation().isQueryable();
165
    }
166 8765 jjdelcerro
    public boolean hasLegendGraphic()
167
    {
168
            return handler.getServiceInformation().hasLegendGraphic();
169
    }
170 3483 jaume
171 3323 ldiaz
    public void close() {
172
        // your code here
173
    }
174 3377 ldiaz
175
176 3483 jaume
    /**
177
     * Returns the max extent that envolves the requested layers
178
     * */
179
    public Rectangle2D getLayersExtent(String[]layerNames, String srs)
180
    {
181
        try
182
        {
183 8765 jjdelcerro
                if (layerNames == null) return null;
184 3483 jaume
            BoundaryBox bbox;
185
            WMSLayer layer = getLayer(layerNames[0]);
186
187
            bbox = layer.getBbox(srs);
188 4222 jaume
            if (bbox == null) return null;
189 3483 jaume
            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 4222 jaume
                if (bbox == null) return null;
199 3483 jaume
                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 4222 jaume
217 3483 jaume
            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 4355 jaume
237
238 3483 jaume
    /**
239
     * <p>Checks the connection to de remote WMS and requests its capabilities.</p>
240 4355 jaume
     * @param override, if true the previous downloaded data will be overridden
241 3483 jaume
     */
242 5409 jaume
    public boolean connect(boolean override, ICancellable cancel)
243 3483 jaume
    {
244 3687 ldiaz
        try {
245 3483 jaume
            if (handler == null)
246
            {
247
                if (getHost().trim().length() > 0)
248
                {
249
                    //TODO: Implement correctly the negotiate algorithm
250 5239 ldiaz
                    handler = WMSProtocolHandlerFactory.negotiate(getHost());
251 3687 ldiaz
                    //handler = new WMSProtocolHandler1_1_1();
252 3483 jaume
                    handler.setHost(getHost());
253
                }
254
                else
255
                {
256
                    //must to specify host first!!!!
257
                    return false;
258 3687 ldiaz
                }
259 3483 jaume
            }
260 5409 jaume
            getCapabilities(null, override, cancel);
261 3483 jaume
            return true;
262
263
        } catch (Exception e) {
264
            e.printStackTrace();
265
            return false;
266
        }
267
    }
268
269 3377 ldiaz
    //TODO Check this out: Always 1 layer at first level...
270
    public WMSLayer getLayersRoot() {
271 5343 jaume
        return handler.rootLayer;
272 3377 ldiaz
    }
273 4355 jaume
274 5409 jaume
        public boolean connect(ICancellable cancel) {
275
                return connect(false, cancel);
276 4355 jaume
        }
277 3377 ldiaz
}