source: josm/trunk/src/org/openstreetmap/josm/io/imagery/WMSImagery.java@ 13228

Last change on this file since 13228 was 13228, checked in by Don-vip, 6 years ago

WMS robustness

  • Property svn:eol-style set to native
File size: 20.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.imagery;
3
4import java.awt.HeadlessException;
5import java.io.IOException;
6import java.io.StringReader;
7import java.net.MalformedURLException;
8import java.net.URL;
9import java.util.ArrayList;
10import java.util.Collection;
11import java.util.Collections;
12import java.util.HashSet;
13import java.util.Iterator;
14import java.util.List;
15import java.util.Locale;
16import java.util.NoSuchElementException;
17import java.util.Set;
18import java.util.regex.Pattern;
19import java.util.stream.Collectors;
20import java.util.stream.Stream;
21import java.util.stream.StreamSupport;
22
23import javax.imageio.ImageIO;
24import javax.xml.parsers.DocumentBuilder;
25import javax.xml.parsers.ParserConfigurationException;
26
27import org.openstreetmap.josm.data.Bounds;
28import org.openstreetmap.josm.data.imagery.ImageryInfo;
29import org.openstreetmap.josm.data.projection.Projections;
30import org.openstreetmap.josm.tools.HttpClient;
31import org.openstreetmap.josm.tools.HttpClient.Response;
32import org.openstreetmap.josm.tools.Logging;
33import org.openstreetmap.josm.tools.Utils;
34import org.w3c.dom.Document;
35import org.w3c.dom.Element;
36import org.w3c.dom.Node;
37import org.w3c.dom.NodeList;
38import org.xml.sax.InputSource;
39import org.xml.sax.SAXException;
40
41/**
42 * This class represents the capabilities of a WMS imagery server.
43 */
44public class WMSImagery {
45
46 private static final class ChildIterator implements Iterator<Element> {
47 private Element child;
48
49 ChildIterator(Element parent) {
50 child = advanceToElement(parent.getFirstChild());
51 }
52
53 private static Element advanceToElement(Node firstChild) {
54 Node node = firstChild;
55 while (node != null && !(node instanceof Element)) {
56 node = node.getNextSibling();
57 }
58 return (Element) node;
59 }
60
61 @Override
62 public boolean hasNext() {
63 return child != null;
64 }
65
66 @Override
67 public Element next() {
68 if (!hasNext()) {
69 throw new NoSuchElementException("No next sibling.");
70 }
71 Element next = child;
72 child = advanceToElement(child.getNextSibling());
73 return next;
74 }
75 }
76
77 /**
78 * An exception that is thrown if there was an error while getting the capabilities of the WMS server.
79 */
80 public static class WMSGetCapabilitiesException extends Exception {
81 private final String incomingData;
82
83 /**
84 * Constructs a new {@code WMSGetCapabilitiesException}
85 * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method)
86 * @param incomingData the answer from WMS server
87 */
88 public WMSGetCapabilitiesException(Throwable cause, String incomingData) {
89 super(cause);
90 this.incomingData = incomingData;
91 }
92
93 /**
94 * Constructs a new {@code WMSGetCapabilitiesException}
95 * @param message the detail message. The detail message is saved for later retrieval by the {@link #getMessage()} method
96 * @param incomingData the answer from the server
97 * @since 10520
98 */
99 public WMSGetCapabilitiesException(String message, String incomingData) {
100 super(message);
101 this.incomingData = incomingData;
102 }
103
104 /**
105 * The data that caused this exception.
106 * @return The server response to the capabilities request.
107 */
108 public String getIncomingData() {
109 return incomingData;
110 }
111 }
112
113 private List<LayerDetails> layers;
114 private URL serviceUrl;
115 private List<String> formats;
116
117 /**
118 * Returns the list of layers.
119 * @return the list of layers
120 */
121 public List<LayerDetails> getLayers() {
122 return Collections.unmodifiableList(layers);
123 }
124
125 /**
126 * Returns the service URL.
127 * @return the service URL
128 */
129 public URL getServiceUrl() {
130 return serviceUrl;
131 }
132
133 /**
134 * Returns the list of supported formats.
135 * @return the list of supported formats
136 */
137 public List<String> getFormats() {
138 return Collections.unmodifiableList(formats);
139 }
140
141 /**
142 * Gets the preffered format for this imagery layer.
143 * @return The preffered format as mime type.
144 */
145 public String getPreferredFormats() {
146 if (formats.contains("image/jpeg")) {
147 return "image/jpeg";
148 } else if (formats.contains("image/png")) {
149 return "image/png";
150 } else if (formats.isEmpty()) {
151 return null;
152 } else {
153 return formats.get(0);
154 }
155 }
156
157 String buildRootUrl() {
158 if (serviceUrl == null) {
159 return null;
160 }
161 StringBuilder a = new StringBuilder(serviceUrl.getProtocol());
162 a.append("://").append(serviceUrl.getHost());
163 if (serviceUrl.getPort() != -1) {
164 a.append(':').append(serviceUrl.getPort());
165 }
166 a.append(serviceUrl.getPath()).append('?');
167 if (serviceUrl.getQuery() != null) {
168 a.append(serviceUrl.getQuery());
169 if (!serviceUrl.getQuery().isEmpty() && !serviceUrl.getQuery().endsWith("&")) {
170 a.append('&');
171 }
172 }
173 return a.toString();
174 }
175
176 /**
177 * Returns the URL for the "GetMap" WMS request in JPEG format.
178 * @param selectedLayers the list of selected layers, matching the "LAYERS" WMS request argument
179 * @return the URL for the "GetMap" WMS request
180 */
181 public String buildGetMapUrl(Collection<LayerDetails> selectedLayers) {
182 return buildGetMapUrl(selectedLayers, "image/jpeg");
183 }
184
185 /**
186 * Returns the URL for the "GetMap" WMS request.
187 * @param selectedLayers the list of selected layers, matching the "LAYERS" WMS request argument
188 * @param format the requested image format, matching the "FORMAT" WMS request argument
189 * @return the URL for the "GetMap" WMS request
190 */
191 public String buildGetMapUrl(Collection<LayerDetails> selectedLayers, String format) {
192 return buildRootUrl() + "FORMAT=" + format + (imageFormatHasTransparency(format) ? "&TRANSPARENT=TRUE" : "")
193 + "&VERSION=1.1.1&SERVICE=WMS&REQUEST=GetMap&LAYERS="
194 + selectedLayers.stream().map(x -> x.ident).collect(Collectors.joining(","))
195 + "&STYLES=&SRS={proj}&WIDTH={width}&HEIGHT={height}&BBOX={bbox}";
196 }
197
198 /**
199 * Attempts WMS "GetCapabilities" request and initializes internal variables if successful.
200 * @param serviceUrlStr WMS service URL
201 * @throws IOException if any I/O errors occurs
202 * @throws WMSGetCapabilitiesException if the WMS server replies a ServiceException
203 */
204 public void attemptGetCapabilities(String serviceUrlStr) throws IOException, WMSGetCapabilitiesException {
205 URL getCapabilitiesUrl = null;
206 try {
207 if (!Pattern.compile(".*GetCapabilities.*", Pattern.CASE_INSENSITIVE).matcher(serviceUrlStr).matches()) {
208 // If the url doesn't already have GetCapabilities, add it in
209 getCapabilitiesUrl = new URL(serviceUrlStr);
210 final String getCapabilitiesQuery = "VERSION=1.1.1&SERVICE=WMS&REQUEST=GetCapabilities";
211 if (getCapabilitiesUrl.getQuery() == null) {
212 getCapabilitiesUrl = new URL(serviceUrlStr + '?' + getCapabilitiesQuery);
213 } else if (!getCapabilitiesUrl.getQuery().isEmpty() && !getCapabilitiesUrl.getQuery().endsWith("&")) {
214 getCapabilitiesUrl = new URL(serviceUrlStr + '&' + getCapabilitiesQuery);
215 } else {
216 getCapabilitiesUrl = new URL(serviceUrlStr + getCapabilitiesQuery);
217 }
218 } else {
219 // Otherwise assume it's a good URL and let the subsequent error
220 // handling systems deal with problems
221 getCapabilitiesUrl = new URL(serviceUrlStr);
222 }
223 // Make sure we don't keep GetCapabilities request in service URL
224 serviceUrl = new URL(serviceUrlStr.replace("REQUEST=GetCapabilities", "").replace("&&", "&"));
225 } catch (HeadlessException e) {
226 Logging.warn(e);
227 return;
228 }
229
230 final Response response = HttpClient.create(getCapabilitiesUrl).connect();
231 final String incomingData = response.fetchContent();
232 Logging.debug("Server response to Capabilities request:");
233 Logging.debug(incomingData);
234
235 if (response.getResponseCode() >= 400) {
236 throw new WMSGetCapabilitiesException(response.getResponseMessage(), incomingData);
237 }
238
239 try {
240 DocumentBuilder builder = Utils.newSafeDOMBuilder();
241 builder.setEntityResolver((publicId, systemId) -> {
242 Logging.info("Ignoring DTD " + publicId + ", " + systemId);
243 return new InputSource(new StringReader(""));
244 });
245 Document document = builder.parse(new InputSource(new StringReader(incomingData)));
246 Element root = document.getDocumentElement();
247
248 // Check if the request resulted in ServiceException
249 if ("ServiceException".equals(root.getTagName())) {
250 throw new WMSGetCapabilitiesException(root.getTextContent(), incomingData);
251 }
252
253 // Some WMS service URLs specify a different base URL for their GetMap service
254 Element child = getChild(root, "Capability");
255 child = getChild(child, "Request");
256 child = getChild(child, "GetMap");
257
258 formats = getChildrenStream(child, "Format")
259 .map(Node::getTextContent)
260 .filter(WMSImagery::isImageFormatSupportedWarn)
261 .collect(Collectors.toList());
262
263 child = getChild(child, "DCPType");
264 child = getChild(child, "HTTP");
265 child = getChild(child, "Get");
266 child = getChild(child, "OnlineResource");
267 if (child != null) {
268 String baseURL = child.getAttribute("xlink:href");
269 if (!baseURL.equals(serviceUrlStr)) {
270 URL newURL = new URL(baseURL);
271 if (newURL.getAuthority() != null) {
272 Logging.info("GetCapabilities specifies a different service URL: " + baseURL);
273 serviceUrl = newURL;
274 }
275 }
276 }
277
278 Element capabilityElem = getChild(root, "Capability");
279 List<Element> children = getChildren(capabilityElem, "Layer");
280 layers = parseLayers(children, new HashSet<String>());
281 } catch (MalformedURLException | ParserConfigurationException | SAXException e) {
282 throw new WMSGetCapabilitiesException(e, incomingData);
283 }
284 }
285
286 private static boolean isImageFormatSupportedWarn(String format) {
287 boolean isFormatSupported = isImageFormatSupported(format);
288 if (!isFormatSupported) {
289 Logging.info("Skipping unsupported image format {0}", format);
290 }
291 return isFormatSupported;
292 }
293
294 static boolean isImageFormatSupported(final String format) {
295 return ImageIO.getImageReadersByMIMEType(format).hasNext()
296 // handles image/tiff image/tiff8 image/geotiff image/geotiff8
297 || isImageFormatSupported(format, "tiff", "geotiff")
298 || isImageFormatSupported(format, "png")
299 || isImageFormatSupported(format, "svg")
300 || isImageFormatSupported(format, "bmp");
301 }
302
303 static boolean isImageFormatSupported(String format, String... mimeFormats) {
304 for (String mime : mimeFormats) {
305 if (format.startsWith("image/" + mime)) {
306 return ImageIO.getImageReadersBySuffix(mimeFormats[0]).hasNext();
307 }
308 }
309 return false;
310 }
311
312 static boolean imageFormatHasTransparency(final String format) {
313 return format != null && (format.startsWith("image/png") || format.startsWith("image/gif")
314 || format.startsWith("image/svg") || format.startsWith("image/tiff"));
315 }
316
317 /**
318 * Returns a new {@code ImageryInfo} describing the given service name and selected WMS layers.
319 * @param name service name
320 * @param selectedLayers selected WMS layers
321 * @return a new {@code ImageryInfo} describing the given service name and selected WMS layers
322 */
323 public ImageryInfo toImageryInfo(String name, Collection<LayerDetails> selectedLayers) {
324 ImageryInfo i = new ImageryInfo(name, buildGetMapUrl(selectedLayers));
325 if (selectedLayers != null) {
326 Set<String> proj = new HashSet<>();
327 for (WMSImagery.LayerDetails l : selectedLayers) {
328 proj.addAll(l.getProjections());
329 }
330 i.setServerProjections(proj);
331 }
332 return i;
333 }
334
335 private List<LayerDetails> parseLayers(List<Element> children, Set<String> parentCrs) {
336 List<LayerDetails> details = new ArrayList<>(children.size());
337 for (Element element : children) {
338 details.add(parseLayer(element, parentCrs));
339 }
340 return details;
341 }
342
343 private LayerDetails parseLayer(Element element, Set<String> parentCrs) {
344 String name = getChildContent(element, "Title", null, null);
345 String ident = getChildContent(element, "Name", null, null);
346 String abstr = getChildContent(element, "Abstract", null, null);
347
348 // The set of supported CRS/SRS for this layer
349 Set<String> crsList = new HashSet<>();
350 // ...including this layer's already-parsed parent projections
351 crsList.addAll(parentCrs);
352
353 // Parse the CRS/SRS pulled out of this layer's XML element
354 // I think CRS and SRS are the same at this point
355 getChildrenStream(element)
356 .filter(child -> "CRS".equals(child.getNodeName()) || "SRS".equals(child.getNodeName()))
357 .map(child -> (String) getContent(child))
358 .filter(crs -> !crs.isEmpty())
359 .map(crs -> crs.trim().toUpperCase(Locale.ENGLISH))
360 .forEach(crsList::add);
361
362 // Check to see if any of the specified projections are supported by JOSM
363 boolean josmSupportsThisLayer = false;
364 for (String crs : crsList) {
365 josmSupportsThisLayer |= isProjSupported(crs);
366 }
367
368 Bounds bounds = null;
369 Element bboxElem = getChild(element, "EX_GeographicBoundingBox");
370 if (bboxElem != null) {
371 // Attempt to use EX_GeographicBoundingBox for bounding box
372 double left = Double.parseDouble(getChildContent(bboxElem, "westBoundLongitude", null, null));
373 double top = Double.parseDouble(getChildContent(bboxElem, "northBoundLatitude", null, null));
374 double right = Double.parseDouble(getChildContent(bboxElem, "eastBoundLongitude", null, null));
375 double bot = Double.parseDouble(getChildContent(bboxElem, "southBoundLatitude", null, null));
376 bounds = new Bounds(bot, left, top, right);
377 } else {
378 // If that's not available, try LatLonBoundingBox
379 bboxElem = getChild(element, "LatLonBoundingBox");
380 if (bboxElem != null) {
381 double left = getDecimalDegree(bboxElem, "minx");
382 double top = getDecimalDegree(bboxElem, "maxy");
383 double right = getDecimalDegree(bboxElem, "maxx");
384 double bot = getDecimalDegree(bboxElem, "miny");
385 bounds = new Bounds(bot, left, top, right);
386 }
387 }
388
389 List<Element> layerChildren = getChildren(element, "Layer");
390 List<LayerDetails> childLayers = parseLayers(layerChildren, crsList);
391
392 return new LayerDetails(name, ident, abstr, crsList, josmSupportsThisLayer, bounds, childLayers);
393 }
394
395 private static double getDecimalDegree(Element elem, String attr) {
396 // Some real-world WMS servers use a comma instead of a dot as decimal separator (seen in Polish WMS server)
397 return Double.parseDouble(elem.getAttribute(attr).replace(',', '.'));
398 }
399
400 private static boolean isProjSupported(String crs) {
401 return Projections.getProjectionByCode(crs) != null;
402 }
403
404 private static String getChildContent(Element parent, String name, String missing, String empty) {
405 Element child = getChild(parent, name);
406 if (child == null)
407 return missing;
408 else {
409 String content = (String) getContent(child);
410 return (!content.isEmpty()) ? content : empty;
411 }
412 }
413
414 private static Object getContent(Element element) {
415 NodeList nl = element.getChildNodes();
416 StringBuilder content = new StringBuilder();
417 for (int i = 0; i < nl.getLength(); i++) {
418 Node node = nl.item(i);
419 switch (node.getNodeType()) {
420 case Node.ELEMENT_NODE:
421 return node;
422 case Node.CDATA_SECTION_NODE:
423 case Node.TEXT_NODE:
424 content.append(node.getNodeValue());
425 break;
426 default: // Do nothing
427 }
428 }
429 return content.toString().trim();
430 }
431
432 private static Stream<Element> getChildrenStream(Element parent) {
433 if (parent == null) {
434 // ignore missing elements
435 return Stream.empty();
436 } else {
437 Iterable<Element> it = () -> new ChildIterator(parent);
438 return StreamSupport.stream(it.spliterator(), false);
439 }
440 }
441
442 private static Stream<Element> getChildrenStream(Element parent, String name) {
443 return getChildrenStream(parent).filter(child -> name.equals(child.getNodeName()));
444 }
445
446 private static List<Element> getChildren(Element parent, String name) {
447 return getChildrenStream(parent, name).collect(Collectors.toList());
448 }
449
450 private static Element getChild(Element parent, String name) {
451 return getChildrenStream(parent, name).findFirst().orElse(null);
452 }
453
454 /**
455 * The details of a layer of this WMS server.
456 */
457 public static class LayerDetails {
458
459 /**
460 * The layer name (WMS {@code Title})
461 */
462 public final String name;
463 /**
464 * The layer ident (WMS {@code Name})
465 */
466 public final String ident;
467 /**
468 * The layer abstract (WMS {@code Abstract})
469 * @since 13199
470 */
471 public final String abstr;
472 /**
473 * The child layers of this layer
474 */
475 public final List<LayerDetails> children;
476 /**
477 * The bounds this layer can be used for
478 */
479 public final Bounds bounds;
480 /**
481 * the CRS/SRS pulled out of this layer's XML element
482 */
483 public final Set<String> crsList;
484 /**
485 * {@code true} if any of the specified projections are supported by JOSM
486 */
487 public final boolean supported;
488
489 /**
490 * Constructs a new {@code LayerDetails}.
491 * @param name The layer name (WMS {@code Title})
492 * @param ident The layer ident (WMS {@code Name})
493 * @param abstr The layer abstract (WMS {@code Abstract})
494 * @param crsList The CRS/SRS pulled out of this layer's XML element
495 * @param supportedLayer {@code true} if any of the specified projections are supported by JOSM
496 * @param bounds The bounds this layer can be used for
497 * @param childLayers The child layers of this layer
498 * @since 13199
499 */
500 public LayerDetails(String name, String ident, String abstr, Set<String> crsList, boolean supportedLayer, Bounds bounds,
501 List<LayerDetails> childLayers) {
502 this.name = name;
503 this.ident = ident;
504 this.abstr = abstr;
505 this.supported = supportedLayer;
506 this.children = childLayers;
507 this.bounds = bounds;
508 this.crsList = crsList;
509 }
510
511 /**
512 * Determines if any of the specified projections are supported by JOSM.
513 * @return {@code true} if any of the specified projections are supported by JOSM
514 */
515 public boolean isSupported() {
516 return this.supported;
517 }
518
519 /**
520 * Returns the CRS/SRS pulled out of this layer's XML element.
521 * @return the CRS/SRS pulled out of this layer's XML element
522 */
523 public Set<String> getProjections() {
524 return crsList;
525 }
526
527 @Override
528 public String toString() {
529 String baseName = (name == null || name.isEmpty()) ? ident : name;
530 return abstr == null || abstr.equalsIgnoreCase(baseName) ? baseName : baseName + " (" + abstr + ')';
531 }
532 }
533}
Note: See TracBrowser for help on using the repository browser.