svn-gvsig-desktop / tags / v10_RC2c / extensions / extWMS / src / com / iver / cit / gvsig / fmap / drivers / wms / FMapWMSDriver.java @ 8745
History | View | Annotate | Download (9.36 KB)
1 | 4503 | jaume | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | 3746 | jaume | *
|
3 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | 4503 | jaume | * Av. Blasco Ib??ez, 50
|
24 | 3746 | jaume | * 46010 VALENCIA
|
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | package com.iver.cit.gvsig.fmap.drivers.wms; |
||
42 | |||
43 | import java.awt.geom.Rectangle2D; |
||
44 | 4222 | jaume | import java.io.File; |
45 | 3746 | jaume | import java.io.IOException; |
46 | 3804 | ldiaz | import java.net.ConnectException; |
47 | 3746 | jaume | import java.net.URL; |
48 | import java.util.ArrayList; |
||
49 | 4222 | jaume | import java.util.Hashtable; |
50 | import java.util.Iterator; |
||
51 | 3746 | jaume | import java.util.TreeMap; |
52 | import java.util.Vector; |
||
53 | |||
54 | import org.gvsig.remoteClient.exceptions.ServerErrorException; |
||
55 | 5409 | jaume | import org.gvsig.remoteClient.wms.ICancellable; |
56 | 3746 | jaume | import org.gvsig.remoteClient.wms.WMSClient; |
57 | import org.gvsig.remoteClient.wms.WMSDimension; |
||
58 | import org.gvsig.remoteClient.wms.WMSLayer; |
||
59 | import org.gvsig.remoteClient.wms.WMSStatus; |
||
60 | import org.gvsig.remoteClient.wms.WMSStyle; |
||
61 | 4222 | jaume | import org.gvsig.remoteClient.wms.WMSProtocolHandler.ServiceInformation; |
62 | 3746 | jaume | |
63 | import com.iver.cit.gvsig.fmap.layers.WMSLayerNode; |
||
64 | /**
|
||
65 | * Driver WMS.
|
||
66 | *
|
||
67 | 4503 | jaume | * @author Jaume Dominguez Faus
|
68 | 3746 | jaume | */
|
69 | 5409 | jaume | public class FMapWMSDriver { |
70 | 3746 | jaume | private WMSClient client;
|
71 | private WMSLayerNode fmapRootLayer;
|
||
72 | private TreeMap layers = new TreeMap(); |
||
73 | 5277 | jmvivo | private URL url; |
74 | 6087 | jaume | |
75 | private FMapWMSDriver() {}
|
||
76 | |||
77 | 5277 | jmvivo | protected FMapWMSDriver(URL url) throws ConnectException, IOException { |
78 | client = new WMSClient(url.toString());
|
||
79 | } |
||
80 | 6087 | jaume | |
81 | 5435 | ldiaz | public String[] getLayerNames(){ |
82 | return client.getLayerNames();
|
||
83 | } |
||
84 | 5709 | ldiaz | public String[] getLayerTitles(){ |
85 | return client.getLayerTitles();
|
||
86 | } |
||
87 | 4222 | jaume | /*
|
88 | * (non-Javadoc)
|
||
89 | * @see com.iver.cit.gvsig.fmap.drivers.WMSDriver#getCapabilities(java.net.URL)
|
||
90 | */
|
||
91 | 3774 | jaume | public void getCapabilities(URL server) |
92 | 3746 | jaume | throws WMSException {
|
93 | 5409 | jaume | // try {
|
94 | // client.connect();
|
||
95 | 6087 | jaume | // } catch (Exception e) {
|
96 | 5409 | jaume | // throw new WMSException(e);
|
97 | // }
|
||
98 | 3746 | jaume | } |
99 | |||
100 | 4222 | jaume | /*
|
101 | * (non-Javadoc)
|
||
102 | * @see com.iver.cit.gvsig.fmap.drivers.WMSDriver#getMap(org.gvsig.remoteClient.wms.WMSStatus)
|
||
103 | 3746 | jaume | */
|
104 | 5409 | jaume | public File getMap(WMSStatus status, ICancellable cancel) throws WMSException { |
105 | 6087 | jaume | try {
|
106 | return client.getMap(status, cancel);
|
||
107 | 3746 | jaume | } catch (org.gvsig.remoteClient.exceptions.WMSException e) {
|
108 | throw new WMSException(e.getMessage()); |
||
109 | } catch (ServerErrorException e) {
|
||
110 | 6087 | jaume | throw new WMSException("WMS Unexpected server error."+e.getMessage()); |
111 | 3746 | jaume | } |
112 | } |
||
113 | 6087 | jaume | |
114 | 3746 | jaume | /**
|
115 | * Devuelve WMSClient a partir de su URL.
|
||
116 | *
|
||
117 | * @param url URL.
|
||
118 | *
|
||
119 | * @return WMSClient.
|
||
120 | 6087 | jaume | * @throws IOException
|
121 | * @throws ConnectException
|
||
122 | 3746 | jaume | *
|
123 | * @throws UnsupportedVersionException
|
||
124 | * @throws IOException
|
||
125 | */
|
||
126 | |||
127 | 6087 | jaume | |
128 | 3746 | jaume | /**
|
129 | * Establishes the connection.
|
||
130 | 4503 | jaume | * @param override, if true the previous downloaded data will be overridden
|
131 | 3746 | jaume | * @return <b>true</b> if the connection was successful, or <b>false</b> if it was no possible
|
132 | * to establish the connection for any reason such as version negotiation.
|
||
133 | */
|
||
134 | 5409 | jaume | public boolean connect(boolean override, ICancellable cancel) { |
135 | 5539 | jaume | if (override) {
|
136 | fmapRootLayer = null;
|
||
137 | layers.clear(); |
||
138 | } |
||
139 | 5409 | jaume | return client.connect(override, cancel);
|
140 | 4503 | jaume | } |
141 | |||
142 | 5409 | jaume | public boolean connect(ICancellable cancel) { |
143 | return client.connect(false, cancel); |
||
144 | 3746 | jaume | } |
145 | |||
146 | /**
|
||
147 | * @return the version of this client.
|
||
148 | */
|
||
149 | 6087 | jaume | public String getVersion() { |
150 | 5277 | jmvivo | return client.getVersion();
|
151 | 3746 | jaume | } |
152 | |||
153 | /**
|
||
154 | * @return The title of the service offered by the WMS server.
|
||
155 | */
|
||
156 | public String getServiceTitle() { |
||
157 | 5277 | jmvivo | return client.getServiceInformation().title;
|
158 | 3746 | jaume | } |
159 | |||
160 | /**
|
||
161 | 4222 | jaume | * Returns a Hash table containing the values for each online resource.
|
162 | * Using as key a String with name of the WMS request and the value returned
|
||
163 | 6087 | jaume | * by the hash is another string containing the corresponding Url
|
164 | 4222 | jaume | * @return HashTable
|
165 | */
|
||
166 | public Hashtable getOnlineResources() { |
||
167 | Hashtable onlineResources = new Hashtable(); |
||
168 | ServiceInformation si = client.getServiceInformation(); |
||
169 | Iterator it = si.operations.keySet().iterator();
|
||
170 | while (it.hasNext()) {
|
||
171 | String key = (String) it.next(); |
||
172 | String val = (String) si.operations.get(key); |
||
173 | if (val==null && (si.online_resource!=null || si.online_resource!= "")) |
||
174 | val = si.online_resource; |
||
175 | if (val!=null) { |
||
176 | onlineResources.put(key, val); |
||
177 | } |
||
178 | } |
||
179 | 5277 | jmvivo | |
180 | 4222 | jaume | return onlineResources;
|
181 | } |
||
182 | /**
|
||
183 | 3746 | jaume | * @return <b>Vector</b> containing strings with the formats supported by this server.
|
184 | */
|
||
185 | public Vector getFormats() { |
||
186 | 6087 | jaume | |
187 | 5277 | jmvivo | return client.getFormats();
|
188 | 3746 | jaume | } |
189 | |||
190 | /**
|
||
191 | 4222 | jaume | * @return <b>Boolean</b> returns if the WMS is queryable (suppports GetFeatureInfo)
|
192 | */
|
||
193 | public boolean isQueryable() { |
||
194 | return client.isQueryable();
|
||
195 | } |
||
196 | 6087 | jaume | |
197 | 4222 | jaume | /**
|
198 | 6087 | jaume | * @return A tree containing the info of all layers available on this server.
|
199 | 3746 | jaume | */
|
200 | public WMSLayerNode getLayersTree() {
|
||
201 | if (fmapRootLayer == null){ |
||
202 | 5277 | jmvivo | WMSLayer clientRoot; |
203 | 7388 | jaume | if (client.getLayersRoot() == null) { |
204 | client.connect(false, null); |
||
205 | } |
||
206 | 5277 | jmvivo | clientRoot = client.getLayersRoot(); |
207 | 6087 | jaume | |
208 | 3746 | jaume | fmapRootLayer = parseTree(clientRoot, null);
|
209 | } |
||
210 | return fmapRootLayer;
|
||
211 | } |
||
212 | |||
213 | /**
|
||
214 | * Parses the client's layer node and translates it to a fmap's layer node
|
||
215 | * @param WMSLayer
|
||
216 | * @return WMSLayerNode
|
||
217 | */
|
||
218 | private WMSLayerNode parseTree(WMSLayer node, WMSLayerNode parentNode) {
|
||
219 | 6087 | jaume | |
220 | 3746 | jaume | WMSLayerNode myNode = new WMSLayerNode();
|
221 | 6087 | jaume | |
222 | 4503 | jaume | // Name
|
223 | 3746 | jaume | myNode.setName(node.getName()); |
224 | 6087 | jaume | |
225 | 4503 | jaume | // Title
|
226 | 3746 | jaume | myNode.setTitle(node.getTitle()); |
227 | 6087 | jaume | |
228 | 4503 | jaume | // Transparency
|
229 | 3746 | jaume | myNode.setTransparency(node.hasTransparency()); |
230 | 6087 | jaume | |
231 | 4503 | jaume | // SRS
|
232 | 3746 | jaume | myNode.setSrs(node.getAllSrs()); |
233 | 6087 | jaume | |
234 | |||
235 | 4503 | jaume | // Queryable
|
236 | 6087 | jaume | |
237 | 3797 | ldiaz | myNode.setQueryable(node.isQueryable() && client.getServiceInformation().isQueryable()); |
238 | 6087 | jaume | |
239 | 4503 | jaume | // Parent layer
|
240 | 3746 | jaume | myNode.setParent(parentNode); |
241 | 6087 | jaume | |
242 | 4503 | jaume | // Abstract
|
243 | 3746 | jaume | myNode.setAbstract(node.getAbstract()); |
244 | 6087 | jaume | |
245 | 4503 | jaume | // Fixed Size
|
246 | 4222 | jaume | myNode.setFixedSize(node.getfixedWidth(), node.getfixedHeight()); |
247 | 6087 | jaume | |
248 | 4503 | jaume | // LatLonBox
|
249 | 3746 | jaume | if (node.getLatLonBox()!=null) |
250 | myNode.setLatLonBox(node.getLatLonBox().toString()); |
||
251 | 6087 | jaume | |
252 | 5442 | jaume | // Keywords
|
253 | ArrayList keywords = node.getKeywords();
|
||
254 | for (int i = 0; i < keywords.size(); i++) { |
||
255 | myNode.addKeyword((String) keywords.get(i));
|
||
256 | } |
||
257 | 6087 | jaume | |
258 | 4503 | jaume | // Styles
|
259 | 3746 | jaume | ArrayList styles = node.getStyles();
|
260 | for (int i = 0; i < styles.size(); i++) { |
||
261 | WMSStyle style = (WMSStyle) styles.get(i); |
||
262 | myNode.addStyle(style.getName(), style.getTitle(), style.getAbstract()); |
||
263 | } |
||
264 | 6087 | jaume | |
265 | 4503 | jaume | // Dimensions
|
266 | 3746 | jaume | ArrayList dimensions = node.getDimensions();
|
267 | for (int i = 0; i < dimensions.size(); i++) { |
||
268 | WMSDimension d = (WMSDimension) dimensions.get(i); |
||
269 | myNode.addDimension(d.getName(), d.getUnits(), d.getUnitSymbol(), d.getDimensionExpression()); |
||
270 | } |
||
271 | 6087 | jaume | |
272 | 4503 | jaume | // Childs
|
273 | 3746 | jaume | int children = node.getChildren().size();
|
274 | myNode.setChildren(new ArrayList()); |
||
275 | for (int i = 0; i < children; i++) { |
||
276 | myNode.getChildren().add(parseTree((WMSLayer)node.getChildren().get(i), myNode)); |
||
277 | } |
||
278 | 6087 | jaume | |
279 | |||
280 | 3746 | jaume | if (myNode.getName()!=null) |
281 | layers.put(myNode.getName(), myNode); |
||
282 | 6087 | jaume | |
283 | 3746 | jaume | return myNode;
|
284 | } |
||
285 | |||
286 | /**
|
||
287 | * @return
|
||
288 | */
|
||
289 | public String getAbstract() { |
||
290 | 6087 | jaume | |
291 | 5277 | jmvivo | return client.getServiceInformation().abstr;
|
292 | 6087 | jaume | |
293 | 3746 | jaume | } |
294 | |||
295 | /**
|
||
296 | * @param layerName
|
||
297 | * @param srs
|
||
298 | * @return
|
||
299 | */
|
||
300 | public Rectangle2D getLayersExtent(String[] layerName, String srs) { |
||
301 | 5277 | jmvivo | return client.getLayersExtent(layerName, srs);
|
302 | 3746 | jaume | } |
303 | |||
304 | /**
|
||
305 | * @param string
|
||
306 | * @return
|
||
307 | */
|
||
308 | public WMSLayerNode getLayer(String layerName) { |
||
309 | if (getLayers().get(layerName) != null) |
||
310 | { |
||
311 | return (WMSLayerNode)layers.get(layerName);
|
||
312 | } |
||
313 | return null; |
||
314 | } |
||
315 | |||
316 | /**
|
||
317 | * @return
|
||
318 | */
|
||
319 | private TreeMap getLayers() { |
||
320 | if (fmapRootLayer == null){ |
||
321 | fmapRootLayer = getLayersTree(); |
||
322 | } |
||
323 | return layers;
|
||
324 | } |
||
325 | |||
326 | /**
|
||
327 | * @param wmsStatus
|
||
328 | * @param i
|
||
329 | * @param j
|
||
330 | * @param max_value
|
||
331 | * @return
|
||
332 | 6087 | jaume | * @throws WMSException
|
333 | 3746 | jaume | */
|
334 | public String getFeatureInfo(WMSStatus _wmsStatus, int i, int j, int max_value) throws WMSException { |
||
335 | try {
|
||
336 | return client.getFeatureInfo(_wmsStatus, i, j, max_value);
|
||
337 | } catch (org.gvsig.remoteClient.exceptions.WMSException e) {
|
||
338 | 6087 | jaume | throw new WMSException(); |
339 | 5277 | jmvivo | } |
340 | 3746 | jaume | } |
341 | 6087 | jaume | |
342 | 3746 | jaume | public String getHost(){ |
343 | 5277 | jmvivo | return client.getHost();
|
344 | |||
345 | 3746 | jaume | } |
346 | } |