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

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

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

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