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