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