svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / main / java / org / gvsig / remoteclient / wms / WMSClient.java @ 40559
History | View | Annotate | Download (10 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 3
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | 40435 | jjdelcerro | |
25 | package org.gvsig.remoteclient.wms; |
||
26 | |||
27 | import java.awt.geom.Rectangle2D; |
||
28 | import java.io.File; |
||
29 | import java.io.IOException; |
||
30 | import java.net.ConnectException; |
||
31 | import java.net.URL; |
||
32 | import java.util.TreeMap; |
||
33 | import java.util.Vector; |
||
34 | |||
35 | import org.gvsig.compat.net.ICancellable; |
||
36 | import org.gvsig.remoteclient.exceptions.ServerErrorException; |
||
37 | import org.gvsig.remoteclient.exceptions.WMSException; |
||
38 | import org.gvsig.remoteclient.utils.BoundaryBox; |
||
39 | |||
40 | |||
41 | /**
|
||
42 | * <p>Represents the class the with the necessary logic to connect to a OGCWMS and interpretate the data </p>
|
||
43 | *
|
||
44 | */
|
||
45 | public class WMSClient extends org.gvsig.remoteclient.RasterClient { |
||
46 | |||
47 | private org.gvsig.remoteclient.wms.WMSProtocolHandler handler;
|
||
48 | // private TreeMap layers = new TreeMap();
|
||
49 | // private WMSLayer rootLayer;
|
||
50 | |||
51 | /**
|
||
52 | * @return Returns the rootLayer.
|
||
53 | */
|
||
54 | public WMSLayer getRootLayer() {
|
||
55 | return handler.rootLayer;
|
||
56 | } |
||
57 | |||
58 | /**
|
||
59 | * Constructor.
|
||
60 | * the parameter host, indicates the WMS host to connect.
|
||
61 | * */
|
||
62 | public WMSClient(String host) throws ConnectException, IOException |
||
63 | { |
||
64 | setHost(host); |
||
65 | try {
|
||
66 | handler = WMSProtocolHandlerFactory.negotiate(host); |
||
67 | handler.setHost(host); |
||
68 | } catch(ConnectException conE) { |
||
69 | conE.printStackTrace(); |
||
70 | throw conE;
|
||
71 | } catch(IOException ioE) { |
||
72 | ioE.printStackTrace(); |
||
73 | throw ioE;
|
||
74 | } catch(Exception e) { |
||
75 | e.printStackTrace(); |
||
76 | } |
||
77 | } |
||
78 | |||
79 | public String getVersion() |
||
80 | { |
||
81 | return handler.getVersion();
|
||
82 | } |
||
83 | /**
|
||
84 | * <p>One of the three interfaces that OGC WMS defines. Request a map.</p>
|
||
85 | * @throws ServerErrorException
|
||
86 | */
|
||
87 | public File getMap(WMSStatus status, ICancellable cancel) throws WMSException, ServerErrorException{ |
||
88 | return handler.getMap(status, cancel);
|
||
89 | } |
||
90 | |||
91 | /**
|
||
92 | * <p>Gets the GetMap URL. The final client should download the file</p>
|
||
93 | * @throws ServerErrorException
|
||
94 | */
|
||
95 | public URL getGetMapURL(WMSStatus status, ICancellable cancel) throws WMSException, ServerErrorException{ |
||
96 | return handler.getMapURL(status, cancel);
|
||
97 | } |
||
98 | |||
99 | /**
|
||
100 | * Returns the exception message if the file is a XML instead of a image.
|
||
101 | * @param file
|
||
102 | * @return
|
||
103 | * @throws IOException
|
||
104 | */
|
||
105 | public String getExceptionMessage(File file) throws IOException { |
||
106 | return handler.getExceptionMessage(file);
|
||
107 | } |
||
108 | |||
109 | /**
|
||
110 | * <p>One of the three interfaces defined by OGC WMS, it gets the service capabilities</p>
|
||
111 | * @param override, if true the previous downloaded data will be overridden
|
||
112 | */
|
||
113 | public void getCapabilities(WMSStatus status, boolean override, ICancellable cancel) { |
||
114 | handler.getCapabilities(status, override, cancel); |
||
115 | } |
||
116 | |||
117 | /**
|
||
118 | * <p>One of the three interfaces defined by the OGC WMS, it gets the information about a feature requested</p>
|
||
119 | * @return
|
||
120 | */
|
||
121 | public String getFeatureInfo(WMSStatus status, int x, int y, int featureCount, ICancellable cancel) throws WMSException{ |
||
122 | return handler.getFeatureInfo(status, x, y, featureCount, cancel);
|
||
123 | } |
||
124 | |||
125 | /**
|
||
126 | * <p>One of the three interfaces defined by the OGC WMS, it gets legend of a layer</p>
|
||
127 | * @return
|
||
128 | */
|
||
129 | public File getLegendGraphic(WMSStatus status, String layerName, ICancellable cancel) throws WMSException, ServerErrorException{ |
||
130 | return handler.getLegendGraphic(status, layerName, cancel);
|
||
131 | } |
||
132 | |||
133 | /**
|
||
134 | * <p> Reads from the WMS Capabilities, the layers available in the service</p>
|
||
135 | * @return a TreeMap with the available layers in the WMS
|
||
136 | */
|
||
137 | public TreeMap getLayers() { |
||
138 | return handler.layers;
|
||
139 | } |
||
140 | |||
141 | /**
|
||
142 | * <p>Reads from the WMS Capabilities the number if layers available in the service</p>
|
||
143 | * @return, number of layers available
|
||
144 | */
|
||
145 | public int getNumberOfLayers() { |
||
146 | if (handler.layers != null) |
||
147 | { |
||
148 | return handler.layers.size();
|
||
149 | } |
||
150 | return 0; |
||
151 | } |
||
152 | |||
153 | /**
|
||
154 | * <p>Gets the WMSLayer with this name</p>
|
||
155 | *
|
||
156 | * @param _name, layer name
|
||
157 | * @return the layer with this name
|
||
158 | */
|
||
159 | public WMSLayer getLayer(String _name) { |
||
160 | if (handler.layers.get(_name) != null) |
||
161 | { |
||
162 | return (WMSLayer)handler.layers.get(_name);
|
||
163 | } |
||
164 | |||
165 | return null; |
||
166 | } |
||
167 | |||
168 | public String[] getLayerNames() |
||
169 | { |
||
170 | WMSLayer[] lyrs;
|
||
171 | |||
172 | lyrs = (WMSLayer[])handler.layers.values().toArray(new WMSLayer[0]); |
||
173 | |||
174 | String[] names = new String[lyrs.length]; |
||
175 | |||
176 | for(int i = 0; i<lyrs.length; i++) |
||
177 | { |
||
178 | names[i] = ((WMSLayer)lyrs[i]).getName(); |
||
179 | } |
||
180 | return names;
|
||
181 | } |
||
182 | |||
183 | public String[] getLayerTitles() |
||
184 | { |
||
185 | WMSLayer[] lyrs;
|
||
186 | |||
187 | lyrs = (WMSLayer[])handler.layers.values().toArray(new WMSLayer[0]); |
||
188 | |||
189 | String[] titles = new String[lyrs.length]; |
||
190 | |||
191 | for(int i = 0; i<lyrs.length; i++) |
||
192 | { |
||
193 | titles[i] = ((WMSLayer)lyrs[i]).getTitle(); |
||
194 | } |
||
195 | return titles;
|
||
196 | } |
||
197 | /**
|
||
198 | * <p>Gets the image formats available in the Service to retrieve the maps</p>
|
||
199 | * @return a vector with all the available formats
|
||
200 | */
|
||
201 | public Vector getFormats() { |
||
202 | return ((WMSServiceInformation)handler.getServiceInformation()).formats;
|
||
203 | } |
||
204 | |||
205 | /**
|
||
206 | * <p>Gets the information by point formats available in the Service</p>
|
||
207 | * @return a vector with all the available formats
|
||
208 | */
|
||
209 | public Vector getInfoFormats() { |
||
210 | return ((WMSServiceInformation)handler.getServiceInformation()).infoformats;
|
||
211 | } |
||
212 | |||
213 | public boolean isQueryable() |
||
214 | { |
||
215 | return ((WMSServiceInformation)handler.getServiceInformation()).isQueryable();
|
||
216 | } |
||
217 | public boolean hasLegendGraphic() |
||
218 | { |
||
219 | return ((WMSServiceInformation)handler.getServiceInformation()).hasLegendGraphic();
|
||
220 | } |
||
221 | |||
222 | public void close() { |
||
223 | // your code here
|
||
224 | } |
||
225 | |||
226 | |||
227 | /**
|
||
228 | * Returns the max extent that envolves the requested layers
|
||
229 | * */
|
||
230 | public Rectangle2D getLayersExtent(String[]layerNames, String srs) |
||
231 | { |
||
232 | try
|
||
233 | { |
||
234 | if (layerNames == null) return null; |
||
235 | BoundaryBox bbox; |
||
236 | WMSLayer layer = getLayer(layerNames[0]);
|
||
237 | |||
238 | bbox = layer.getBbox(srs); |
||
239 | if (bbox == null) return null; |
||
240 | double xmin = bbox.getXmin();
|
||
241 | double xmax = bbox.getXmax();
|
||
242 | double ymin = bbox.getYmin();
|
||
243 | double ymax = bbox.getYmax();
|
||
244 | |||
245 | for(int i=1; i<layerNames.length; i++) |
||
246 | { |
||
247 | layer = getLayer(layerNames[i]); |
||
248 | bbox = layer.getBbox(srs); |
||
249 | if (bbox == null) return null; |
||
250 | if (bbox.getXmin() < xmin)
|
||
251 | { |
||
252 | xmin = bbox.getXmin(); |
||
253 | } |
||
254 | if (bbox.getYmin() < ymin)
|
||
255 | { |
||
256 | ymin = bbox.getYmin(); |
||
257 | } |
||
258 | if (bbox.getXmax() > xmax)
|
||
259 | { |
||
260 | xmax = bbox.getXmax(); |
||
261 | } |
||
262 | if (bbox.getYmax() > ymax)
|
||
263 | { |
||
264 | ymax = bbox.getYmax(); |
||
265 | } |
||
266 | } |
||
267 | |||
268 | Rectangle2D extent = new Rectangle2D.Double(xmin,ymin,Math.abs(xmax-xmin),Math.abs(ymax-ymin)); |
||
269 | return extent;
|
||
270 | } |
||
271 | catch(Exception e) |
||
272 | { |
||
273 | e.printStackTrace(); |
||
274 | return null; |
||
275 | } |
||
276 | } |
||
277 | |||
278 | |||
279 | /**
|
||
280 | * Gets the Service information included in the Capabilities
|
||
281 | * */
|
||
282 | public WMSServiceInformation getServiceInformation()
|
||
283 | { |
||
284 | return ((WMSServiceInformation)handler.getServiceInformation());
|
||
285 | } |
||
286 | |||
287 | |||
288 | /**
|
||
289 | * <p>Checks the connection to de remote WMS and requests its capabilities.</p>
|
||
290 | * @param override, if true the previous downloaded data will be overridden
|
||
291 | */
|
||
292 | public boolean connect(boolean override, ICancellable cancel) |
||
293 | { |
||
294 | try {
|
||
295 | if (handler == null) |
||
296 | { |
||
297 | if (getHost().trim().length() > 0) |
||
298 | { |
||
299 | //TODO: Implement correctly the negotiate algorithm
|
||
300 | handler = WMSProtocolHandlerFactory.negotiate(getHost()); |
||
301 | //handler = new WMSProtocolHandler1_1_1();
|
||
302 | handler.setHost(getHost()); |
||
303 | } |
||
304 | else
|
||
305 | { |
||
306 | //must to specify host first!!!!
|
||
307 | return false; |
||
308 | } |
||
309 | } |
||
310 | getCapabilities(null, override, cancel);
|
||
311 | return true; |
||
312 | |||
313 | } catch (Exception e) { |
||
314 | e.printStackTrace(); |
||
315 | return false; |
||
316 | } |
||
317 | } |
||
318 | |||
319 | //TODO Check this out: Always 1 layer at first level...
|
||
320 | public WMSLayer getLayersRoot() {
|
||
321 | return handler.rootLayer;
|
||
322 | } |
||
323 | |||
324 | public boolean connect(ICancellable cancel) { |
||
325 | return connect(false, cancel); |
||
326 | } |
||
327 | } |