Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2020 / libraries / libRemoteServices / src / org / gvsig / remoteclient / wms / WMSClient.java @ 33910

History | View | Annotate | Download (8.18 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 3323 ldiaz
import java.util.TreeMap;
9
import java.util.Vector;
10 4222 jaume
11 33738 jpiera
import org.gvsig.compat.net.ICancellable;
12 29658 jpiera
import org.gvsig.remoteclient.exceptions.ServerErrorException;
13
import org.gvsig.remoteclient.exceptions.WMSException;
14
import org.gvsig.remoteclient.utils.BoundaryBox;
15 3323 ldiaz
16 3377 ldiaz
17 3323 ldiaz
/**
18 3345 ldiaz
 * <p>Represents the class the with the necessary logic to connect to a OGCWMS and interpretate the data </p>
19 3323 ldiaz
 *
20
 */
21 29658 jpiera
public class WMSClient extends org.gvsig.remoteclient.RasterClient {
22 5239 ldiaz
23 29658 jpiera
    private org.gvsig.remoteclient.wms.WMSProtocolHandler handler;
24 5343 jaume
//    private TreeMap layers = new TreeMap();
25
//    private WMSLayer rootLayer;
26 3323 ldiaz
27 3345 ldiaz
    /**
28 3483 jaume
     * @return Returns the rootLayer.
29
     */
30
    public WMSLayer getRootLayer() {
31 5343 jaume
        return handler.rootLayer;
32 3483 jaume
    }
33
34
    /**
35 3345 ldiaz
     * Constructor.
36
     * the parameter host, indicates the WMS host to connect.
37
     * */
38 3803 ldiaz
    public WMSClient(String host) throws ConnectException, IOException
39 3483 jaume
    {
40 4409 jaume
            setHost(host);
41
        try {
42 5239 ldiaz
                handler = WMSProtocolHandlerFactory.negotiate(host);
43 3803 ldiaz
                handler.setHost(host);
44 4409 jaume
        } catch(ConnectException conE) {
45 3803 ldiaz
                conE.printStackTrace();
46
                throw conE;
47 4409 jaume
        } catch(IOException ioE) {
48 3803 ldiaz
                ioE.printStackTrace();
49
                throw ioE;
50 4409 jaume
        } catch(Exception e) {
51 3803 ldiaz
                e.printStackTrace();
52 3687 ldiaz
        }
53 3483 jaume
    }
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 3516 jaume
     * @throws ServerErrorException
62 3483 jaume
     */
63 5409 jaume
    public File getMap(WMSStatus status, ICancellable cancel) throws WMSException, ServerErrorException{
64
        return handler.getMap(status, cancel);
65 3323 ldiaz
    }
66 3483 jaume
67
    /**
68
     * <p>One of the three interfaces defined by OGC WMS, it gets the service capabilities</p>
69 4355 jaume
     * @param override, if true the previous downloaded data will be overridden
70 3483 jaume
     */
71 5409 jaume
    public void getCapabilities(WMSStatus status, boolean override, ICancellable cancel) {
72
        handler.getCapabilities(status, override, cancel);
73 3323 ldiaz
    }
74 3483 jaume
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 8559 ldiaz
    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 3323 ldiaz
    }
82 3483 jaume
83
    /**
84 7835 ldiaz
     * <p>One of the three interfaces defined by the OGC WMS, it gets legend of a layer</p>
85
     * @return
86
     */
87 7945 ldiaz
    public File getLegendGraphic(WMSStatus status, String layerName, ICancellable cancel) throws WMSException, ServerErrorException{
88
        return handler.getLegendGraphic(status, layerName, cancel);
89 7835 ldiaz
    }
90
91
    /**
92 3483 jaume
     * <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 3323 ldiaz
    public TreeMap getLayers() {
96 5343 jaume
        return handler.layers;
97 3323 ldiaz
    }
98 3483 jaume
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 3323 ldiaz
    public int getNumberOfLayers() {
104 5343 jaume
        if (handler.layers != null)
105 3483 jaume
        {
106 5343 jaume
            return handler.layers.size();
107 3483 jaume
        }
108
        return 0;
109 3323 ldiaz
    }
110 3483 jaume
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 3323 ldiaz
    public WMSLayer getLayer(String _name) {
118 5343 jaume
        if (handler.layers.get(_name) != null)
119 3483 jaume
        {
120 5343 jaume
            return (WMSLayer)handler.layers.get(_name);
121 3483 jaume
        }
122
123 3323 ldiaz
        return null;
124
    }
125 3483 jaume
126 3341 ldiaz
    public String[] getLayerNames()
127
    {
128 3483 jaume
        WMSLayer[] lyrs;
129
130 5343 jaume
        lyrs = (WMSLayer[])handler.layers.values().toArray(new WMSLayer[0]);
131 3483 jaume
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 3341 ldiaz
    }
140
141 5708 ldiaz
    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 3483 jaume
    /**
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 3323 ldiaz
    public Vector getFormats() {
160 27881 jpiera
        return ((WMSServiceInformation)handler.getServiceInformation()).formats;
161 3323 ldiaz
    }
162 3483 jaume
163 4222 jaume
    public boolean isQueryable()
164
    {
165 27881 jpiera
            return ((WMSServiceInformation)handler.getServiceInformation()).isQueryable();
166 4222 jaume
    }
167 8290 ldiaz
    public boolean hasLegendGraphic()
168
    {
169 27881 jpiera
            return ((WMSServiceInformation)handler.getServiceInformation()).hasLegendGraphic();
170 8290 ldiaz
    }
171 3483 jaume
172 3323 ldiaz
    public void close() {
173
        // your code here
174
    }
175 3377 ldiaz
176
177 3483 jaume
    /**
178
     * Returns the max extent that envolves the requested layers
179
     * */
180
    public Rectangle2D getLayersExtent(String[]layerNames, String srs)
181
    {
182
        try
183
        {
184 7485 ldiaz
                if (layerNames == null) return null;
185 3483 jaume
            BoundaryBox bbox;
186
            WMSLayer layer = getLayer(layerNames[0]);
187
188
            bbox = layer.getBbox(srs);
189 4222 jaume
            if (bbox == null) return null;
190 3483 jaume
            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 4222 jaume
                if (bbox == null) return null;
200 3483 jaume
                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 4222 jaume
218 3483 jaume
            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 27881 jpiera
     * */
232
    public WMSServiceInformation getServiceInformation()
233 3483 jaume
    {
234 27881 jpiera
        return ((WMSServiceInformation)handler.getServiceInformation());
235 3483 jaume
    }
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
}