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