1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.imagery;
|
---|
3 |
|
---|
4 | import static java.nio.charset.StandardCharsets.UTF_8;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.io.InputStream;
|
---|
10 | import java.net.MalformedURLException;
|
---|
11 | import java.net.URL;
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.Collection;
|
---|
14 | import java.util.Collections;
|
---|
15 | import java.util.HashSet;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.Map;
|
---|
18 | import java.util.Set;
|
---|
19 | import java.util.concurrent.ConcurrentHashMap;
|
---|
20 | import java.util.function.UnaryOperator;
|
---|
21 | import java.util.regex.Pattern;
|
---|
22 | import java.util.stream.Collectors;
|
---|
23 |
|
---|
24 | import javax.imageio.ImageIO;
|
---|
25 | import javax.xml.namespace.QName;
|
---|
26 | import javax.xml.stream.XMLStreamException;
|
---|
27 | import javax.xml.stream.XMLStreamReader;
|
---|
28 |
|
---|
29 | import org.openstreetmap.josm.data.Bounds;
|
---|
30 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
31 | import org.openstreetmap.josm.data.imagery.DefaultLayer;
|
---|
32 | import org.openstreetmap.josm.data.imagery.GetCapabilitiesParseHelper;
|
---|
33 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
34 | import org.openstreetmap.josm.data.imagery.LayerDetails;
|
---|
35 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
36 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
37 | import org.openstreetmap.josm.io.CachedFile;
|
---|
38 | import org.openstreetmap.josm.tools.Logging;
|
---|
39 | import org.openstreetmap.josm.tools.Utils;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * This class represents the capabilities of a WMS imagery server.
|
---|
43 | */
|
---|
44 | public class WMSImagery {
|
---|
45 |
|
---|
46 | private static final String CAPABILITIES_QUERY_STRING = "SERVICE=WMS&REQUEST=GetCapabilities";
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * WMS namespace address
|
---|
50 | */
|
---|
51 | public static final String WMS_NS_URL = "http://www.opengis.net/wms";
|
---|
52 |
|
---|
53 | // CHECKSTYLE.OFF: SingleSpaceSeparator
|
---|
54 | // WMS 1.0 - 1.3.0
|
---|
55 | private static final QName CAPABILITITES_ROOT_130 = new QName("WMS_Capabilities", WMS_NS_URL);
|
---|
56 | private static final QName QN_ABSTRACT = new QName(WMS_NS_URL, "Abstract");
|
---|
57 | private static final QName QN_CAPABILITY = new QName(WMS_NS_URL, "Capability");
|
---|
58 | private static final QName QN_CRS = new QName(WMS_NS_URL, "CRS");
|
---|
59 | private static final QName QN_DCPTYPE = new QName(WMS_NS_URL, "DCPType");
|
---|
60 | private static final QName QN_FORMAT = new QName(WMS_NS_URL, "Format");
|
---|
61 | private static final QName QN_GET = new QName(WMS_NS_URL, "Get");
|
---|
62 | private static final QName QN_GETMAP = new QName(WMS_NS_URL, "GetMap");
|
---|
63 | private static final QName QN_HTTP = new QName(WMS_NS_URL, "HTTP");
|
---|
64 | private static final QName QN_LAYER = new QName(WMS_NS_URL, "Layer");
|
---|
65 | private static final QName QN_NAME = new QName(WMS_NS_URL, "Name");
|
---|
66 | private static final QName QN_REQUEST = new QName(WMS_NS_URL, "Request");
|
---|
67 | private static final QName QN_SERVICE = new QName(WMS_NS_URL, "Service");
|
---|
68 | private static final QName QN_STYLE = new QName(WMS_NS_URL, "Style");
|
---|
69 | private static final QName QN_TITLE = new QName(WMS_NS_URL, "Title");
|
---|
70 | private static final QName QN_BOUNDINGBOX = new QName(WMS_NS_URL, "BoundingBox");
|
---|
71 | private static final QName QN_EX_GEOGRAPHIC_BBOX = new QName(WMS_NS_URL, "EX_GeographicBoundingBox");
|
---|
72 | private static final QName QN_WESTBOUNDLONGITUDE = new QName(WMS_NS_URL, "westBoundLongitude");
|
---|
73 | private static final QName QN_EASTBOUNDLONGITUDE = new QName(WMS_NS_URL, "eastBoundLongitude");
|
---|
74 | private static final QName QN_SOUTHBOUNDLATITUDE = new QName(WMS_NS_URL, "southBoundLatitude");
|
---|
75 | private static final QName QN_NORTHBOUNDLATITUDE = new QName(WMS_NS_URL, "northBoundLatitude");
|
---|
76 | private static final QName QN_ONLINE_RESOURCE = new QName(WMS_NS_URL, "OnlineResource");
|
---|
77 |
|
---|
78 | // WMS 1.1 - 1.1.1
|
---|
79 | private static final QName CAPABILITIES_ROOT_111 = new QName("WMT_MS_Capabilities");
|
---|
80 | private static final QName QN_SRS = new QName("SRS");
|
---|
81 | private static final QName QN_LATLONBOUNDINGBOX = new QName("LatLonBoundingBox");
|
---|
82 |
|
---|
83 | // CHECKSTYLE.ON: SingleSpaceSeparator
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * An exception that is thrown if there was an error while getting the capabilities of the WMS server.
|
---|
87 | */
|
---|
88 | public static class WMSGetCapabilitiesException extends Exception {
|
---|
89 | private final String incomingData;
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Constructs a new {@code WMSGetCapabilitiesException}
|
---|
93 | * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method)
|
---|
94 | * @param incomingData the answer from WMS server
|
---|
95 | */
|
---|
96 | public WMSGetCapabilitiesException(Throwable cause, String incomingData) {
|
---|
97 | super(cause);
|
---|
98 | this.incomingData = incomingData;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Constructs a new {@code WMSGetCapabilitiesException}
|
---|
103 | * @param message the detail message. The detail message is saved for later retrieval by the {@link #getMessage()} method
|
---|
104 | * @param incomingData the answer from the server
|
---|
105 | * @since 10520
|
---|
106 | */
|
---|
107 | public WMSGetCapabilitiesException(String message, String incomingData) {
|
---|
108 | super(message);
|
---|
109 | this.incomingData = incomingData;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * The data that caused this exception.
|
---|
114 | * @return The server response to the capabilities request.
|
---|
115 | */
|
---|
116 | public String getIncomingData() {
|
---|
117 | return incomingData;
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | private final Map<String, String> headers = new ConcurrentHashMap<>();
|
---|
122 | private String version = "1.1.1"; // default version
|
---|
123 | private String getMapUrl;
|
---|
124 | private URL capabilitiesUrl;
|
---|
125 | private final List<String> formats = new ArrayList<>();
|
---|
126 | private List<LayerDetails> layers = new ArrayList<>();
|
---|
127 |
|
---|
128 | private String title;
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Make getCapabilities request towards given URL
|
---|
132 | * @param url service url
|
---|
133 | * @throws IOException when connection error when fetching get capabilities document
|
---|
134 | * @throws WMSGetCapabilitiesException when there are errors when parsing get capabilities document
|
---|
135 | */
|
---|
136 | public WMSImagery(String url) throws IOException, WMSGetCapabilitiesException {
|
---|
137 | this(url, null);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Make getCapabilities request towards given URL using headers
|
---|
142 | * @param url service url
|
---|
143 | * @param headers HTTP headers to be sent with request
|
---|
144 | * @throws IOException when connection error when fetching get capabilities document
|
---|
145 | * @throws WMSGetCapabilitiesException when there are errors when parsing get capabilities document
|
---|
146 | */
|
---|
147 | public WMSImagery(String url, Map<String, String> headers) throws IOException, WMSGetCapabilitiesException {
|
---|
148 | if (headers != null) {
|
---|
149 | this.headers.putAll(headers);
|
---|
150 | }
|
---|
151 |
|
---|
152 | IOException savedExc = null;
|
---|
153 | String workingAddress = null;
|
---|
154 | url_search:
|
---|
155 | for (String z: new String[]{
|
---|
156 | normalizeUrl(url),
|
---|
157 | url,
|
---|
158 | url + CAPABILITIES_QUERY_STRING,
|
---|
159 | }) {
|
---|
160 | for (String ver: new String[]{"", "&VERSION=1.3.0", "&VERSION=1.1.1"}) {
|
---|
161 | try {
|
---|
162 | attemptGetCapabilities(z + ver);
|
---|
163 | workingAddress = z;
|
---|
164 | calculateChildren();
|
---|
165 | // clear saved exception - we've got something working
|
---|
166 | savedExc = null;
|
---|
167 | break url_search;
|
---|
168 | } catch (IOException e) {
|
---|
169 | savedExc = e;
|
---|
170 | Logging.warn(e);
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | if (workingAddress != null) {
|
---|
176 | try {
|
---|
177 | capabilitiesUrl = new URL(workingAddress);
|
---|
178 | } catch (MalformedURLException e) {
|
---|
179 | if (savedExc == null) {
|
---|
180 | savedExc = e;
|
---|
181 | }
|
---|
182 | try {
|
---|
183 | capabilitiesUrl = new File(workingAddress).toURI().toURL();
|
---|
184 | } catch (MalformedURLException e1) { // NOPMD
|
---|
185 | // do nothing, raise original exception
|
---|
186 | Logging.trace(e1);
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | if (savedExc != null) {
|
---|
192 | throw savedExc;
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | private void calculateChildren() {
|
---|
197 | Map<LayerDetails, List<LayerDetails>> layerChildren = layers.stream()
|
---|
198 | .filter(x -> x.getParent() != null) // exclude top-level elements
|
---|
199 | .collect(Collectors.groupingBy(LayerDetails::getParent));
|
---|
200 | for (LayerDetails ld: layers) {
|
---|
201 | if (layerChildren.containsKey(ld)) {
|
---|
202 | ld.setChildren(layerChildren.get(ld));
|
---|
203 | }
|
---|
204 | }
|
---|
205 | // leave only top-most elements in the list
|
---|
206 | layers = layers.stream().filter(x -> x.getParent() == null).collect(Collectors.toCollection(ArrayList::new));
|
---|
207 | }
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Returns the list of top-level layers.
|
---|
211 | * @return the list of top-level layers
|
---|
212 | */
|
---|
213 | public List<LayerDetails> getLayers() {
|
---|
214 | return Collections.unmodifiableList(layers);
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Returns the list of supported formats.
|
---|
219 | * @return the list of supported formats
|
---|
220 | */
|
---|
221 | public Collection<String> getFormats() {
|
---|
222 | return Collections.unmodifiableList(formats);
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Gets the preferred format for this imagery layer.
|
---|
227 | * @return The preferred format as mime type.
|
---|
228 | */
|
---|
229 | public String getPreferredFormat() {
|
---|
230 | if (formats.contains("image/png")) {
|
---|
231 | return "image/png";
|
---|
232 | } else if (formats.contains("image/jpeg")) {
|
---|
233 | return "image/jpeg";
|
---|
234 | } else if (formats.isEmpty()) {
|
---|
235 | return null;
|
---|
236 | } else {
|
---|
237 | return formats.get(0);
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * @return root URL of services in this GetCapabilities
|
---|
243 | */
|
---|
244 | public String buildRootUrl() {
|
---|
245 | if (getMapUrl == null && capabilitiesUrl == null) {
|
---|
246 | return null;
|
---|
247 | }
|
---|
248 | if (getMapUrl != null) {
|
---|
249 | return getMapUrl;
|
---|
250 | }
|
---|
251 |
|
---|
252 | URL serviceUrl = capabilitiesUrl;
|
---|
253 | StringBuilder a = new StringBuilder(serviceUrl.getProtocol());
|
---|
254 | a.append("://").append(serviceUrl.getHost());
|
---|
255 | if (serviceUrl.getPort() != -1) {
|
---|
256 | a.append(':').append(serviceUrl.getPort());
|
---|
257 | }
|
---|
258 | a.append(serviceUrl.getPath()).append('?');
|
---|
259 | if (serviceUrl.getQuery() != null) {
|
---|
260 | a.append(serviceUrl.getQuery());
|
---|
261 | if (!serviceUrl.getQuery().isEmpty() && !serviceUrl.getQuery().endsWith("&")) {
|
---|
262 | a.append('&');
|
---|
263 | }
|
---|
264 | }
|
---|
265 | return a.toString();
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Returns URL for accessing GetMap service. String will contain following parameters:
|
---|
270 | * * {proj} - that needs to be replaced with projection (one of {@link #getServerProjections(List)})
|
---|
271 | * * {width} - that needs to be replaced with width of the tile
|
---|
272 | * * {height} - that needs to be replaces with height of the tile
|
---|
273 | * * {bbox} - that needs to be replaced with area that should be fetched (in {proj} coordinates)
|
---|
274 | *
|
---|
275 | * Format of the response will be calculated using {@link #getPreferredFormat()}
|
---|
276 | *
|
---|
277 | * @param selectedLayers list of DefaultLayer selection of layers to be shown
|
---|
278 | * @param transparent whether returned images should contain transparent pixels (if supported by format)
|
---|
279 | * @return URL template for GetMap service containing
|
---|
280 | */
|
---|
281 | public String buildGetMapUrl(List<DefaultLayer> selectedLayers, boolean transparent) {
|
---|
282 | return buildGetMapUrl(
|
---|
283 | getLayers(selectedLayers),
|
---|
284 | selectedLayers.stream().map(DefaultLayer::getStyle).collect(Collectors.toList()),
|
---|
285 | transparent);
|
---|
286 | }
|
---|
287 |
|
---|
288 | /**
|
---|
289 | * @param selectedLayers selected layers as subset of the tree returned by {@link #getLayers()}
|
---|
290 | * @param selectedStyles selected styles for all selectedLayers
|
---|
291 | * @param transparent whether returned images should contain transparent pixels (if supported by format)
|
---|
292 | * @return URL template for GetMap service
|
---|
293 | * @see #buildGetMapUrl(List, boolean)
|
---|
294 | */
|
---|
295 | public String buildGetMapUrl(List<LayerDetails> selectedLayers, List<String> selectedStyles, boolean transparent) {
|
---|
296 | return buildGetMapUrl(
|
---|
297 | selectedLayers.stream().map(LayerDetails::getName).collect(Collectors.toList()),
|
---|
298 | selectedStyles,
|
---|
299 | getPreferredFormat(),
|
---|
300 | transparent);
|
---|
301 | }
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * @param selectedLayers selected layers as list of strings
|
---|
305 | * @param selectedStyles selected styles of layers as list of strings
|
---|
306 | * @param format format of the response - one of {@link #getFormats()}
|
---|
307 | * @param transparent whether returned images should contain transparent pixels (if supported by format)
|
---|
308 | * @return URL template for GetMap service
|
---|
309 | * @see #buildGetMapUrl(List, boolean)
|
---|
310 | */
|
---|
311 | public String buildGetMapUrl(List<String> selectedLayers,
|
---|
312 | Collection<String> selectedStyles,
|
---|
313 | String format,
|
---|
314 | boolean transparent) {
|
---|
315 |
|
---|
316 | Utils.ensure(selectedStyles == null || selectedLayers.size() == selectedStyles.size(),
|
---|
317 | tr("Styles size {0} does not match layers size {1}"),
|
---|
318 | selectedStyles == null ? 0 : selectedStyles.size(),
|
---|
319 | selectedLayers.size());
|
---|
320 |
|
---|
321 | return buildRootUrl() + "FORMAT=" + format + ((imageFormatHasTransparency(format) && transparent) ? "&TRANSPARENT=TRUE" : "")
|
---|
322 | + "&VERSION=" + this.version + "&SERVICE=WMS&REQUEST=GetMap&LAYERS="
|
---|
323 | + selectedLayers.stream().collect(Collectors.joining(","))
|
---|
324 | + "&STYLES="
|
---|
325 | + (selectedStyles != null ? Utils.join(",", selectedStyles) : "")
|
---|
326 | + "&"
|
---|
327 | + (belowWMS130() ? "SRS" : "CRS")
|
---|
328 | + "={proj}&WIDTH={width}&HEIGHT={height}&BBOX={bbox}";
|
---|
329 | }
|
---|
330 |
|
---|
331 | private boolean tagEquals(QName a, QName b) {
|
---|
332 | boolean ret = a.equals(b);
|
---|
333 | if (ret) {
|
---|
334 | return ret;
|
---|
335 | }
|
---|
336 |
|
---|
337 | if (belowWMS130()) {
|
---|
338 | return a.getLocalPart().equals(b.getLocalPart());
|
---|
339 | }
|
---|
340 |
|
---|
341 | return false;
|
---|
342 | }
|
---|
343 |
|
---|
344 | private void attemptGetCapabilities(String url) throws IOException, WMSGetCapabilitiesException {
|
---|
345 | Logging.debug("Trying WMS getcapabilities with url {0}", url);
|
---|
346 | try (CachedFile cf = new CachedFile(url); InputStream in = cf.setHttpHeaders(headers).
|
---|
347 | setMaxAge(7 * CachedFile.DAYS).
|
---|
348 | setCachingStrategy(CachedFile.CachingStrategy.IfModifiedSince).
|
---|
349 | getInputStream()) {
|
---|
350 |
|
---|
351 | try {
|
---|
352 | XMLStreamReader reader = GetCapabilitiesParseHelper.getReader(in);
|
---|
353 | for (int event = reader.getEventType(); reader.hasNext(); event = reader.next()) {
|
---|
354 | if (event == XMLStreamReader.START_ELEMENT) {
|
---|
355 | if (tagEquals(CAPABILITIES_ROOT_111, reader.getName())) {
|
---|
356 | // version 1.1.1
|
---|
357 | this.version = reader.getAttributeValue(null, "version");
|
---|
358 | if (this.version == null) {
|
---|
359 | this.version = "1.1.1";
|
---|
360 | }
|
---|
361 | }
|
---|
362 | if (tagEquals(CAPABILITITES_ROOT_130, reader.getName())) {
|
---|
363 | this.version = reader.getAttributeValue(WMS_NS_URL, "version");
|
---|
364 | }
|
---|
365 | if (tagEquals(QN_SERVICE, reader.getName())) {
|
---|
366 | parseService(reader);
|
---|
367 | }
|
---|
368 |
|
---|
369 | if (tagEquals(QN_CAPABILITY, reader.getName())) {
|
---|
370 | parseCapability(reader);
|
---|
371 | }
|
---|
372 | }
|
---|
373 | }
|
---|
374 | } catch (XMLStreamException e) {
|
---|
375 | String content = new String(cf.getByteContent(), UTF_8);
|
---|
376 | cf.clear(); // if there is a problem with parsing of the file, remove it from the cache
|
---|
377 | throw new WMSGetCapabilitiesException(e, content);
|
---|
378 | }
|
---|
379 | }
|
---|
380 | }
|
---|
381 |
|
---|
382 | private void parseService(XMLStreamReader reader) throws XMLStreamException {
|
---|
383 | if (GetCapabilitiesParseHelper.moveReaderToTag(reader, this::tagEquals, QN_TITLE)) {
|
---|
384 | this.title = reader.getElementText();
|
---|
385 | // CHECKSTYLE.OFF: EmptyBlock
|
---|
386 | for (int event = reader.getEventType();
|
---|
387 | reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && tagEquals(QN_SERVICE, reader.getName()));
|
---|
388 | event = reader.next()) {
|
---|
389 | // empty loop, just move reader to the end of Service tag, if moveReaderToTag return false, it's already done
|
---|
390 | }
|
---|
391 | // CHECKSTYLE.ON: EmptyBlock
|
---|
392 | }
|
---|
393 | }
|
---|
394 |
|
---|
395 | private void parseCapability(XMLStreamReader reader) throws XMLStreamException {
|
---|
396 | for (int event = reader.getEventType();
|
---|
397 | reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && tagEquals(QN_CAPABILITY, reader.getName()));
|
---|
398 | event = reader.next()) {
|
---|
399 |
|
---|
400 | if (event == XMLStreamReader.START_ELEMENT) {
|
---|
401 | if (tagEquals(QN_REQUEST, reader.getName())) {
|
---|
402 | parseRequest(reader);
|
---|
403 | }
|
---|
404 | if (tagEquals(QN_LAYER, reader.getName())) {
|
---|
405 | parseLayer(reader, null);
|
---|
406 | }
|
---|
407 | }
|
---|
408 | }
|
---|
409 | }
|
---|
410 |
|
---|
411 | private void parseRequest(XMLStreamReader reader) throws XMLStreamException {
|
---|
412 | String mode = "";
|
---|
413 | String getMapUrl = "";
|
---|
414 | if (GetCapabilitiesParseHelper.moveReaderToTag(reader, this::tagEquals, QN_GETMAP)) {
|
---|
415 | for (int event = reader.getEventType();
|
---|
416 | reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && tagEquals(QN_GETMAP, reader.getName()));
|
---|
417 | event = reader.next()) {
|
---|
418 |
|
---|
419 | if (event == XMLStreamReader.START_ELEMENT) {
|
---|
420 | if (tagEquals(QN_FORMAT, reader.getName())) {
|
---|
421 | String value = reader.getElementText();
|
---|
422 | if (isImageFormatSupportedWarn(value) && !this.formats.contains(value)) {
|
---|
423 | this.formats.add(value);
|
---|
424 | }
|
---|
425 | }
|
---|
426 | if (tagEquals(QN_DCPTYPE, reader.getName()) && GetCapabilitiesParseHelper.moveReaderToTag(reader,
|
---|
427 | this::tagEquals, QN_HTTP, QN_GET)) {
|
---|
428 | mode = reader.getName().getLocalPart();
|
---|
429 | if (GetCapabilitiesParseHelper.moveReaderToTag(reader, this::tagEquals, QN_ONLINE_RESOURCE)) {
|
---|
430 | getMapUrl = reader.getAttributeValue(GetCapabilitiesParseHelper.XLINK_NS_URL, "href");
|
---|
431 | }
|
---|
432 | // TODO should we handle also POST?
|
---|
433 | if ("GET".equalsIgnoreCase(mode) && getMapUrl != null && !"".equals(getMapUrl)) {
|
---|
434 | this.getMapUrl = getMapUrl;
|
---|
435 | }
|
---|
436 | }
|
---|
437 | }
|
---|
438 | }
|
---|
439 | }
|
---|
440 | }
|
---|
441 |
|
---|
442 | private void parseLayer(XMLStreamReader reader, LayerDetails parentLayer) throws XMLStreamException {
|
---|
443 | LayerDetails ret = new LayerDetails(parentLayer);
|
---|
444 | for (int event = reader.next(); // start with advancing reader by one element to get the contents of the layer
|
---|
445 | reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && tagEquals(QN_LAYER, reader.getName()));
|
---|
446 | event = reader.next()) {
|
---|
447 |
|
---|
448 | if (event == XMLStreamReader.START_ELEMENT) {
|
---|
449 | if (tagEquals(QN_NAME, reader.getName())) {
|
---|
450 | ret.setName(reader.getElementText());
|
---|
451 | }
|
---|
452 | if (tagEquals(QN_ABSTRACT, reader.getName())) {
|
---|
453 | ret.setAbstract(GetCapabilitiesParseHelper.getElementTextWithSubtags(reader));
|
---|
454 | }
|
---|
455 | if (tagEquals(QN_TITLE, reader.getName())) {
|
---|
456 | ret.setTitle(reader.getElementText());
|
---|
457 | }
|
---|
458 | if (tagEquals(QN_CRS, reader.getName())) {
|
---|
459 | ret.addCrs(reader.getElementText());
|
---|
460 | }
|
---|
461 | if (tagEquals(QN_SRS, reader.getName()) && belowWMS130()) {
|
---|
462 | ret.addCrs(reader.getElementText());
|
---|
463 | }
|
---|
464 | if (tagEquals(QN_STYLE, reader.getName())) {
|
---|
465 | parseAndAddStyle(reader, ret);
|
---|
466 | }
|
---|
467 | if (tagEquals(QN_LAYER, reader.getName())) {
|
---|
468 | parseLayer(reader, ret);
|
---|
469 | }
|
---|
470 | if (tagEquals(QN_EX_GEOGRAPHIC_BBOX, reader.getName()) && ret.getBounds() == null) {
|
---|
471 | ret.setBounds(parseExGeographic(reader));
|
---|
472 | }
|
---|
473 | if (tagEquals(QN_BOUNDINGBOX, reader.getName())) {
|
---|
474 | Projection conv;
|
---|
475 | if (belowWMS130()) {
|
---|
476 | conv = Projections.getProjectionByCode(reader.getAttributeValue(WMS_NS_URL, "SRS"));
|
---|
477 | } else {
|
---|
478 | conv = Projections.getProjectionByCode(reader.getAttributeValue(WMS_NS_URL, "CRS"));
|
---|
479 | }
|
---|
480 | if (ret.getBounds() == null && conv != null) {
|
---|
481 | ret.setBounds(parseBoundingBox(reader, conv));
|
---|
482 | }
|
---|
483 | }
|
---|
484 | if (tagEquals(QN_LATLONBOUNDINGBOX, reader.getName()) && belowWMS130() && ret.getBounds() == null) {
|
---|
485 | ret.setBounds(parseBoundingBox(reader, null));
|
---|
486 | }
|
---|
487 | }
|
---|
488 | }
|
---|
489 | this.layers.add(ret);
|
---|
490 | }
|
---|
491 |
|
---|
492 | /**
|
---|
493 | * @return if this service operates at protocol level below 1.3.0
|
---|
494 | */
|
---|
495 | public boolean belowWMS130() {
|
---|
496 | return "1.1.1".equals(version) || "1.1".equals(version) || "1.0".equals(version);
|
---|
497 | }
|
---|
498 |
|
---|
499 | private void parseAndAddStyle(XMLStreamReader reader, LayerDetails ld) throws XMLStreamException {
|
---|
500 | String name = null;
|
---|
501 | String title = null;
|
---|
502 | for (int event = reader.getEventType();
|
---|
503 | reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && tagEquals(QN_STYLE, reader.getName()));
|
---|
504 | event = reader.next()) {
|
---|
505 | if (event == XMLStreamReader.START_ELEMENT) {
|
---|
506 | if (tagEquals(QN_NAME, reader.getName())) {
|
---|
507 | name = reader.getElementText();
|
---|
508 | }
|
---|
509 | if (tagEquals(QN_TITLE, reader.getName())) {
|
---|
510 | title = reader.getElementText();
|
---|
511 | }
|
---|
512 | }
|
---|
513 | }
|
---|
514 | if (name == null) {
|
---|
515 | name = "";
|
---|
516 | }
|
---|
517 | ld.addStyle(name, title);
|
---|
518 | }
|
---|
519 |
|
---|
520 | private Bounds parseExGeographic(XMLStreamReader reader) throws XMLStreamException {
|
---|
521 | String minx = null, maxx = null, maxy = null, miny = null;
|
---|
522 |
|
---|
523 | for (int event = reader.getEventType();
|
---|
524 | reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && tagEquals(QN_EX_GEOGRAPHIC_BBOX, reader.getName()));
|
---|
525 | event = reader.next()) {
|
---|
526 | if (event == XMLStreamReader.START_ELEMENT) {
|
---|
527 | if (tagEquals(QN_WESTBOUNDLONGITUDE, reader.getName())) {
|
---|
528 | minx = reader.getElementText();
|
---|
529 | }
|
---|
530 |
|
---|
531 | if (tagEquals(QN_EASTBOUNDLONGITUDE, reader.getName())) {
|
---|
532 | maxx = reader.getElementText();
|
---|
533 | }
|
---|
534 |
|
---|
535 | if (tagEquals(QN_SOUTHBOUNDLATITUDE, reader.getName())) {
|
---|
536 | miny = reader.getElementText();
|
---|
537 | }
|
---|
538 |
|
---|
539 | if (tagEquals(QN_NORTHBOUNDLATITUDE, reader.getName())) {
|
---|
540 | maxy = reader.getElementText();
|
---|
541 | }
|
---|
542 | }
|
---|
543 | }
|
---|
544 | return parseBBox(null, miny, minx, maxy, maxx);
|
---|
545 | }
|
---|
546 |
|
---|
547 | private Bounds parseBoundingBox(XMLStreamReader reader, Projection conv) {
|
---|
548 | UnaryOperator<String> attrGetter = tag -> belowWMS130() ?
|
---|
549 | reader.getAttributeValue(null, tag)
|
---|
550 | : reader.getAttributeValue(WMS_NS_URL, tag);
|
---|
551 |
|
---|
552 | return parseBBox(
|
---|
553 | conv,
|
---|
554 | attrGetter.apply("miny"),
|
---|
555 | attrGetter.apply("minx"),
|
---|
556 | attrGetter.apply("maxy"),
|
---|
557 | attrGetter.apply("maxx")
|
---|
558 | );
|
---|
559 | }
|
---|
560 |
|
---|
561 | private static Bounds parseBBox(Projection conv, String miny, String minx, String maxy, String maxx) {
|
---|
562 | if (miny == null || minx == null || maxy == null || maxx == null) {
|
---|
563 | return null;
|
---|
564 | }
|
---|
565 | if (conv != null) {
|
---|
566 | return new Bounds(
|
---|
567 | conv.eastNorth2latlon(new EastNorth(getDecimalDegree(minx), getDecimalDegree(miny))),
|
---|
568 | conv.eastNorth2latlon(new EastNorth(getDecimalDegree(maxx), getDecimalDegree(maxy)))
|
---|
569 | );
|
---|
570 | }
|
---|
571 | return new Bounds(
|
---|
572 | getDecimalDegree(miny),
|
---|
573 | getDecimalDegree(minx),
|
---|
574 | getDecimalDegree(maxy),
|
---|
575 | getDecimalDegree(maxx)
|
---|
576 | );
|
---|
577 | }
|
---|
578 |
|
---|
579 | private static double getDecimalDegree(String value) {
|
---|
580 | // Some real-world WMS servers use a comma instead of a dot as decimal separator (seen in Polish WMS server)
|
---|
581 | return Double.parseDouble(value.replace(',', '.'));
|
---|
582 | }
|
---|
583 |
|
---|
584 | private static String normalizeUrl(String serviceUrlStr) throws MalformedURLException {
|
---|
585 | URL getCapabilitiesUrl = null;
|
---|
586 | String ret = null;
|
---|
587 |
|
---|
588 | if (!Pattern.compile(".*GetCapabilities.*", Pattern.CASE_INSENSITIVE).matcher(serviceUrlStr).matches()) {
|
---|
589 | // If the url doesn't already have GetCapabilities, add it in
|
---|
590 | getCapabilitiesUrl = new URL(serviceUrlStr);
|
---|
591 | if (getCapabilitiesUrl.getQuery() == null) {
|
---|
592 | ret = serviceUrlStr + '?' + CAPABILITIES_QUERY_STRING;
|
---|
593 | } else if (!getCapabilitiesUrl.getQuery().isEmpty() && !getCapabilitiesUrl.getQuery().endsWith("&")) {
|
---|
594 | ret = serviceUrlStr + '&' + CAPABILITIES_QUERY_STRING;
|
---|
595 | } else {
|
---|
596 | ret = serviceUrlStr + CAPABILITIES_QUERY_STRING;
|
---|
597 | }
|
---|
598 | } else {
|
---|
599 | // Otherwise assume it's a good URL and let the subsequent error
|
---|
600 | // handling systems deal with problems
|
---|
601 | ret = serviceUrlStr;
|
---|
602 | }
|
---|
603 | return ret;
|
---|
604 | }
|
---|
605 |
|
---|
606 | private static boolean isImageFormatSupportedWarn(String format) {
|
---|
607 | boolean isFormatSupported = isImageFormatSupported(format);
|
---|
608 | if (!isFormatSupported) {
|
---|
609 | Logging.info("Skipping unsupported image format {0}", format);
|
---|
610 | }
|
---|
611 | return isFormatSupported;
|
---|
612 | }
|
---|
613 |
|
---|
614 | static boolean isImageFormatSupported(final String format) {
|
---|
615 | return ImageIO.getImageReadersByMIMEType(format).hasNext()
|
---|
616 | // handles image/tiff image/tiff8 image/geotiff image/geotiff8
|
---|
617 | || isImageFormatSupported(format, "tiff", "geotiff")
|
---|
618 | || isImageFormatSupported(format, "png")
|
---|
619 | || isImageFormatSupported(format, "svg")
|
---|
620 | || isImageFormatSupported(format, "bmp");
|
---|
621 | }
|
---|
622 |
|
---|
623 | static boolean isImageFormatSupported(String format, String... mimeFormats) {
|
---|
624 | for (String mime : mimeFormats) {
|
---|
625 | if (format.startsWith("image/" + mime)) {
|
---|
626 | return ImageIO.getImageReadersBySuffix(mimeFormats[0]).hasNext();
|
---|
627 | }
|
---|
628 | }
|
---|
629 | return false;
|
---|
630 | }
|
---|
631 |
|
---|
632 | static boolean imageFormatHasTransparency(final String format) {
|
---|
633 | return format != null && (format.startsWith("image/png") || format.startsWith("image/gif")
|
---|
634 | || format.startsWith("image/svg") || format.startsWith("image/tiff"));
|
---|
635 | }
|
---|
636 |
|
---|
637 | /**
|
---|
638 | * Creates ImageryInfo object from this GetCapabilities document
|
---|
639 | *
|
---|
640 | * @param name name of imagery layer
|
---|
641 | * @param selectedLayers layers which are to be used by this imagery layer
|
---|
642 | * @param selectedStyles styles that should be used for selectedLayers
|
---|
643 | * @param transparent if layer should be transparent
|
---|
644 | * @return ImageryInfo object
|
---|
645 | */
|
---|
646 | public ImageryInfo toImageryInfo(String name, List<LayerDetails> selectedLayers, List<String> selectedStyles, boolean transparent) {
|
---|
647 | ImageryInfo i = new ImageryInfo(name, buildGetMapUrl(selectedLayers, selectedStyles, transparent));
|
---|
648 | if (selectedLayers != null && !selectedLayers.isEmpty()) {
|
---|
649 | i.setServerProjections(getServerProjections(selectedLayers));
|
---|
650 | }
|
---|
651 | return i;
|
---|
652 | }
|
---|
653 |
|
---|
654 | /**
|
---|
655 | * Returns projections that server supports for provided list of layers. This will be intersection of projections
|
---|
656 | * defined for each layer
|
---|
657 | *
|
---|
658 | * @param selectedLayers list of layers
|
---|
659 | * @return projection code
|
---|
660 | */
|
---|
661 | public Collection<String> getServerProjections(List<LayerDetails> selectedLayers) {
|
---|
662 | if (selectedLayers.isEmpty()) {
|
---|
663 | return Collections.emptyList();
|
---|
664 | }
|
---|
665 | Set<String> proj = new HashSet<>(selectedLayers.get(0).getCrs());
|
---|
666 |
|
---|
667 | // set intersect with all layers
|
---|
668 | for (LayerDetails ld: selectedLayers) {
|
---|
669 | proj.retainAll(ld.getCrs());
|
---|
670 | }
|
---|
671 | return proj;
|
---|
672 | }
|
---|
673 |
|
---|
674 | /**
|
---|
675 | * @param defaultLayers default layers that should select layer object
|
---|
676 | * @return collection of LayerDetails specified by DefaultLayers
|
---|
677 | */
|
---|
678 | public List<LayerDetails> getLayers(List<DefaultLayer> defaultLayers) {
|
---|
679 | Collection<String> layerNames = defaultLayers.stream().map(DefaultLayer::getLayerName).collect(Collectors.toList());
|
---|
680 | return layers.stream()
|
---|
681 | .flatMap(LayerDetails::flattened)
|
---|
682 | .filter(x -> layerNames.contains(x.getName()))
|
---|
683 | .collect(Collectors.toList());
|
---|
684 | }
|
---|
685 |
|
---|
686 | /**
|
---|
687 | * @return title of this service
|
---|
688 | */
|
---|
689 | public String getTitle() {
|
---|
690 | return title;
|
---|
691 | }
|
---|
692 | }
|
---|