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

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

sonar - squid:S2325 - "private" methods that don't access instance data should be "static"

  • Property svn:eol-style set to native
File size: 15.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.imagery;
3
4import java.awt.HeadlessException;
5import java.io.BufferedReader;
6import java.io.IOException;
7import java.io.InputStream;
8import java.io.StringReader;
9import java.net.MalformedURLException;
10import java.net.URL;
11import java.net.URLConnection;
12import java.util.ArrayList;
13import java.util.Collection;
14import java.util.Collections;
15import java.util.HashSet;
16import java.util.List;
17import java.util.Locale;
18import java.util.Set;
19import java.util.regex.Pattern;
20
21import javax.imageio.ImageIO;
22import javax.xml.parsers.DocumentBuilder;
23import javax.xml.parsers.DocumentBuilderFactory;
24
25import org.openstreetmap.josm.Main;
26import org.openstreetmap.josm.data.Bounds;
27import org.openstreetmap.josm.data.imagery.ImageryInfo;
28import org.openstreetmap.josm.gui.preferences.projection.ProjectionChoice;
29import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
30import org.openstreetmap.josm.io.UTFInputStreamReader;
31import org.openstreetmap.josm.tools.Predicate;
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.EntityResolver;
38import org.xml.sax.InputSource;
39import org.xml.sax.SAXException;
40
41public class WMSImagery {
42
43 public static class WMSGetCapabilitiesException extends Exception {
44 private final String incomingData;
45
46 public WMSGetCapabilitiesException(Throwable cause, String incomingData) {
47 super(cause);
48 this.incomingData = incomingData;
49 }
50
51 public String getIncomingData() {
52 return incomingData;
53 }
54 }
55
56 private List<LayerDetails> layers;
57 private URL serviceUrl;
58 private List<String> formats;
59
60 public List<LayerDetails> getLayers() {
61 return layers;
62 }
63
64 public URL getServiceUrl() {
65 return serviceUrl;
66 }
67
68 public List<String> getFormats() {
69 return Collections.unmodifiableList(formats);
70 }
71
72 public String getPreferredFormats() {
73 return formats.contains("image/jpeg") ? "image/jpeg"
74 : formats.contains("image/png") ? "image/png"
75 : formats.isEmpty() ? null
76 : formats.get(0);
77 }
78
79 String buildRootUrl() {
80 if (serviceUrl == null) {
81 return null;
82 }
83 StringBuilder a = new StringBuilder(serviceUrl.getProtocol());
84 a.append("://").append(serviceUrl.getHost());
85 if (serviceUrl.getPort() != -1) {
86 a.append(':').append(serviceUrl.getPort());
87 }
88 a.append(serviceUrl.getPath()).append('?');
89 if (serviceUrl.getQuery() != null) {
90 a.append(serviceUrl.getQuery());
91 if (!serviceUrl.getQuery().isEmpty() && !serviceUrl.getQuery().endsWith("&")) {
92 a.append('&');
93 }
94 }
95 return a.toString();
96 }
97
98 public String buildGetMapUrl(Collection<LayerDetails> selectedLayers) {
99 return buildGetMapUrl(selectedLayers, "image/jpeg");
100 }
101
102 public String buildGetMapUrl(Collection<LayerDetails> selectedLayers, String format) {
103 return buildRootUrl()
104 + "FORMAT=" + format + (imageFormatHasTransparency(format) ? "&TRANSPARENT=TRUE" : "")
105 + "&VERSION=1.1.1&SERVICE=WMS&REQUEST=GetMap&LAYERS="
106 + Utils.join(",", Utils.transform(selectedLayers, new Utils.Function<LayerDetails, String>() {
107 @Override
108 public String apply(LayerDetails x) {
109 return x.ident;
110 }
111 }))
112 + "&STYLES=&SRS={proj}&WIDTH={width}&HEIGHT={height}&BBOX={bbox}";
113 }
114
115 public void attemptGetCapabilities(String serviceUrlStr) throws MalformedURLException, IOException, WMSGetCapabilitiesException {
116 URL getCapabilitiesUrl = null;
117 try {
118 if (!Pattern.compile(".*GetCapabilities.*", Pattern.CASE_INSENSITIVE).matcher(serviceUrlStr).matches()) {
119 // If the url doesn't already have GetCapabilities, add it in
120 getCapabilitiesUrl = new URL(serviceUrlStr);
121 final String getCapabilitiesQuery = "VERSION=1.1.1&SERVICE=WMS&REQUEST=GetCapabilities";
122 if (getCapabilitiesUrl.getQuery() == null) {
123 getCapabilitiesUrl = new URL(serviceUrlStr + '?' + getCapabilitiesQuery);
124 } else if (!getCapabilitiesUrl.getQuery().isEmpty() && !getCapabilitiesUrl.getQuery().endsWith("&")) {
125 getCapabilitiesUrl = new URL(serviceUrlStr + '&' + getCapabilitiesQuery);
126 } else {
127 getCapabilitiesUrl = new URL(serviceUrlStr + getCapabilitiesQuery);
128 }
129 } else {
130 // Otherwise assume it's a good URL and let the subsequent error
131 // handling systems deal with problems
132 getCapabilitiesUrl = new URL(serviceUrlStr);
133 }
134 serviceUrl = new URL(serviceUrlStr);
135 } catch (HeadlessException e) {
136 return;
137 }
138
139 Main.info("GET " + getCapabilitiesUrl);
140 URLConnection openConnection = Utils.openHttpConnection(getCapabilitiesUrl, false, true);
141 StringBuilder ba = new StringBuilder();
142
143 try (
144 InputStream inputStream = openConnection.getInputStream();
145 BufferedReader br = new BufferedReader(UTFInputStreamReader.create(inputStream))
146 ) {
147 String line;
148 while ((line = br.readLine()) != null) {
149 ba.append(line);
150 ba.append('\n');
151 }
152 }
153 String incomingData = ba.toString();
154 Main.debug("Server response to Capabilities request:");
155 Main.debug(incomingData);
156
157 try {
158 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
159 builderFactory.setValidating(false);
160 builderFactory.setNamespaceAware(true);
161 DocumentBuilder builder = null;
162 builder = builderFactory.newDocumentBuilder();
163 builder.setEntityResolver(new EntityResolver() {
164 @Override
165 public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
166 Main.info("Ignoring DTD " + publicId + ", " + systemId);
167 return new InputSource(new StringReader(""));
168 }
169 });
170 Document document = null;
171 document = builder.parse(new InputSource(new StringReader(incomingData)));
172
173 // Some WMS service URLs specify a different base URL for their GetMap service
174 Element child = getChild(document.getDocumentElement(), "Capability");
175 child = getChild(child, "Request");
176 child = getChild(child, "GetMap");
177
178 formats = new ArrayList<>(Utils.filter(Utils.transform(getChildren(child, "Format"),
179 new Utils.Function<Element, String>() {
180 @Override
181 public String apply(Element x) {
182 return x.getTextContent();
183 }
184 }),
185 new Predicate<String>() {
186 @Override
187 public boolean evaluate(String format) {
188 boolean isFormatSupported = isImageFormatSupported(format);
189 if (!isFormatSupported) {
190 Main.info("Skipping unsupported image format {0}", format);
191 }
192 return isFormatSupported;
193 }
194 }
195 ));
196
197 child = getChild(child, "DCPType");
198 child = getChild(child, "HTTP");
199 child = getChild(child, "Get");
200 child = getChild(child, "OnlineResource");
201 if (child != null) {
202 String baseURL = child.getAttribute("xlink:href");
203 if (baseURL != null && !baseURL.equals(serviceUrlStr)) {
204 Main.info("GetCapabilities specifies a different service URL: " + baseURL);
205 serviceUrl = new URL(baseURL);
206 }
207 }
208
209 Element capabilityElem = getChild(document.getDocumentElement(), "Capability");
210 List<Element> children = getChildren(capabilityElem, "Layer");
211 layers = parseLayers(children, new HashSet<String>());
212 } catch (Exception e) {
213 throw new WMSGetCapabilitiesException(e, incomingData);
214 }
215 }
216
217 static boolean isImageFormatSupported(final String format) {
218 return ImageIO.getImageReadersByMIMEType(format).hasNext()
219 // handles image/tiff image/tiff8 image/geotiff image/geotiff8
220 || (format.startsWith("image/tiff") || format.startsWith("image/geotiff")) && ImageIO.getImageReadersBySuffix("tiff").hasNext()
221 || format.startsWith("image/png") && ImageIO.getImageReadersBySuffix("png").hasNext()
222 || format.startsWith("image/svg") && ImageIO.getImageReadersBySuffix("svg").hasNext()
223 || format.startsWith("image/bmp") && ImageIO.getImageReadersBySuffix("bmp").hasNext();
224 }
225
226 static boolean imageFormatHasTransparency(final String format) {
227 return format != null && (format.startsWith("image/png") || format.startsWith("image/gif")
228 || format.startsWith("image/svg") || format.startsWith("image/tiff"));
229 }
230
231 public ImageryInfo toImageryInfo(String name, Collection<LayerDetails> selectedLayers) {
232 ImageryInfo i = new ImageryInfo(name, buildGetMapUrl(selectedLayers));
233 if (selectedLayers != null) {
234 Set<String> proj = new HashSet<>();
235 for (WMSImagery.LayerDetails l : selectedLayers) {
236 proj.addAll(l.getProjections());
237 }
238 i.setServerProjections(proj);
239 }
240 return i;
241 }
242
243 private List<LayerDetails> parseLayers(List<Element> children, Set<String> parentCrs) {
244 List<LayerDetails> details = new ArrayList<>(children.size());
245 for (Element element : children) {
246 details.add(parseLayer(element, parentCrs));
247 }
248 return details;
249 }
250
251 private LayerDetails parseLayer(Element element, Set<String> parentCrs) {
252 String name = getChildContent(element, "Title", null, null);
253 String ident = getChildContent(element, "Name", null, null);
254
255 // The set of supported CRS/SRS for this layer
256 Set<String> crsList = new HashSet<>();
257 // ...including this layer's already-parsed parent projections
258 crsList.addAll(parentCrs);
259
260 // Parse the CRS/SRS pulled out of this layer's XML element
261 // I think CRS and SRS are the same at this point
262 List<Element> crsChildren = getChildren(element, "CRS");
263 crsChildren.addAll(getChildren(element, "SRS"));
264 for (Element child : crsChildren) {
265 String crs = (String) getContent(child);
266 if (!crs.isEmpty()) {
267 String upperCase = crs.trim().toUpperCase(Locale.ENGLISH);
268 crsList.add(upperCase);
269 }
270 }
271
272 // Check to see if any of the specified projections are supported by JOSM
273 boolean josmSupportsThisLayer = false;
274 for (String crs : crsList) {
275 josmSupportsThisLayer |= isProjSupported(crs);
276 }
277
278 Bounds bounds = null;
279 Element bboxElem = getChild(element, "EX_GeographicBoundingBox");
280 if (bboxElem != null) {
281 // Attempt to use EX_GeographicBoundingBox for bounding box
282 double left = Double.parseDouble(getChildContent(bboxElem, "westBoundLongitude", null, null));
283 double top = Double.parseDouble(getChildContent(bboxElem, "northBoundLatitude", null, null));
284 double right = Double.parseDouble(getChildContent(bboxElem, "eastBoundLongitude", null, null));
285 double bot = Double.parseDouble(getChildContent(bboxElem, "southBoundLatitude", null, null));
286 bounds = new Bounds(bot, left, top, right);
287 } else {
288 // If that's not available, try LatLonBoundingBox
289 bboxElem = getChild(element, "LatLonBoundingBox");
290 if (bboxElem != null) {
291 double left = Double.parseDouble(bboxElem.getAttribute("minx"));
292 double top = Double.parseDouble(bboxElem.getAttribute("maxy"));
293 double right = Double.parseDouble(bboxElem.getAttribute("maxx"));
294 double bot = Double.parseDouble(bboxElem.getAttribute("miny"));
295 bounds = new Bounds(bot, left, top, right);
296 }
297 }
298
299 List<Element> layerChildren = getChildren(element, "Layer");
300 List<LayerDetails> childLayers = parseLayers(layerChildren, crsList);
301
302 return new LayerDetails(name, ident, crsList, josmSupportsThisLayer, bounds, childLayers);
303 }
304
305 private static boolean isProjSupported(String crs) {
306 for (ProjectionChoice pc : ProjectionPreference.getProjectionChoices()) {
307 if (pc.getPreferencesFromCode(crs) != null) return true;
308 }
309 return false;
310 }
311
312 private static String getChildContent(Element parent, String name, String missing, String empty) {
313 Element child = getChild(parent, name);
314 if (child == null)
315 return missing;
316 else {
317 String content = (String) getContent(child);
318 return (!content.isEmpty()) ? content : empty;
319 }
320 }
321
322 private static Object getContent(Element element) {
323 NodeList nl = element.getChildNodes();
324 StringBuilder content = new StringBuilder();
325 for (int i = 0; i < nl.getLength(); i++) {
326 Node node = nl.item(i);
327 switch (node.getNodeType()) {
328 case Node.ELEMENT_NODE:
329 return node;
330 case Node.CDATA_SECTION_NODE:
331 case Node.TEXT_NODE:
332 content.append(node.getNodeValue());
333 break;
334 }
335 }
336 return content.toString().trim();
337 }
338
339 private static List<Element> getChildren(Element parent, String name) {
340 List<Element> retVal = new ArrayList<>();
341 for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
342 if (child instanceof Element && name.equals(child.getNodeName())) {
343 retVal.add((Element) child);
344 }
345 }
346 return retVal;
347 }
348
349 private static Element getChild(Element parent, String name) {
350 if (parent == null)
351 return null;
352 for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
353 if (child instanceof Element && name.equals(child.getNodeName()))
354 return (Element) child;
355 }
356 return null;
357 }
358
359 public static class LayerDetails {
360
361 public final String name;
362 public final String ident;
363 public final List<LayerDetails> children;
364 public final Bounds bounds;
365 public final Set<String> crsList;
366 public final boolean supported;
367
368 public LayerDetails(String name, String ident, Set<String> crsList,
369 boolean supportedLayer, Bounds bounds,
370 List<LayerDetails> childLayers) {
371 this.name = name;
372 this.ident = ident;
373 this.supported = supportedLayer;
374 this.children = childLayers;
375 this.bounds = bounds;
376 this.crsList = crsList;
377 }
378
379 public boolean isSupported() {
380 return this.supported;
381 }
382
383 public Set<String> getProjections() {
384 return crsList;
385 }
386
387 @Override
388 public String toString() {
389 if (this.name == null || this.name.isEmpty())
390 return this.ident;
391 else
392 return this.name;
393 }
394
395 }
396}
Note: See TracBrowser for help on using the repository browser.