root / trunk / libraries / libWMSv0 / src / com / iver / wmsclient / wms_1_1_0 / WMS_1_1_0.java @ 2040
History | View | Annotate | Download (12.8 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
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 |
* Av. Blasco Ib??ez, 50
|
24 |
* 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.wmsclient.wms_1_1_0; |
42 |
|
43 |
import com.iver.utiles.StringUtilities; |
44 |
|
45 |
import com.iver.wmsclient.Capabilities; |
46 |
import com.iver.wmsclient.FeatureInfoQuery; |
47 |
import com.iver.wmsclient.MapQuery; |
48 |
import com.iver.wmsclient.UnsupportedVersionException; |
49 |
import com.iver.wmsclient.WMSException; |
50 |
import com.iver.wmsclient.WMSUtilities; |
51 |
import com.iver.wmsclient.WMSVersionClient; |
52 |
import com.iver.wmsclient.wms_1_1_0.capabilities.BoundingBox; |
53 |
import com.iver.wmsclient.wms_1_1_0.capabilities.DCPType; |
54 |
import com.iver.wmsclient.wms_1_1_0.capabilities.Format; |
55 |
import com.iver.wmsclient.wms_1_1_0.capabilities.GetFeatureInfo; |
56 |
import com.iver.wmsclient.wms_1_1_0.capabilities.LatLonBoundingBox; |
57 |
import com.iver.wmsclient.wms_1_1_0.capabilities.Layer; |
58 |
import com.iver.wmsclient.wms_1_1_0.capabilities.WMT_MS_Capabilities; |
59 |
import com.iver.wmsclient.wms_1_1_0.exception.ServiceException; |
60 |
import com.iver.wmsclient.wms_1_1_0.exception.ServiceExceptionReport; |
61 |
|
62 |
import org.apache.log4j.Logger; |
63 |
|
64 |
import org.exolab.castor.xml.MarshalException; |
65 |
import org.exolab.castor.xml.Unmarshaller; |
66 |
import org.exolab.castor.xml.ValidationException; |
67 |
|
68 |
import java.awt.geom.Rectangle2D; |
69 |
|
70 |
import java.io.ByteArrayInputStream; |
71 |
import java.io.ByteArrayOutputStream; |
72 |
import java.io.IOException; |
73 |
import java.io.InputStream; |
74 |
import java.io.InputStreamReader; |
75 |
|
76 |
import java.net.URL; |
77 |
|
78 |
import java.util.ArrayList; |
79 |
|
80 |
|
81 |
/**
|
82 |
* Cliente para la versi?n 1.1.0
|
83 |
*
|
84 |
* @author Fernando Gonz?lez Cort?s
|
85 |
*/
|
86 |
public class WMS_1_1_0 implements WMSVersionClient { |
87 |
private static Logger logger = Logger.getLogger(WMS_1_1_0.class.getName()); |
88 |
private String[] mapURLs; |
89 |
private String[] infoURLs; |
90 |
private WMT_MS_Capabilities root;
|
91 |
|
92 |
/**
|
93 |
* @see com.iver.cit.gvsig.fmap.wms.wmsclient.WMSClient#getCapabilities(java.net.URL)
|
94 |
*/
|
95 |
public Capabilities getCapabilities(URL host) |
96 |
throws UnsupportedVersionException, IllegalStateException, IOException { |
97 |
InputStream in = null; |
98 |
String response = null; |
99 |
|
100 |
try {
|
101 |
logger.debug("Obteniendo capabilities...");
|
102 |
|
103 |
String hostString = host.toString();
|
104 |
|
105 |
if (!hostString.endsWith("?")) { |
106 |
hostString += "?";
|
107 |
} |
108 |
|
109 |
String request = getQueryHeader() + "SERVICE=WMS&request=GetCapabilities"; |
110 |
|
111 |
//String request = "REQUEST=GetCapabilities&SERVICE=wms";
|
112 |
URL query = new URL(hostString + request); |
113 |
in = query.openStream(); |
114 |
|
115 |
ByteArrayOutputStream outbytes = new ByteArrayOutputStream(); |
116 |
WMSUtilities.serializar(in, outbytes); |
117 |
|
118 |
response = new String(outbytes.toByteArray()); |
119 |
logger.debug(response); |
120 |
|
121 |
logger.debug("Parseando la respuesta...");
|
122 |
|
123 |
try {
|
124 |
Unmarshaller umn = new Unmarshaller(WMT_MS_Capabilities.class);
|
125 |
umn.setIgnoreExtraElements(true);
|
126 |
umn.setIgnoreExtraAttributes(true);
|
127 |
root = (WMT_MS_Capabilities) umn.unmarshal(new InputStreamReader( |
128 |
new ByteArrayInputStream(WMSUtilities.eliminarDTD( |
129 |
outbytes.toByteArray(), |
130 |
"WMT_MS_Capabilities"))));
|
131 |
} catch (MarshalException e) { |
132 |
Capabilities c = new Capabilities();
|
133 |
c.setVersion(null);
|
134 |
|
135 |
return c;
|
136 |
} |
137 |
|
138 |
// LocalConfiguration.getInstance().getProperties().setProperty("org.exolab.castor.xml.strictelements", "false");
|
139 |
|
140 |
/* WMT_MS_Capabilities root = (WMT_MS_Capabilities) WMT_MS_Capabilities.unmarshal(new InputStreamReader(
|
141 |
new ByteArrayInputStream(WMSUtilities.eliminarDTD(
|
142 |
outbytes.toByteArray()))));
|
143 |
*/
|
144 |
DCPType[] DCPs = root.getCapability().getRequest().getGetMap()
|
145 |
.getDCPType(); |
146 |
mapURLs = new String[DCPs.length]; |
147 |
|
148 |
for (int i = 0; i < DCPs.length; i++) { |
149 |
mapURLs[i] = DCPs[i].getHTTP().getGet().getOnlineResource() |
150 |
.getHref(); |
151 |
|
152 |
if (!mapURLs[i].endsWith("?")) { |
153 |
mapURLs[i] = mapURLs[i] + "?";
|
154 |
} |
155 |
} |
156 |
|
157 |
GetFeatureInfo fi = root.getCapability().getRequest() |
158 |
.getGetFeatureInfo(); |
159 |
|
160 |
if (fi != null) { |
161 |
DCPs = fi.getDCPType(); |
162 |
infoURLs = new String[DCPs.length]; |
163 |
|
164 |
for (int i = 0; i < DCPs.length; i++) { |
165 |
infoURLs[i] = DCPs[i].getHTTP().getGet().getOnlineResource() |
166 |
.getHref(); |
167 |
|
168 |
if (!infoURLs[i].endsWith("?")) { |
169 |
infoURLs[i] = infoURLs[i] + "?";
|
170 |
} |
171 |
} |
172 |
} |
173 |
|
174 |
Capabilities ret = new Capabilities();
|
175 |
ret.setRoot(root); |
176 |
ret.setVersion(root.getVersion()); |
177 |
|
178 |
return ret;
|
179 |
} catch (IOException e) { |
180 |
logger.debug(e); |
181 |
throw e;
|
182 |
} catch (Exception e) { |
183 |
logger.error("Error", e);
|
184 |
|
185 |
Capabilities ret = new Capabilities();
|
186 |
|
187 |
if (response == null) { |
188 |
ret.setRoot(null);
|
189 |
ret.setVersion(null);
|
190 |
|
191 |
return ret;
|
192 |
} else {
|
193 |
ret.setRoot(null);
|
194 |
ret.setVersion(StringUtilities.substringDelimited(response, |
195 |
"version=\"", "\"", response.indexOf("bilities"))); |
196 |
|
197 |
return ret;
|
198 |
} |
199 |
} finally {
|
200 |
if (in != null) { |
201 |
in.close(); |
202 |
} |
203 |
} |
204 |
} |
205 |
|
206 |
/**
|
207 |
* @see com.iver.cit.gvsig.fmap.wms.wmsclient.WMSClient#doMapQuery(com.iver.cit.gvsig.fmap.wms.wmsclient.MapQuery)
|
208 |
*/
|
209 |
public byte[] doMapQuery(MapQuery queryInfo) |
210 |
throws IOException, NoSuchFieldException, ValidationException, |
211 |
WMSException { |
212 |
throw new RuntimeException("no debio llegar aqu?"); |
213 |
} |
214 |
|
215 |
/**
|
216 |
* @see com.iver.cit.gvsig.fmap.wms.wmsclient.WMSClient#getVersion()
|
217 |
*/
|
218 |
public String getVersion() { |
219 |
return "1.1.0"; |
220 |
} |
221 |
|
222 |
/**
|
223 |
* @see com.iver.cit.gvsig.fmap.wms.wmsclient.WMSClient#getBoundingBox(javax.swing.tree.TreePath,
|
224 |
* java.lang.String)
|
225 |
*/
|
226 |
public Rectangle2D getBoundingBox(String[] t, String srs) { |
227 |
Layer[] layer = getLayerByPath(t);
|
228 |
BoundingBox[] bbox = getBoundingBox(layer);
|
229 |
|
230 |
for (int i = bbox.length - 1; i >= 0; i--) { |
231 |
if (bbox[i].getSRS().equals(srs)) {
|
232 |
return new Rectangle2D.Double(Double.parseDouble( |
233 |
bbox[i].getMinx()), |
234 |
Double.parseDouble(bbox[i].getMiny()),
|
235 |
Double.parseDouble(bbox[i].getMaxx())- Double.parseDouble( |
236 |
bbox[i].getMinx()), |
237 |
Double.parseDouble(bbox[i].getMaxy()) - Double.parseDouble(bbox[i].getMiny())); |
238 |
} |
239 |
} |
240 |
|
241 |
LatLonBoundingBox[] llbbox = getLatLonBoundingBox(layer);
|
242 |
|
243 |
for (int i = llbbox.length - 1; i >= 0; i--) { |
244 |
return new Rectangle2D.Double(Double.parseDouble( |
245 |
llbbox[i].getMinx()), |
246 |
Double.parseDouble(llbbox[i].getMiny()),
|
247 |
Double.parseDouble(llbbox[i].getMaxx()) - Double.parseDouble( |
248 |
llbbox[i].getMinx()), |
249 |
Double.parseDouble(llbbox[i].getMaxy()) - Double.parseDouble(llbbox[i].getMiny())); |
250 |
} |
251 |
|
252 |
return null; |
253 |
} |
254 |
|
255 |
/**
|
256 |
* Dado un array de layers devuelve el array de boundingbox de todos ellos
|
257 |
*
|
258 |
* @param layer Array de layers
|
259 |
*
|
260 |
* @return array de boundingBox
|
261 |
*/
|
262 |
private BoundingBox[] getBoundingBox(Layer[] layer) { |
263 |
ArrayList globalBbox = new ArrayList(); |
264 |
|
265 |
for (int i = 0; i < layer.length; i++) { |
266 |
Layer l = (Layer) layer[i]; |
267 |
BoundingBox[] bboxes = l.getBoundingBox();
|
268 |
|
269 |
if (bboxes != null) { |
270 |
for (int j = 0; j < bboxes.length; j++) { |
271 |
globalBbox.add(bboxes[j]); |
272 |
} |
273 |
} |
274 |
} |
275 |
|
276 |
return (BoundingBox[]) globalBbox.toArray(new BoundingBox[0]); |
277 |
} |
278 |
|
279 |
/**
|
280 |
* Dado un array de layers devuelve el array de boundingbox de todos ellos
|
281 |
*
|
282 |
* @param layer Array de layers
|
283 |
*
|
284 |
* @return array de boundingBox
|
285 |
*/
|
286 |
private LatLonBoundingBox[] getLatLonBoundingBox(Layer[] layer) { |
287 |
ArrayList globalBbox = new ArrayList(); |
288 |
|
289 |
for (int i = 0; i < layer.length; i++) { |
290 |
Layer l = (Layer) layer[i]; |
291 |
LatLonBoundingBox bbox = l.getLatLonBoundingBox(); |
292 |
|
293 |
if (bbox != null) { |
294 |
globalBbox.add(bbox); |
295 |
} |
296 |
} |
297 |
|
298 |
return (LatLonBoundingBox[]) globalBbox.toArray(new LatLonBoundingBox[0]); |
299 |
} |
300 |
|
301 |
/**
|
302 |
* Obtiene dado un TreePath con las capas devuelve un array con los objetos
|
303 |
* Layer desde el inicio al final del path
|
304 |
*
|
305 |
* @param t TreePath
|
306 |
*
|
307 |
* @return Array de layers
|
308 |
*/
|
309 |
private Layer[] getLayerByPath(String[] t) { |
310 |
ArrayList ret = new ArrayList(); |
311 |
Layer raiz = root.getCapability().getLayer(); |
312 |
ret.add(raiz); |
313 |
|
314 |
for (int i = 1; i < t.length; i++) { |
315 |
for (int j = 0; j < raiz.getLayerCount(); j++) { |
316 |
if (raiz.getLayer(j).getTitle() == null) continue; |
317 |
if (raiz.getLayer(j).getTitle().getContent().equals(t[i])) {
|
318 |
raiz = raiz.getLayer(j); |
319 |
ret.add(raiz); |
320 |
|
321 |
break;
|
322 |
} |
323 |
} |
324 |
} |
325 |
|
326 |
return (Layer[]) ret.toArray(new Layer[0]); |
327 |
} |
328 |
|
329 |
/**
|
330 |
* @see com.iver.cit.gvsig.fmap.wms.wmsclient.WMSClient#doFeatureInfo(com.iver.cit.gvsig.fmap.wms.wmsclient.FeatureInfoQuery)
|
331 |
*/
|
332 |
public byte[] doFeatureInfo(FeatureInfoQuery query) |
333 |
throws ValidationException, IOException, WMSException, |
334 |
NoSuchFieldException {
|
335 |
throw new RuntimeException("no debio llegar aqu?"); |
336 |
} |
337 |
|
338 |
/**
|
339 |
* @see com.iver.cit.gvsig.fmap.wms.WMSVersionClient#getMapURLs()
|
340 |
*/
|
341 |
public String[] getMapURLs() { |
342 |
return mapURLs;
|
343 |
} |
344 |
|
345 |
/**
|
346 |
* @see com.iver.cit.gvsig.fmap.wms.WMSVersionClient#parseException(byte[])
|
347 |
*/
|
348 |
public String parseException(byte[] bytes) { |
349 |
try {
|
350 |
String error = ""; |
351 |
|
352 |
ServiceExceptionReport ex = (ServiceExceptionReport) ServiceExceptionReport.unmarshal(new InputStreamReader( |
353 |
new ByteArrayInputStream(bytes))); |
354 |
ServiceException[] exs = ex.getServiceException();
|
355 |
|
356 |
for (int i = 0; i < exs.length; i++) { |
357 |
if (exs[i].getCode() != null) { |
358 |
error = error + exs[i].getCode() + ": ";
|
359 |
} |
360 |
|
361 |
error += (exs[i].getContent() + "\n");
|
362 |
} |
363 |
|
364 |
return error;
|
365 |
} catch (Throwable e) { |
366 |
return null; |
367 |
} |
368 |
} |
369 |
|
370 |
/**
|
371 |
* @see com.iver.cit.gvsig.fmap.wms.WMSVersionClient#getInfoURLs()
|
372 |
*/
|
373 |
public String[] getInfoURLs() { |
374 |
return infoURLs;
|
375 |
} |
376 |
|
377 |
/**
|
378 |
* @see com.iver.wmsclient.WMSClient#getInfoFormats()
|
379 |
*/
|
380 |
public String[] getInfoFormats() { |
381 |
Format[] formats = root.getCapability().getRequest().getGetFeatureInfo().getFormat(); |
382 |
String[] ret = new String[formats.length]; |
383 |
for (int i = 0; i < formats.length; i++) { |
384 |
ret[i] = formats[i].getContent(); |
385 |
} |
386 |
|
387 |
return ret;
|
388 |
} |
389 |
|
390 |
/**
|
391 |
* @see com.iver.wmsclient.WMSVersionClient#getQueryHeader()
|
392 |
*/
|
393 |
public String getQueryHeader() { |
394 |
return "VERSION=1.1.0&"; |
395 |
} |
396 |
|
397 |
/**
|
398 |
* @see com.iver.wmsclient.WMSClient#createQuery()
|
399 |
*/
|
400 |
public MapQuery createQuery() {
|
401 |
throw new RuntimeException("No debi? llegar nunca a esta linea"); |
402 |
} |
403 |
} |