source: josm/trunk/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java@ 13733

Last change on this file since 13733 was 13733, checked in by wiktorn, 6 years ago

Imagery definition refactor

Extend imagery definitions by:

  • allowing setting default layers for WMS_ENDPOINT and WMTS
  • allowing setting minimum expires time for tile for this imagery
  • allowing setting custom headers that will be sent for all requests

(get map, get capabilities) for this imagery

Additional changes in code:

  • use TileJobOptions to pass miscellaneous options to loaders
  • refactor WMSImagery to use SAX parser

See: #15981, #7953, #16224, #15940, #16249

  • Property svn:eol-style set to native
File size: 47.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GridBagLayout;
7import java.awt.Point;
8import java.io.ByteArrayInputStream;
9import java.io.IOException;
10import java.io.InputStream;
11import java.nio.charset.StandardCharsets;
12import java.util.ArrayList;
13import java.util.Arrays;
14import java.util.Collection;
15import java.util.Collections;
16import java.util.LinkedHashSet;
17import java.util.List;
18import java.util.Map;
19import java.util.Map.Entry;
20import java.util.Objects;
21import java.util.Optional;
22import java.util.SortedSet;
23import java.util.Stack;
24import java.util.TreeSet;
25import java.util.concurrent.ConcurrentHashMap;
26import java.util.regex.Matcher;
27import java.util.regex.Pattern;
28import java.util.stream.Collectors;
29
30import javax.imageio.ImageIO;
31import javax.swing.JPanel;
32import javax.swing.JScrollPane;
33import javax.swing.JTable;
34import javax.swing.ListSelectionModel;
35import javax.swing.table.AbstractTableModel;
36import javax.xml.namespace.QName;
37import javax.xml.stream.XMLStreamException;
38import javax.xml.stream.XMLStreamReader;
39
40import org.openstreetmap.gui.jmapviewer.Coordinate;
41import org.openstreetmap.gui.jmapviewer.Projected;
42import org.openstreetmap.gui.jmapviewer.Tile;
43import org.openstreetmap.gui.jmapviewer.TileRange;
44import org.openstreetmap.gui.jmapviewer.TileXY;
45import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
46import org.openstreetmap.gui.jmapviewer.interfaces.IProjected;
47import org.openstreetmap.gui.jmapviewer.interfaces.TemplatedTileSource;
48import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource;
49import org.openstreetmap.josm.Main;
50import org.openstreetmap.josm.data.ProjectionBounds;
51import org.openstreetmap.josm.data.coor.EastNorth;
52import org.openstreetmap.josm.data.coor.LatLon;
53import org.openstreetmap.josm.data.imagery.GetCapabilitiesParseHelper.TransferMode;
54import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
55import org.openstreetmap.josm.data.projection.Projection;
56import org.openstreetmap.josm.data.projection.Projections;
57import org.openstreetmap.josm.gui.ExtendedDialog;
58import org.openstreetmap.josm.gui.layer.NativeScaleLayer.ScaleList;
59import org.openstreetmap.josm.io.CachedFile;
60import org.openstreetmap.josm.spi.preferences.Config;
61import org.openstreetmap.josm.tools.CheckParameterUtil;
62import org.openstreetmap.josm.tools.GBC;
63import org.openstreetmap.josm.tools.Logging;
64import org.openstreetmap.josm.tools.Utils;
65
66/**
67 * Tile Source handling WMTS providers
68 *
69 * @author Wiktor Niesiobędzki
70 * @since 8526
71 */
72public class WMTSTileSource extends AbstractTMSTileSource implements TemplatedTileSource {
73 /**
74 * WMTS namespace address
75 */
76 public static final String WMTS_NS_URL = "http://www.opengis.net/wmts/1.0";
77
78 // CHECKSTYLE.OFF: SingleSpaceSeparator
79 private static final QName QN_CONTENTS = new QName(WMTSTileSource.WMTS_NS_URL, "Contents");
80 private static final QName QN_DEFAULT = new QName(WMTSTileSource.WMTS_NS_URL, "Default");
81 private static final QName QN_DIMENSION = new QName(WMTSTileSource.WMTS_NS_URL, "Dimension");
82 private static final QName QN_FORMAT = new QName(WMTSTileSource.WMTS_NS_URL, "Format");
83 private static final QName QN_LAYER = new QName(WMTSTileSource.WMTS_NS_URL, "Layer");
84 private static final QName QN_MATRIX_WIDTH = new QName(WMTSTileSource.WMTS_NS_URL, "MatrixWidth");
85 private static final QName QN_MATRIX_HEIGHT = new QName(WMTSTileSource.WMTS_NS_URL, "MatrixHeight");
86 private static final QName QN_RESOURCE_URL = new QName(WMTSTileSource.WMTS_NS_URL, "ResourceURL");
87 private static final QName QN_SCALE_DENOMINATOR = new QName(WMTSTileSource.WMTS_NS_URL, "ScaleDenominator");
88 private static final QName QN_STYLE = new QName(WMTSTileSource.WMTS_NS_URL, "Style");
89 private static final QName QN_TILEMATRIX = new QName(WMTSTileSource.WMTS_NS_URL, "TileMatrix");
90 private static final QName QN_TILEMATRIXSET = new QName(WMTSTileSource.WMTS_NS_URL, "TileMatrixSet");
91 private static final QName QN_TILEMATRIX_SET_LINK = new QName(WMTSTileSource.WMTS_NS_URL, "TileMatrixSetLink");
92 private static final QName QN_TILE_WIDTH = new QName(WMTSTileSource.WMTS_NS_URL, "TileWidth");
93 private static final QName QN_TILE_HEIGHT = new QName(WMTSTileSource.WMTS_NS_URL, "TileHeight");
94 private static final QName QN_TOPLEFT_CORNER = new QName(WMTSTileSource.WMTS_NS_URL, "TopLeftCorner");
95 private static final QName QN_VALUE = new QName(WMTSTileSource.WMTS_NS_URL, "Value");
96 // CHECKSTYLE.ON: SingleSpaceSeparator
97
98 private static final String PATTERN_HEADER = "\\{header\\(([^,]+),([^}]+)\\)\\}";
99
100 private static final String URL_GET_ENCODING_PARAMS = "SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER={layer}&STYLE={style}&"
101 + "FORMAT={format}&tileMatrixSet={TileMatrixSet}&tileMatrix={TileMatrix}&tileRow={TileRow}&tileCol={TileCol}";
102
103 private static final String[] ALL_PATTERNS = {
104 PATTERN_HEADER,
105 };
106
107 private int cachedTileSize = -1;
108
109 private static class TileMatrix {
110 private String identifier;
111 private double scaleDenominator;
112 private EastNorth topLeftCorner;
113 private int tileWidth;
114 private int tileHeight;
115 private int matrixWidth = -1;
116 private int matrixHeight = -1;
117 }
118
119 private static class TileMatrixSetBuilder {
120 // sorted by zoom level
121 SortedSet<TileMatrix> tileMatrix = new TreeSet<>((o1, o2) -> -1 * Double.compare(o1.scaleDenominator, o2.scaleDenominator));
122 private String crs;
123 private String identifier;
124
125 TileMatrixSet build() {
126 return new TileMatrixSet(this);
127 }
128 }
129
130 /**
131 *
132 * class representing WMTS TileMatrixSet
133 * This connects projection and TileMatrix (how the map is divided in tiles)
134 *
135 */
136 public static class TileMatrixSet {
137
138 private final List<TileMatrix> tileMatrix;
139 private final String crs;
140 private final String identifier;
141
142 TileMatrixSet(TileMatrixSet tileMatrixSet) {
143 if (tileMatrixSet != null) {
144 tileMatrix = new ArrayList<>(tileMatrixSet.tileMatrix);
145 crs = tileMatrixSet.crs;
146 identifier = tileMatrixSet.identifier;
147 } else {
148 tileMatrix = Collections.emptyList();
149 crs = null;
150 identifier = null;
151 }
152 }
153
154 TileMatrixSet(TileMatrixSetBuilder builder) {
155 tileMatrix = new ArrayList<>(builder.tileMatrix);
156 crs = builder.crs;
157 identifier = builder.identifier;
158 }
159
160 @Override
161 public String toString() {
162 return "TileMatrixSet [crs=" + crs + ", identifier=" + identifier + ']';
163 }
164
165 /**
166 *
167 * @return identifier of this TileMatrixSet
168 */
169 public String getIdentifier() {
170 return identifier;
171 }
172 }
173
174 private static class Dimension {
175 private String identifier;
176 private String defaultValue;
177 private final List<String> values = new ArrayList<>();
178 }
179
180 /**
181 * Class representing WMTS Layer information
182 *
183 */
184 public static class Layer {
185 private String format;
186 private String identifier;
187 private String title;
188 private TileMatrixSet tileMatrixSet;
189 private String baseUrl;
190 private String style;
191 private final Collection<String> tileMatrixSetLinks = new ArrayList<>();
192 private final Collection<Dimension> dimensions = new ArrayList<>();
193
194 Layer(Layer l) {
195 Objects.requireNonNull(l);
196 format = l.format;
197 identifier = l.identifier;
198 title = l.title;
199 baseUrl = l.baseUrl;
200 style = l.style;
201 tileMatrixSet = new TileMatrixSet(l.tileMatrixSet);
202 dimensions.addAll(l.dimensions);
203 }
204
205 Layer() {
206 }
207
208 /**
209 * Get title of the layer for user display.
210 *
211 * This is either the content of the Title element (if available) or
212 * the layer identifier (as fallback)
213 * @return title of the layer for user display
214 */
215 public String getUserTitle() {
216 return title != null ? title : identifier;
217 }
218
219 @Override
220 public String toString() {
221 return "Layer [identifier=" + identifier + ", title=" + title + ", tileMatrixSet="
222 + tileMatrixSet + ", baseUrl=" + baseUrl + ", style=" + style + ']';
223 }
224
225 /**
226 *
227 * @return identifier of this layer
228 */
229 public String getIdentifier() {
230 return identifier;
231 }
232
233 /**
234 *
235 * @return style of this layer
236 */
237 public String getStyle() {
238 return style;
239 }
240
241 /**
242 *
243 * @return
244 */
245 public TileMatrixSet getTileMatrixSet() {
246 return tileMatrixSet;
247 }
248 }
249
250 /**
251 * Exception thrown when praser doesn't find expected information in GetCapabilities document
252 *
253 */
254 public static class WMTSGetCapabilitiesException extends Exception {
255
256 /**
257 * @param cause description of cause
258 */
259 public WMTSGetCapabilitiesException(String cause) {
260 super(cause);
261 }
262 }
263
264 private static final class SelectLayerDialog extends ExtendedDialog {
265 private final transient List<Entry<String, List<Layer>>> layers;
266 private final JTable list;
267
268 SelectLayerDialog(Collection<Layer> layers) {
269 super(Main.parent, tr("Select WMTS layer"), tr("Add layers"), tr("Cancel"));
270 this.layers = groupLayersByNameAndTileMatrixSet(layers);
271 this.list = getLayerSelectionPanel(this.layers);
272 JPanel panel = new JPanel(new GridBagLayout());
273 panel.add(new JScrollPane(this.list), GBC.eol().fill());
274 setContent(panel);
275 }
276
277 public DefaultLayer getSelectedLayer() {
278 int index = list.getSelectedRow();
279 if (index < 0) {
280 return null; //nothing selected
281 }
282 Layer selectedLayer = layers.get(list.convertRowIndexToModel(index)).getValue().get(0);
283 return new DefaultLayer(ImageryType.WMTS, selectedLayer.identifier, selectedLayer.style, selectedLayer.tileMatrixSet.identifier);
284 }
285
286 }
287
288 private final Map<String, String> headers = new ConcurrentHashMap<>();
289 private final Collection<Layer> layers;
290 private Layer currentLayer;
291 private TileMatrixSet currentTileMatrixSet;
292 private double crsScale;
293 private GetCapabilitiesParseHelper.TransferMode transferMode;
294
295 private ScaleList nativeScaleList;
296
297 private final DefaultLayer defaultLayer;
298
299 private Projection tileProjection;
300
301 /**
302 * Creates a tile source based on imagery info
303 * @param info imagery info
304 * @throws IOException if any I/O error occurs
305 * @throws WMTSGetCapabilitiesException
306 * @throws IllegalArgumentException if any other error happens for the given imagery info
307 */
308 public WMTSTileSource(ImageryInfo info) throws IOException, WMTSGetCapabilitiesException {
309 super(info);
310 CheckParameterUtil.ensureThat(info.getDefaultLayers().size() < 2, "At most 1 default layer for WMTS is supported");
311 this.headers.putAll(info.getCustomHttpHeaders());
312 this.baseUrl = GetCapabilitiesParseHelper.normalizeCapabilitiesUrl(handleTemplate(info.getUrl()));
313 WMTSCapabilities capabilities = getCapabilities(baseUrl, headers);
314 this.layers = capabilities.getLayers();
315 this.baseUrl = capabilities.getBaseUrl();
316 this.transferMode = capabilities.getTransferMode();
317 if (info.getDefaultLayers().isEmpty()) {
318 Logging.warn(tr("No default layer selected, choosing first layer."));
319 if (!layers.isEmpty()) {
320 Layer first = layers.iterator().next();
321 this.defaultLayer = new DefaultLayer(info.getImageryType(), first.identifier, first.style, first.tileMatrixSet.identifier);
322 } else {
323 this.defaultLayer = null;
324 }
325 } else {
326 this.defaultLayer = info.getDefaultLayers().iterator().next();
327 }
328 if (this.layers.isEmpty())
329 throw new IllegalArgumentException(tr("No layers defined by getCapabilities document: {0}", info.getUrl()));
330 }
331
332 /**
333 * Creates a dialog based on this tile source with all available layers and returns the name of selected layer
334 * @return Name of selected layer
335 */
336 public DefaultLayer userSelectLayer() {
337 Map<String, List<Layer>> layerById = layers.stream().collect(
338 Collectors.groupingBy(x -> x.identifier));
339 if (layerById.size() == 1) { // only one layer
340 List<Layer> ls = layerById.entrySet().iterator().next().getValue()
341 .stream().filter(
342 u -> u.tileMatrixSet.crs.equals(Main.getProjection().toCode()))
343 .collect(Collectors.toList());
344 if (ls.size() == 1) {
345 // only one tile matrix set with matching projection - no point in asking
346 Layer selectedLayer = ls.get(0);
347 return new DefaultLayer(ImageryType.WMTS, selectedLayer.identifier, selectedLayer.style, selectedLayer.tileMatrixSet.identifier);
348 }
349 }
350
351 final SelectLayerDialog layerSelection = new SelectLayerDialog(layers);
352 if (layerSelection.showDialog().getValue() == 1) {
353 return layerSelection.getSelectedLayer();
354 }
355 return null;
356 }
357
358 private String handleTemplate(String url) {
359 Pattern pattern = Pattern.compile(PATTERN_HEADER);
360 StringBuffer output = new StringBuffer();
361 Matcher matcher = pattern.matcher(url);
362 while (matcher.find()) {
363 this.headers.put(matcher.group(1), matcher.group(2));
364 matcher.appendReplacement(output, "");
365 }
366 matcher.appendTail(output);
367 return output.toString();
368 }
369
370
371 /**
372 * @param url of the getCapabilities document
373 * @param headers HTTP headers to set when calling getCapabilities url
374 * @return capabilities
375 * @throws IOException in case of any I/O error
376 * @throws WMTSGetCapabilitiesException
377 * @throws IllegalArgumentException in case of any other error
378 */
379 public static WMTSCapabilities getCapabilities(String url, Map<String, String> headers) throws IOException, WMTSGetCapabilitiesException {
380 try (CachedFile cf = new CachedFile(url); InputStream in = cf.setHttpHeaders(headers).
381 setMaxAge(Config.getPref().getLong("wmts.capabilities.cache.max_age", 7 * CachedFile.DAYS)).
382 setCachingStrategy(CachedFile.CachingStrategy.IfModifiedSince).
383 getInputStream()) {
384 byte[] data = Utils.readBytesFromStream(in);
385 if (data.length == 0) {
386 cf.clear();
387 throw new IllegalArgumentException("Could not read data from: " + url);
388 }
389
390 try {
391 XMLStreamReader reader = GetCapabilitiesParseHelper.getReader(new ByteArrayInputStream(data));
392 WMTSCapabilities ret = null;
393 Collection<Layer> layers = null;
394 for (int event = reader.getEventType(); reader.hasNext(); event = reader.next()) {
395 if (event == XMLStreamReader.START_ELEMENT) {
396 if (GetCapabilitiesParseHelper.QN_OWS_OPERATIONS_METADATA.equals(reader.getName())) {
397 ret = parseOperationMetadata(reader);
398 }
399
400 if (QN_CONTENTS.equals(reader.getName())) {
401 layers = parseContents(reader);
402 }
403 }
404 }
405 if (ret == null) {
406 /*
407 * see #12168 - create dummy operation metadata - not all WMTS services provide this information
408 *
409 * WMTS Standard:
410 * > Resource oriented architecture style HTTP encodings SHALL not be described in the OperationsMetadata section.
411 *
412 * And OperationMetada is not mandatory element. So REST mode is justifiable
413 */
414 ret = new WMTSCapabilities(url, TransferMode.REST);
415 }
416 if (layers == null) {
417 throw new WMTSGetCapabilitiesException(tr("WMTS Capabilties document did not contain layers in url: {0}", url));
418 }
419 ret.addLayers(layers);
420 return ret;
421 } catch (XMLStreamException e) {
422 cf.clear();
423 Logging.warn(new String(data, StandardCharsets.UTF_8));
424 throw new IllegalArgumentException(e);
425 }
426 }
427 }
428
429 /**
430 * Parse Contents tag. Returns when reader reaches Contents closing tag
431 *
432 * @param reader StAX reader instance
433 * @return collection of layers within contents with properly linked TileMatrixSets
434 * @throws XMLStreamException See {@link XMLStreamReader}
435 */
436 private static Collection<Layer> parseContents(XMLStreamReader reader) throws XMLStreamException {
437 Map<String, TileMatrixSet> matrixSetById = new ConcurrentHashMap<>();
438 Collection<Layer> layers = new ArrayList<>();
439 for (int event = reader.getEventType();
440 reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && QN_CONTENTS.equals(reader.getName()));
441 event = reader.next()) {
442 if (event == XMLStreamReader.START_ELEMENT) {
443 if (QN_LAYER.equals(reader.getName())) {
444 Layer l = parseLayer(reader);
445 if (l != null) {
446 layers.add(l);
447 }
448 }
449 if (QN_TILEMATRIXSET.equals(reader.getName())) {
450 TileMatrixSet entry = parseTileMatrixSet(reader);
451 matrixSetById.put(entry.identifier, entry);
452 }
453 }
454 }
455 Collection<Layer> ret = new ArrayList<>();
456 // link layers to matrix sets
457 for (Layer l: layers) {
458 for (String tileMatrixId: l.tileMatrixSetLinks) {
459 Layer newLayer = new Layer(l); // create a new layer object for each tile matrix set supported
460 newLayer.tileMatrixSet = matrixSetById.get(tileMatrixId);
461 ret.add(newLayer);
462 }
463 }
464 return ret;
465 }
466
467 /**
468 * Parse Layer tag. Returns when reader will reach Layer closing tag
469 *
470 * @param reader StAX reader instance
471 * @return Layer object, with tileMatrixSetLinks and no tileMatrixSet attribute set.
472 * @throws XMLStreamException See {@link XMLStreamReader}
473 */
474 private static Layer parseLayer(XMLStreamReader reader) throws XMLStreamException {
475 Layer layer = new Layer();
476 Stack<QName> tagStack = new Stack<>();
477 List<String> supportedMimeTypes = new ArrayList<>(Arrays.asList(ImageIO.getReaderMIMETypes()));
478 supportedMimeTypes.add("image/jpgpng"); // used by ESRI
479 supportedMimeTypes.add("image/png8"); // used by geoserver
480 if (supportedMimeTypes.contains("image/jpeg")) {
481 supportedMimeTypes.add("image/jpg"); // sometimes mispelled by Arcgis
482 }
483 Collection<String> unsupportedFormats = new ArrayList<>();
484
485 for (int event = reader.getEventType();
486 reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && QN_LAYER.equals(reader.getName()));
487 event = reader.next()) {
488 if (event == XMLStreamReader.START_ELEMENT) {
489 tagStack.push(reader.getName());
490 if (tagStack.size() == 2) {
491 if (QN_FORMAT.equals(reader.getName())) {
492 String format = reader.getElementText();
493 if (supportedMimeTypes.contains(format)) {
494 layer.format = format;
495 } else {
496 unsupportedFormats.add(format);
497 }
498 } else if (GetCapabilitiesParseHelper.QN_OWS_IDENTIFIER.equals(reader.getName())) {
499 layer.identifier = reader.getElementText();
500 } else if (GetCapabilitiesParseHelper.QN_OWS_TITLE.equals(reader.getName())) {
501 layer.title = reader.getElementText();
502 } else if (QN_RESOURCE_URL.equals(reader.getName()) &&
503 "tile".equals(reader.getAttributeValue("", "resourceType"))) {
504 layer.baseUrl = reader.getAttributeValue("", "template");
505 } else if (QN_STYLE.equals(reader.getName()) &&
506 "true".equals(reader.getAttributeValue("", "isDefault"))) {
507 if (GetCapabilitiesParseHelper.moveReaderToTag(reader, GetCapabilitiesParseHelper.QN_OWS_IDENTIFIER)) {
508 layer.style = reader.getElementText();
509 tagStack.push(reader.getName()); // keep tagStack in sync
510 }
511 } else if (QN_DIMENSION.equals(reader.getName())) {
512 layer.dimensions.add(parseDimension(reader));
513 } else if (QN_TILEMATRIX_SET_LINK.equals(reader.getName())) {
514 layer.tileMatrixSetLinks.add(parseTileMatrixSetLink(reader));
515 } else {
516 GetCapabilitiesParseHelper.moveReaderToEndCurrentTag(reader);
517 }
518 }
519 }
520 // need to get event type from reader, as parsing might have change position of reader
521 if (reader.getEventType() == XMLStreamReader.END_ELEMENT) {
522 QName start = tagStack.pop();
523 if (!start.equals(reader.getName())) {
524 throw new IllegalStateException(tr("WMTS Parser error - start element {0} has different name than end element {2}",
525 start, reader.getName()));
526 }
527 }
528 }
529 if (layer.style == null) {
530 layer.style = "";
531 }
532 if (layer.format == null) {
533 // no format found - it's mandatory parameter - can't use this layer
534 Logging.warn(tr("Can''t use layer {0} because no supported formats where found. Layer is available in formats: {1}",
535 layer.getUserTitle(),
536 String.join(", ", unsupportedFormats)));
537 return null;
538 }
539 return layer;
540 }
541
542 /**
543 * Gets Dimension value. Returns when reader is on Dimension closing tag
544 *
545 * @param reader StAX reader instance
546 * @return dimension
547 * @throws XMLStreamException See {@link XMLStreamReader}
548 */
549 private static Dimension parseDimension(XMLStreamReader reader) throws XMLStreamException {
550 Dimension ret = new Dimension();
551 for (int event = reader.getEventType();
552 reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT &&
553 QN_DIMENSION.equals(reader.getName()));
554 event = reader.next()) {
555 if (event == XMLStreamReader.START_ELEMENT) {
556 if (GetCapabilitiesParseHelper.QN_OWS_IDENTIFIER.equals(reader.getName())) {
557 ret.identifier = reader.getElementText();
558 } else if (QN_DEFAULT.equals(reader.getName())) {
559 ret.defaultValue = reader.getElementText();
560 } else if (QN_VALUE.equals(reader.getName())) {
561 ret.values.add(reader.getElementText());
562 }
563 }
564 }
565 return ret;
566 }
567
568 /**
569 * Gets TileMatrixSetLink value. Returns when reader is on TileMatrixSetLink closing tag
570 *
571 * @param reader StAX reader instance
572 * @return TileMatrixSetLink identifier
573 * @throws XMLStreamException See {@link XMLStreamReader}
574 */
575 private static String parseTileMatrixSetLink(XMLStreamReader reader) throws XMLStreamException {
576 String ret = null;
577 for (int event = reader.getEventType();
578 reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT &&
579 QN_TILEMATRIX_SET_LINK.equals(reader.getName()));
580 event = reader.next()) {
581 if (event == XMLStreamReader.START_ELEMENT && QN_TILEMATRIXSET.equals(reader.getName())) {
582 ret = reader.getElementText();
583 }
584 }
585 return ret;
586 }
587
588 /**
589 * Parses TileMatrixSet section. Returns when reader is on TileMatrixSet closing tag
590 * @param reader StAX reader instance
591 * @return TileMatrixSet object
592 * @throws XMLStreamException See {@link XMLStreamReader}
593 */
594 private static TileMatrixSet parseTileMatrixSet(XMLStreamReader reader) throws XMLStreamException {
595 TileMatrixSetBuilder matrixSet = new TileMatrixSetBuilder();
596 for (int event = reader.getEventType();
597 reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && QN_TILEMATRIXSET.equals(reader.getName()));
598 event = reader.next()) {
599 if (event == XMLStreamReader.START_ELEMENT) {
600 if (GetCapabilitiesParseHelper.QN_OWS_IDENTIFIER.equals(reader.getName())) {
601 matrixSet.identifier = reader.getElementText();
602 }
603 if (GetCapabilitiesParseHelper.QN_OWS_SUPPORTED_CRS.equals(reader.getName())) {
604 matrixSet.crs = GetCapabilitiesParseHelper.crsToCode(reader.getElementText());
605 }
606 if (QN_TILEMATRIX.equals(reader.getName())) {
607 matrixSet.tileMatrix.add(parseTileMatrix(reader, matrixSet.crs));
608 }
609 }
610 }
611 return matrixSet.build();
612 }
613
614 /**
615 * Parses TileMatrix section. Returns when reader is on TileMatrix closing tag.
616 * @param reader StAX reader instance
617 * @param matrixCrs projection used by this matrix
618 * @return TileMatrix object
619 * @throws XMLStreamException See {@link XMLStreamReader}
620 */
621 private static TileMatrix parseTileMatrix(XMLStreamReader reader, String matrixCrs) throws XMLStreamException {
622 Projection matrixProj = Optional.ofNullable(Projections.getProjectionByCode(matrixCrs))
623 .orElseGet(Main::getProjection); // use current projection if none found. Maybe user is using custom string
624 TileMatrix ret = new TileMatrix();
625 for (int event = reader.getEventType();
626 reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && QN_TILEMATRIX.equals(reader.getName()));
627 event = reader.next()) {
628 if (event == XMLStreamReader.START_ELEMENT) {
629 if (GetCapabilitiesParseHelper.QN_OWS_IDENTIFIER.equals(reader.getName())) {
630 ret.identifier = reader.getElementText();
631 }
632 if (QN_SCALE_DENOMINATOR.equals(reader.getName())) {
633 ret.scaleDenominator = Double.parseDouble(reader.getElementText());
634 }
635 if (QN_TOPLEFT_CORNER.equals(reader.getName())) {
636 String[] topLeftCorner = reader.getElementText().split(" ");
637 if (matrixProj.switchXY()) {
638 ret.topLeftCorner = new EastNorth(Double.parseDouble(topLeftCorner[1]), Double.parseDouble(topLeftCorner[0]));
639 } else {
640 ret.topLeftCorner = new EastNorth(Double.parseDouble(topLeftCorner[0]), Double.parseDouble(topLeftCorner[1]));
641 }
642 }
643 if (QN_TILE_HEIGHT.equals(reader.getName())) {
644 ret.tileHeight = Integer.parseInt(reader.getElementText());
645 }
646 if (QN_TILE_WIDTH.equals(reader.getName())) {
647 ret.tileWidth = Integer.parseInt(reader.getElementText());
648 }
649 if (QN_MATRIX_HEIGHT.equals(reader.getName())) {
650 ret.matrixHeight = Integer.parseInt(reader.getElementText());
651 }
652 if (QN_MATRIX_WIDTH.equals(reader.getName())) {
653 ret.matrixWidth = Integer.parseInt(reader.getElementText());
654 }
655 }
656 }
657 if (ret.tileHeight != ret.tileWidth) {
658 throw new AssertionError(tr("Only square tiles are supported. {0}x{1} returned by server for TileMatrix identifier {2}",
659 ret.tileHeight, ret.tileWidth, ret.identifier));
660 }
661 return ret;
662 }
663
664 /**
665 * Parses OperationMetadata section. Returns when reader is on OperationsMetadata closing tag.
666 * return WMTSCapabilities with baseUrl and transferMode
667 *
668 * @param reader StAX reader instance
669 * @return WMTSCapabilities with baseUrl and transferMode set
670 * @throws XMLStreamException See {@link XMLStreamReader}
671 */
672 private static WMTSCapabilities parseOperationMetadata(XMLStreamReader reader) throws XMLStreamException {
673 for (int event = reader.getEventType();
674 reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT &&
675 GetCapabilitiesParseHelper.QN_OWS_OPERATIONS_METADATA.equals(reader.getName()));
676 event = reader.next()) {
677 if (event == XMLStreamReader.START_ELEMENT &&
678 GetCapabilitiesParseHelper.QN_OWS_OPERATION.equals(reader.getName()) &&
679 "GetTile".equals(reader.getAttributeValue("", "name")) &&
680 GetCapabilitiesParseHelper.moveReaderToTag(reader,
681 GetCapabilitiesParseHelper.QN_OWS_DCP,
682 GetCapabilitiesParseHelper.QN_OWS_HTTP,
683 GetCapabilitiesParseHelper.QN_OWS_GET
684 )) {
685 return new WMTSCapabilities(
686 reader.getAttributeValue(GetCapabilitiesParseHelper.XLINK_NS_URL, "href"),
687 GetCapabilitiesParseHelper.getTransferMode(reader)
688 );
689 }
690 }
691 return null;
692 }
693
694 /**
695 * Initializes projection for this TileSource with projection
696 * @param proj projection to be used by this TileSource
697 */
698 public void initProjection(Projection proj) {
699 if (proj.equals(tileProjection))
700 return;
701 List<Layer> matchingLayers = layers.stream().filter(
702 l -> l.identifier.equals(defaultLayer.getLayerName()) && l.tileMatrixSet.crs.equals(proj.toCode()))
703 .collect(Collectors.toList());
704 if (matchingLayers.size() > 1) {
705 this.currentLayer = matchingLayers.stream().filter(
706 l -> l.tileMatrixSet.identifier.equals(defaultLayer.getTileMatrixSet()))
707 .findFirst().orElse(matchingLayers.get(0));
708 this.tileProjection = proj;
709 } else if (matchingLayers.size() == 1) {
710 this.currentLayer = matchingLayers.get(0);
711 this.tileProjection = proj;
712 } else {
713 // no tile matrix sets with current projection
714 if (this.currentLayer == null) {
715 this.tileProjection = null;
716 for (Layer layer : layers) {
717 if (!layer.identifier.equals(defaultLayer.getLayerName())) {
718 continue;
719 }
720 Projection pr = Projections.getProjectionByCode(layer.tileMatrixSet.crs);
721 if (pr != null) {
722 this.currentLayer = layer;
723 this.tileProjection = pr;
724 break;
725 }
726 }
727 if (this.currentLayer == null)
728 throw new IllegalArgumentException(
729 layers.stream().map(l -> l.tileMatrixSet).collect(Collectors.toList()).toString());
730 } // else: keep currentLayer and tileProjection as is
731 }
732 if (this.currentLayer != null) {
733 this.currentTileMatrixSet = this.currentLayer.tileMatrixSet;
734 Collection<Double> scales = new ArrayList<>(currentTileMatrixSet.tileMatrix.size());
735 for (TileMatrix tileMatrix : currentTileMatrixSet.tileMatrix) {
736 scales.add(tileMatrix.scaleDenominator * 0.28e-03);
737 }
738 this.nativeScaleList = new ScaleList(scales);
739 }
740 this.crsScale = getTileSize() * 0.28e-03 / this.tileProjection.getMetersPerUnit();
741 }
742
743 @Override
744 public int getTileSize() {
745 if (cachedTileSize > 0) {
746 return cachedTileSize;
747 }
748 if (currentTileMatrixSet != null) {
749 // no support for non-square tiles (tileHeight != tileWidth)
750 // and for different tile sizes at different zoom levels
751 cachedTileSize = currentTileMatrixSet.tileMatrix.get(0).tileHeight;
752 return cachedTileSize;
753 }
754 // Fallback to default mercator tile size. Maybe it will work
755 Logging.warn("WMTS: Could not determine tile size. Using default tile size of: {0}", getDefaultTileSize());
756 return getDefaultTileSize();
757 }
758
759 @Override
760 public String getTileUrl(int zoom, int tilex, int tiley) {
761 if (currentLayer == null) {
762 return "";
763 }
764
765 String url;
766 if (currentLayer.baseUrl != null && transferMode == null) {
767 url = currentLayer.baseUrl;
768 } else {
769 switch (transferMode) {
770 case KVP:
771 url = baseUrl + URL_GET_ENCODING_PARAMS;
772 break;
773 case REST:
774 url = currentLayer.baseUrl;
775 break;
776 default:
777 url = "";
778 break;
779 }
780 }
781
782 TileMatrix tileMatrix = getTileMatrix(zoom);
783
784 if (tileMatrix == null) {
785 return ""; // no matrix, probably unsupported CRS selected.
786 }
787
788 url = url.replaceAll("\\{layer\\}", this.currentLayer.identifier)
789 .replaceAll("\\{format\\}", this.currentLayer.format)
790 .replaceAll("\\{TileMatrixSet\\}", this.currentTileMatrixSet.identifier)
791 .replaceAll("\\{TileMatrix\\}", tileMatrix.identifier)
792 .replaceAll("\\{TileRow\\}", Integer.toString(tiley))
793 .replaceAll("\\{TileCol\\}", Integer.toString(tilex))
794 .replaceAll("(?i)\\{style\\}", this.currentLayer.style);
795
796 for (Dimension d : currentLayer.dimensions) {
797 url = url.replaceAll("(?i)\\{"+d.identifier+"\\}", d.defaultValue);
798 }
799
800 return url;
801 }
802
803 /**
804 *
805 * @param zoom zoom level
806 * @return TileMatrix that's working on this zoom level
807 */
808 private TileMatrix getTileMatrix(int zoom) {
809 if (zoom > getMaxZoom()) {
810 return null;
811 }
812 if (zoom < 0) {
813 return null;
814 }
815 return this.currentTileMatrixSet.tileMatrix.get(zoom);
816 }
817
818 @Override
819 public double getDistance(double lat1, double lon1, double lat2, double lon2) {
820 throw new UnsupportedOperationException("Not implemented");
821 }
822
823 @Override
824 public ICoordinate tileXYToLatLon(Tile tile) {
825 return tileXYToLatLon(tile.getXtile(), tile.getYtile(), tile.getZoom());
826 }
827
828 @Override
829 public ICoordinate tileXYToLatLon(TileXY xy, int zoom) {
830 return tileXYToLatLon(xy.getXIndex(), xy.getYIndex(), zoom);
831 }
832
833 @Override
834 public ICoordinate tileXYToLatLon(int x, int y, int zoom) {
835 TileMatrix matrix = getTileMatrix(zoom);
836 if (matrix == null) {
837 return CoordinateConversion.llToCoor(tileProjection.getWorldBoundsLatLon().getCenter());
838 }
839 double scale = matrix.scaleDenominator * this.crsScale;
840 EastNorth ret = new EastNorth(matrix.topLeftCorner.east() + x * scale, matrix.topLeftCorner.north() - y * scale);
841 return CoordinateConversion.llToCoor(tileProjection.eastNorth2latlon(ret));
842 }
843
844 @Override
845 public TileXY latLonToTileXY(double lat, double lon, int zoom) {
846 TileMatrix matrix = getTileMatrix(zoom);
847 if (matrix == null) {
848 return new TileXY(0, 0);
849 }
850
851 EastNorth enPoint = tileProjection.latlon2eastNorth(new LatLon(lat, lon));
852 double scale = matrix.scaleDenominator * this.crsScale;
853 return new TileXY(
854 (enPoint.east() - matrix.topLeftCorner.east()) / scale,
855 (matrix.topLeftCorner.north() - enPoint.north()) / scale
856 );
857 }
858
859 @Override
860 public TileXY latLonToTileXY(ICoordinate point, int zoom) {
861 return latLonToTileXY(point.getLat(), point.getLon(), zoom);
862 }
863
864 @Override
865 public int getTileXMax(int zoom) {
866 return getTileXMax(zoom, tileProjection);
867 }
868
869 @Override
870 public int getTileYMax(int zoom) {
871 return getTileYMax(zoom, tileProjection);
872 }
873
874 @Override
875 public Point latLonToXY(double lat, double lon, int zoom) {
876 TileMatrix matrix = getTileMatrix(zoom);
877 if (matrix == null) {
878 return new Point(0, 0);
879 }
880 double scale = matrix.scaleDenominator * this.crsScale;
881 EastNorth point = tileProjection.latlon2eastNorth(new LatLon(lat, lon));
882 return new Point(
883 (int) Math.round((point.east() - matrix.topLeftCorner.east()) / scale),
884 (int) Math.round((matrix.topLeftCorner.north() - point.north()) / scale)
885 );
886 }
887
888 @Override
889 public Point latLonToXY(ICoordinate point, int zoom) {
890 return latLonToXY(point.getLat(), point.getLon(), zoom);
891 }
892
893 @Override
894 public Coordinate xyToLatLon(Point point, int zoom) {
895 return xyToLatLon(point.x, point.y, zoom);
896 }
897
898 @Override
899 public Coordinate xyToLatLon(int x, int y, int zoom) {
900 TileMatrix matrix = getTileMatrix(zoom);
901 if (matrix == null) {
902 return new Coordinate(0, 0);
903 }
904 double scale = matrix.scaleDenominator * this.crsScale;
905 EastNorth ret = new EastNorth(
906 matrix.topLeftCorner.east() + x * scale,
907 matrix.topLeftCorner.north() - y * scale
908 );
909 LatLon ll = tileProjection.eastNorth2latlon(ret);
910 return new Coordinate(ll.lat(), ll.lon());
911 }
912
913 @Override
914 public Map<String, String> getHeaders() {
915 return headers;
916 }
917
918 @Override
919 public int getMaxZoom() {
920 if (this.currentTileMatrixSet != null) {
921 return this.currentTileMatrixSet.tileMatrix.size()-1;
922 }
923 return 0;
924 }
925
926 @Override
927 public String getTileId(int zoom, int tilex, int tiley) {
928 return getTileUrl(zoom, tilex, tiley);
929 }
930
931 /**
932 * Checks if url is acceptable by this Tile Source
933 * @param url URL to check
934 */
935 public static void checkUrl(String url) {
936 CheckParameterUtil.ensureParameterNotNull(url, "url");
937 Matcher m = Pattern.compile("\\{[^}]*\\}").matcher(url);
938 while (m.find()) {
939 boolean isSupportedPattern = false;
940 for (String pattern : ALL_PATTERNS) {
941 if (m.group().matches(pattern)) {
942 isSupportedPattern = true;
943 break;
944 }
945 }
946 if (!isSupportedPattern) {
947 throw new IllegalArgumentException(
948 tr("{0} is not a valid WMS argument. Please check this server URL:\n{1}", m.group(), url));
949 }
950 }
951 }
952
953 public static JTable getLayerSelectionPanel(List<Entry<String, List<Layer>>> layers) {
954 JTable list = new JTable(
955 new AbstractTableModel() {
956 @Override
957 public Object getValueAt(int rowIndex, int columnIndex) {
958 switch (columnIndex) {
959 case 0:
960 return layers.get(rowIndex).getValue()
961 .stream()
962 .map(Layer::getUserTitle)
963 .collect(Collectors.joining(", ")); //this should be only one
964 case 1:
965 return layers.get(rowIndex).getValue()
966 .stream()
967 .map(x -> x.tileMatrixSet.crs)
968 .collect(Collectors.joining(", "));
969 case 2:
970 return layers.get(rowIndex).getValue()
971 .stream()
972 .map(x -> x.tileMatrixSet.identifier)
973 .collect(Collectors.joining(", ")); //this should be only one
974 default:
975 throw new IllegalArgumentException();
976 }
977 }
978
979 @Override
980 public int getRowCount() {
981 return layers.size();
982 }
983
984 @Override
985 public int getColumnCount() {
986 return 3;
987 }
988
989 @Override
990 public String getColumnName(int column) {
991 switch (column) {
992 case 0: return tr("Layer name");
993 case 1: return tr("Projection");
994 case 2: return tr("Matrix set identifier");
995 default:
996 throw new IllegalArgumentException();
997 }
998 }
999 });
1000 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
1001 list.setAutoCreateRowSorter(true);
1002 list.setRowSelectionAllowed(true);
1003 list.setColumnSelectionAllowed(false);
1004 return list;
1005 }
1006
1007 public static List<Entry<String, List<Layer>>> groupLayersByNameAndTileMatrixSet(Collection<Layer> layers) {
1008 Map<String, List<Layer>> layerByName = layers.stream().collect(
1009 Collectors.groupingBy(x -> x.identifier + '\u001c' + x.tileMatrixSet.identifier));
1010 return layerByName.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors.toList());
1011 }
1012
1013
1014 /**
1015 * @return set of projection codes that this TileSource supports
1016 */
1017 public Collection<String> getSupportedProjections() {
1018 Collection<String> ret = new LinkedHashSet<>();
1019 if (currentLayer == null) {
1020 for (Layer layer: this.layers) {
1021 ret.add(layer.tileMatrixSet.crs);
1022 }
1023 } else {
1024 for (Layer layer: this.layers) {
1025 if (currentLayer.identifier.equals(layer.identifier)) {
1026 ret.add(layer.tileMatrixSet.crs);
1027 }
1028 }
1029 }
1030 return ret;
1031 }
1032
1033 private int getTileYMax(int zoom, Projection proj) {
1034 TileMatrix matrix = getTileMatrix(zoom);
1035 if (matrix == null) {
1036 return 0;
1037 }
1038
1039 if (matrix.matrixHeight != -1) {
1040 return matrix.matrixHeight;
1041 }
1042
1043 double scale = matrix.scaleDenominator * this.crsScale;
1044 EastNorth min = matrix.topLeftCorner;
1045 EastNorth max = proj.latlon2eastNorth(proj.getWorldBoundsLatLon().getMax());
1046 return (int) Math.ceil(Math.abs(max.north() - min.north()) / scale);
1047 }
1048
1049 private int getTileXMax(int zoom, Projection proj) {
1050 TileMatrix matrix = getTileMatrix(zoom);
1051 if (matrix == null) {
1052 return 0;
1053 }
1054 if (matrix.matrixWidth != -1) {
1055 return matrix.matrixWidth;
1056 }
1057
1058 double scale = matrix.scaleDenominator * this.crsScale;
1059 EastNorth min = matrix.topLeftCorner;
1060 EastNorth max = proj.latlon2eastNorth(proj.getWorldBoundsLatLon().getMax());
1061 return (int) Math.ceil(Math.abs(max.east() - min.east()) / scale);
1062 }
1063
1064 /**
1065 * Get native scales of tile source.
1066 * @return {@link ScaleList} of native scales
1067 */
1068 public ScaleList getNativeScales() {
1069 return nativeScaleList;
1070 }
1071
1072 /**
1073 * Returns the tile projection.
1074 * @return the tile projection
1075 */
1076 public Projection getTileProjection() {
1077 return tileProjection;
1078 }
1079
1080 @Override
1081 public IProjected tileXYtoProjected(int x, int y, int zoom) {
1082 TileMatrix matrix = getTileMatrix(zoom);
1083 if (matrix == null) {
1084 return new Projected(0, 0);
1085 }
1086 double scale = matrix.scaleDenominator * this.crsScale;
1087 return new Projected(
1088 matrix.topLeftCorner.east() + x * scale,
1089 matrix.topLeftCorner.north() - y * scale);
1090 }
1091
1092 @Override
1093 public TileXY projectedToTileXY(IProjected projected, int zoom) {
1094 TileMatrix matrix = getTileMatrix(zoom);
1095 if (matrix == null) {
1096 return new TileXY(0, 0);
1097 }
1098 double scale = matrix.scaleDenominator * this.crsScale;
1099 return new TileXY(
1100 (projected.getEast() - matrix.topLeftCorner.east()) / scale,
1101 -(projected.getNorth() - matrix.topLeftCorner.north()) / scale);
1102 }
1103
1104 private EastNorth tileToEastNorth(int x, int y, int z) {
1105 return CoordinateConversion.projToEn(this.tileXYtoProjected(x, y, z));
1106 }
1107
1108 private ProjectionBounds getTileProjectionBounds(Tile tile) {
1109 ProjectionBounds pb = new ProjectionBounds(tileToEastNorth(tile.getXtile(), tile.getYtile(), tile.getZoom()));
1110 pb.extend(tileToEastNorth(tile.getXtile() + 1, tile.getYtile() + 1, tile.getZoom()));
1111 return pb;
1112 }
1113
1114 @Override
1115 public boolean isInside(Tile inner, Tile outer) {
1116 ProjectionBounds pbInner = getTileProjectionBounds(inner);
1117 ProjectionBounds pbOuter = getTileProjectionBounds(outer);
1118 // a little tolerance, for when inner tile touches the border of the outer tile
1119 double epsilon = 1e-7 * (pbOuter.maxEast - pbOuter.minEast);
1120 return pbOuter.minEast <= pbInner.minEast + epsilon &&
1121 pbOuter.minNorth <= pbInner.minNorth + epsilon &&
1122 pbOuter.maxEast >= pbInner.maxEast - epsilon &&
1123 pbOuter.maxNorth >= pbInner.maxNorth - epsilon;
1124 }
1125
1126 @Override
1127 public TileRange getCoveringTileRange(Tile tile, int newZoom) {
1128 TileMatrix matrixNew = getTileMatrix(newZoom);
1129 if (matrixNew == null) {
1130 return new TileRange(new TileXY(0, 0), new TileXY(0, 0), newZoom);
1131 }
1132 IProjected p0 = tileXYtoProjected(tile.getXtile(), tile.getYtile(), tile.getZoom());
1133 IProjected p1 = tileXYtoProjected(tile.getXtile() + 1, tile.getYtile() + 1, tile.getZoom());
1134 TileXY tMin = projectedToTileXY(p0, newZoom);
1135 TileXY tMax = projectedToTileXY(p1, newZoom);
1136 // shrink the target tile a little, so we don't get neighboring tiles, that
1137 // share an edge, but don't actually cover the target tile
1138 double epsilon = 1e-7 * (tMax.getX() - tMin.getX());
1139 int minX = (int) Math.floor(tMin.getX() + epsilon);
1140 int minY = (int) Math.floor(tMin.getY() + epsilon);
1141 int maxX = (int) Math.ceil(tMax.getX() - epsilon) - 1;
1142 int maxY = (int) Math.ceil(tMax.getY() - epsilon) - 1;
1143 return new TileRange(new TileXY(minX, minY), new TileXY(maxX, maxY), newZoom);
1144 }
1145
1146 @Override
1147 public String getServerCRS() {
1148 return tileProjection != null ? tileProjection.toCode() : null;
1149 }
1150}
Note: See TracBrowser for help on using the repository browser.