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