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.Dimension;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.Point;
|
---|
9 | import java.io.ByteArrayInputStream;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.io.InputStream;
|
---|
12 | import java.net.MalformedURLException;
|
---|
13 | import java.net.URL;
|
---|
14 | import java.util.ArrayList;
|
---|
15 | import java.util.Collection;
|
---|
16 | import java.util.Comparator;
|
---|
17 | import java.util.HashMap;
|
---|
18 | import java.util.Map;
|
---|
19 | import java.util.Set;
|
---|
20 | import java.util.SortedSet;
|
---|
21 | import java.util.TreeSet;
|
---|
22 | import java.util.concurrent.ConcurrentHashMap;
|
---|
23 | import java.util.regex.Matcher;
|
---|
24 | import java.util.regex.Pattern;
|
---|
25 |
|
---|
26 | import javax.swing.JList;
|
---|
27 | import javax.swing.JPanel;
|
---|
28 | import javax.swing.ListSelectionModel;
|
---|
29 | import javax.xml.namespace.QName;
|
---|
30 | import javax.xml.parsers.DocumentBuilder;
|
---|
31 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
32 | import javax.xml.xpath.XPath;
|
---|
33 | import javax.xml.xpath.XPathConstants;
|
---|
34 | import javax.xml.xpath.XPathExpression;
|
---|
35 | import javax.xml.xpath.XPathExpressionException;
|
---|
36 | import javax.xml.xpath.XPathFactory;
|
---|
37 |
|
---|
38 | import org.openstreetmap.gui.jmapviewer.Coordinate;
|
---|
39 | import org.openstreetmap.gui.jmapviewer.Tile;
|
---|
40 | import org.openstreetmap.gui.jmapviewer.TileXY;
|
---|
41 | import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
|
---|
42 | import org.openstreetmap.gui.jmapviewer.interfaces.TemplatedTileSource;
|
---|
43 | import org.openstreetmap.gui.jmapviewer.tilesources.TMSTileSource;
|
---|
44 | import org.openstreetmap.josm.Main;
|
---|
45 | import org.openstreetmap.josm.data.Bounds;
|
---|
46 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
47 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
48 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
49 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
50 | import org.openstreetmap.josm.io.CachedFile;
|
---|
51 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
52 | import org.openstreetmap.josm.tools.GBC;
|
---|
53 | import org.openstreetmap.josm.tools.Utils;
|
---|
54 | import org.w3c.dom.DOMException;
|
---|
55 | import org.w3c.dom.Document;
|
---|
56 | import org.w3c.dom.Node;
|
---|
57 | import org.w3c.dom.NodeList;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Tile Source handling WMS providers
|
---|
61 | *
|
---|
62 | * @author Wiktor Niesiobędzki
|
---|
63 | * @since 8526
|
---|
64 | */
|
---|
65 | public class WMTSTileSource extends TMSTileSource implements TemplatedTileSource {
|
---|
66 | private static final String PATTERN_HEADER = "\\{header\\(([^,]+),([^}]+)\\)\\}";
|
---|
67 |
|
---|
68 | private static final String URL_GET_ENCODING_PARAMS = "SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER={layer}&STYLE={style}&"
|
---|
69 | + "FORMAT={format}&tileMatrixSet={TileMatrixSet}&tileMatrix={TileMatrix}&tileRow={TileRow}&tileCol={TileCol}";
|
---|
70 |
|
---|
71 | private static final String[] ALL_PATTERNS = {
|
---|
72 | PATTERN_HEADER,
|
---|
73 | };
|
---|
74 |
|
---|
75 | private class TileMatrix {
|
---|
76 | String identifier;
|
---|
77 | double scaleDenominator;
|
---|
78 | EastNorth topLeftCorner;
|
---|
79 | int tileWidth;
|
---|
80 | int tileHeight;
|
---|
81 | }
|
---|
82 |
|
---|
83 | private class TileMatrixSet {
|
---|
84 | SortedSet<TileMatrix> tileMatrix = new TreeSet<>(new Comparator<TileMatrix>() {
|
---|
85 | @Override
|
---|
86 | public int compare(TileMatrix o1, TileMatrix o2) {
|
---|
87 | // reverse the order, so it will be from greatest (lowest zoom level) to lowest value (highest zoom level)
|
---|
88 | return -1 * Double.compare(o1.scaleDenominator, o2.scaleDenominator);
|
---|
89 | }
|
---|
90 | }); // sorted by zoom level
|
---|
91 | String crs;
|
---|
92 | String identifier;
|
---|
93 | }
|
---|
94 |
|
---|
95 | private class Layer {
|
---|
96 | String format;
|
---|
97 | String name;
|
---|
98 | Map<String, TileMatrixSet> tileMatrixSetByCRS = new ConcurrentHashMap<>();
|
---|
99 | public String baseUrl;
|
---|
100 | }
|
---|
101 |
|
---|
102 | private enum TransferMode {
|
---|
103 | KVP("KVP"),
|
---|
104 | REST("RESTful");
|
---|
105 |
|
---|
106 | private final String typeString;
|
---|
107 |
|
---|
108 | private TransferMode(String urlString) {
|
---|
109 | this.typeString = urlString;
|
---|
110 | }
|
---|
111 |
|
---|
112 | private String getTypeString() {
|
---|
113 | return typeString;
|
---|
114 | }
|
---|
115 |
|
---|
116 | private static TransferMode fromString(String s) {
|
---|
117 | for (TransferMode type : TransferMode.values()) {
|
---|
118 | if (type.getTypeString().equals(s)) {
|
---|
119 | return type;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | return null;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | private final class SelectLayerDialog extends ExtendedDialog {
|
---|
127 | private Layer[] layers;
|
---|
128 | private JList<String> list;
|
---|
129 |
|
---|
130 | private SelectLayerDialog(Collection<Layer> layers) {
|
---|
131 | super(Main.parent, tr("Select WMTS layer"), new String[]{tr("Add layers"), tr("Cancel")});
|
---|
132 | this.layers = layers.toArray(new Layer[]{});
|
---|
133 | this.list = new JList<>(getLayerNames(layers));
|
---|
134 | this.list.setPreferredSize(new Dimension(400, 400));
|
---|
135 | this.list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
136 | JPanel panel = new JPanel(new GridBagLayout());
|
---|
137 | panel.add(this.list, GBC.eol().fill());
|
---|
138 | setContent(panel);
|
---|
139 | }
|
---|
140 |
|
---|
141 | private String[] getLayerNames(Collection<Layer> layers) {
|
---|
142 | Collection<String> ret = new ArrayList<>();
|
---|
143 | for (Layer layer: layers) {
|
---|
144 | ret.add(layer.name);
|
---|
145 | }
|
---|
146 | return ret.toArray(new String[]{});
|
---|
147 | }
|
---|
148 |
|
---|
149 | public Layer getSelectedLayer() {
|
---|
150 | int index = list.getSelectedIndex();
|
---|
151 | if (index < 0) {
|
---|
152 | return null; //nothing selected
|
---|
153 | }
|
---|
154 | return layers[index];
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | private Map<String, String> headers = new HashMap<>();
|
---|
159 | private Collection<Layer> layers;
|
---|
160 | private Layer currentLayer;
|
---|
161 | private TileMatrixSet currentTileMatrixSet;
|
---|
162 | private double crsScale;
|
---|
163 | private TransferMode transferMode;
|
---|
164 | private String style = "";
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Creates a tile source based on imagery info
|
---|
168 | * @param info imagery info
|
---|
169 | * @throws IOException if any I/O error occurs
|
---|
170 | */
|
---|
171 | public WMTSTileSource(ImageryInfo info) throws IOException {
|
---|
172 | super(info);
|
---|
173 | this.baseUrl = normalizeCapabilitiesUrl(handleTemplate(info.getUrl()));
|
---|
174 | this.layers = getCapabilities();
|
---|
175 | if (layers.size() > 1) {
|
---|
176 | SelectLayerDialog layerSelection = new SelectLayerDialog(layers);
|
---|
177 | if (layerSelection.showDialog().getValue() == 1) {
|
---|
178 | this.currentLayer = layerSelection.getSelectedLayer();
|
---|
179 | // TODO: save layer information into ImageryInfo / ImageryPreferences
|
---|
180 | } else {
|
---|
181 | throw new IllegalArgumentException(); //user canceled operation
|
---|
182 | }
|
---|
183 | } else if (layers.size() == 1) {
|
---|
184 | this.currentLayer = this.layers.iterator().next();
|
---|
185 | } else {
|
---|
186 | throw new IllegalArgumentException(tr("No layers defined by getCapabilities document: {0}", info.getUrl()));
|
---|
187 | }
|
---|
188 |
|
---|
189 | initProjection();
|
---|
190 | }
|
---|
191 |
|
---|
192 | private String handleTemplate(String url) {
|
---|
193 | Pattern pattern = Pattern.compile(PATTERN_HEADER);
|
---|
194 | StringBuffer output = new StringBuffer();
|
---|
195 | Matcher matcher = pattern.matcher(url);
|
---|
196 | while (matcher.find()) {
|
---|
197 | this.headers.put(matcher.group(1), matcher.group(2));
|
---|
198 | matcher.appendReplacement(output, "");
|
---|
199 | }
|
---|
200 | matcher.appendTail(output);
|
---|
201 | return output.toString();
|
---|
202 | }
|
---|
203 |
|
---|
204 | private Collection<Layer> getCapabilities() throws IOException {
|
---|
205 | DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
---|
206 | builderFactory.setValidating(false);
|
---|
207 | builderFactory.setNamespaceAware(false);
|
---|
208 | DocumentBuilder builder = null;
|
---|
209 | byte[] data = {};
|
---|
210 | InputStream in = new CachedFile(baseUrl).
|
---|
211 | setHttpHeaders(headers).
|
---|
212 | setMaxAge(7 * CachedFile.DAYS).
|
---|
213 | setCachingStrategy(CachedFile.CachingStrategy.IfModifiedSince).
|
---|
214 | getInputStream();
|
---|
215 | try {
|
---|
216 | builder = builderFactory.newDocumentBuilder();
|
---|
217 | data = Utils.readBytesFromStream(in);
|
---|
218 | if (data == null || data.length == 0) {
|
---|
219 | throw new IllegalArgumentException("Could not read data from: " + baseUrl);
|
---|
220 | }
|
---|
221 | Document document = builder.parse(new ByteArrayInputStream(data));
|
---|
222 | Node getTileOperation = getByXpath(document,
|
---|
223 | "/Capabilities/OperationsMetadata/Operation[@name=\"GetTile\"]/DCP/HTTP/Get").item(0);
|
---|
224 | this.baseUrl = getStringByXpath(getTileOperation, "@href");
|
---|
225 | this.transferMode = TransferMode.fromString(getStringByXpath(getTileOperation,
|
---|
226 | "Constraint[@name=\"GetEncoding\"]/AllowedValues/Value"));
|
---|
227 | NodeList layersNodeList = getByXpath(document, "/Capabilities/Contents/Layer");
|
---|
228 | Map<String, TileMatrixSet> matrixSetById = parseMatrices(getByXpath(document, "/Capabilities/Contents/TileMatrixSet"));
|
---|
229 | return parseLayer(layersNodeList, matrixSetById);
|
---|
230 |
|
---|
231 | } catch (Exception e) {
|
---|
232 | Main.error(e);
|
---|
233 | }
|
---|
234 | return null;
|
---|
235 | }
|
---|
236 |
|
---|
237 | private static String normalizeCapabilitiesUrl(String url) throws MalformedURLException {
|
---|
238 | URL inUrl = new URL(url);
|
---|
239 | URL ret = new URL(inUrl.getProtocol(), inUrl.getHost(), inUrl.getPort(), inUrl.getFile());
|
---|
240 | return ret.toExternalForm();
|
---|
241 | }
|
---|
242 |
|
---|
243 | private Collection<Layer> parseLayer(NodeList nodeList, Map<String, TileMatrixSet> matrixSetById) throws XPathExpressionException {
|
---|
244 | Collection<Layer> ret = new ArrayList<>();
|
---|
245 | for (int layerId = 0; layerId < nodeList.getLength(); layerId++) {
|
---|
246 | Node layerNode = nodeList.item(layerId);
|
---|
247 | Layer layer = new Layer();
|
---|
248 | layer.format = getStringByXpath(layerNode, "Format");
|
---|
249 | layer.name = getStringByXpath(layerNode, "Identifier");
|
---|
250 | layer.baseUrl = getStringByXpath(layerNode, "ResourceURL[@resourceType='tile']/@template");
|
---|
251 | NodeList tileMatrixSetLinks = getByXpath(layerNode, "TileMatrixSetLink");
|
---|
252 | for (int tileMatrixId = 0; tileMatrixId < tileMatrixSetLinks.getLength(); tileMatrixId++) {
|
---|
253 | Node tileMatrixLink = tileMatrixSetLinks.item(tileMatrixId);
|
---|
254 | TileMatrixSet tms = matrixSetById.get(getStringByXpath(tileMatrixLink, "TileMatrixSet"));
|
---|
255 | layer.tileMatrixSetByCRS.put(tms.crs, tms);
|
---|
256 | }
|
---|
257 | ret.add(layer);
|
---|
258 | }
|
---|
259 | return ret;
|
---|
260 |
|
---|
261 | }
|
---|
262 |
|
---|
263 | private Map<String, TileMatrixSet> parseMatrices(NodeList nodeList) throws DOMException, XPathExpressionException {
|
---|
264 | Map<String, TileMatrixSet> ret = new ConcurrentHashMap<>();
|
---|
265 | for (int matrixSetId = 0; matrixSetId < nodeList.getLength(); matrixSetId++) {
|
---|
266 | Node matrixSetNode = nodeList.item(matrixSetId);
|
---|
267 | TileMatrixSet matrixSet = new TileMatrixSet();
|
---|
268 | matrixSet.identifier = getStringByXpath(matrixSetNode, "Identifier");
|
---|
269 | matrixSet.crs = crsToCode(getStringByXpath(matrixSetNode, "SupportedCRS"));
|
---|
270 | NodeList tileMatrixList = getByXpath(matrixSetNode, "TileMatrix");
|
---|
271 | for (int matrixId = 0; matrixId < tileMatrixList.getLength(); matrixId++) {
|
---|
272 | Node tileMatrixNode = tileMatrixList.item(matrixId);
|
---|
273 | TileMatrix tileMatrix = new TileMatrix();
|
---|
274 | tileMatrix.identifier = getStringByXpath(tileMatrixNode, "Identifier");
|
---|
275 | tileMatrix.scaleDenominator = Double.parseDouble(getStringByXpath(tileMatrixNode, "ScaleDenominator"));
|
---|
276 | String[] topLeftCorner = getStringByXpath(tileMatrixNode, "TopLeftCorner").split(" ");
|
---|
277 | tileMatrix.topLeftCorner = new EastNorth(Double.parseDouble(topLeftCorner[1]), Double.parseDouble(topLeftCorner[0]));
|
---|
278 | tileMatrix.tileHeight = Integer.parseInt(getStringByXpath(tileMatrixNode, "TileHeight"));
|
---|
279 | tileMatrix.tileWidth = Integer.parseInt(getStringByXpath(tileMatrixNode, "TileHeight"));
|
---|
280 | if (tileMatrix.tileHeight != tileMatrix.tileWidth) {
|
---|
281 | throw new AssertionError(tr("Only square tiles are supported. {0}x{1} returned by server for TileMatrix identifier {2}",
|
---|
282 | tileMatrix.tileHeight, tileMatrix.tileWidth, tileMatrix.identifier));
|
---|
283 | }
|
---|
284 |
|
---|
285 | matrixSet.tileMatrix.add(tileMatrix);
|
---|
286 | }
|
---|
287 | ret.put(matrixSet.identifier, matrixSet);
|
---|
288 | }
|
---|
289 | return ret;
|
---|
290 | }
|
---|
291 |
|
---|
292 | private static String crsToCode(String crsIdentifier) {
|
---|
293 | if (crsIdentifier.startsWith("urn:ogc:def:crs:")) {
|
---|
294 | return crsIdentifier.replaceFirst("urn:ogc:def:crs:([^:]*):[^:]*:(.*)$", "$1:$2");
|
---|
295 | }
|
---|
296 | return crsIdentifier;
|
---|
297 | }
|
---|
298 |
|
---|
299 | private static String getStringByXpath(Node document, String xpathQuery) throws XPathExpressionException {
|
---|
300 | return (String) getByXpath(document, xpathQuery, XPathConstants.STRING);
|
---|
301 | }
|
---|
302 |
|
---|
303 | private static NodeList getByXpath(Node document, String xpathQuery) throws XPathExpressionException {
|
---|
304 | return (NodeList) getByXpath(document, xpathQuery, XPathConstants.NODESET);
|
---|
305 | }
|
---|
306 |
|
---|
307 | private static Object getByXpath(Node document, String xpathQuery, QName returnType) throws XPathExpressionException {
|
---|
308 | XPath xpath = XPathFactory.newInstance().newXPath();
|
---|
309 | XPathExpression expr = xpath.compile(xpathQuery);
|
---|
310 | return expr.evaluate(document, returnType);
|
---|
311 | }
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * Initializes projection for this TileSource with current projection
|
---|
315 | */
|
---|
316 | protected void initProjection() {
|
---|
317 | initProjection(Main.getProjection());
|
---|
318 | }
|
---|
319 |
|
---|
320 | /**
|
---|
321 | * Initializes projection for this TileSource with projection
|
---|
322 | * @param proj projection to be used by this TileSource
|
---|
323 | */
|
---|
324 | public void initProjection(Projection proj) {
|
---|
325 | this.currentTileMatrixSet = currentLayer.tileMatrixSetByCRS.get(proj.toCode());
|
---|
326 | if (this.currentTileMatrixSet == null) {
|
---|
327 | Main.warn("Unsupported CRS selected");
|
---|
328 | // take first, maybe it will work (if user sets custom projections, codes will not match)
|
---|
329 | this.currentTileMatrixSet = currentLayer.tileMatrixSetByCRS.values().iterator().next();
|
---|
330 | }
|
---|
331 | this.crsScale = getTileSize() * 0.28e-03 / proj.getMetersPerUnit();
|
---|
332 | }
|
---|
333 |
|
---|
334 | @Override
|
---|
335 | public int getDefaultTileSize() {
|
---|
336 | return getTileSize();
|
---|
337 | }
|
---|
338 |
|
---|
339 | // FIXME: remove in September 2015, when ImageryPreferenceEntry.tileSize will be initialized to -1 instead to 256
|
---|
340 | // need to leave it as it is to keep compatiblity between tested and latest JOSM versions
|
---|
341 | @Override
|
---|
342 | public int getTileSize() {
|
---|
343 | TileMatrix matrix = getTileMatrix(1);
|
---|
344 | if (matrix == null) {
|
---|
345 | return 1;
|
---|
346 | }
|
---|
347 | return matrix.tileHeight;
|
---|
348 | }
|
---|
349 |
|
---|
350 | @Override
|
---|
351 | public String getTileUrl(int zoom, int tilex, int tiley) throws IOException {
|
---|
352 | String url;
|
---|
353 | switch (transferMode) {
|
---|
354 | case KVP:
|
---|
355 | url = baseUrl + URL_GET_ENCODING_PARAMS;
|
---|
356 | break;
|
---|
357 | case REST:
|
---|
358 | url = currentLayer.baseUrl;
|
---|
359 | break;
|
---|
360 | default:
|
---|
361 | url = "";
|
---|
362 | break;
|
---|
363 | }
|
---|
364 |
|
---|
365 | TileMatrix tileMatrix = getTileMatrix(zoom);
|
---|
366 |
|
---|
367 | if (tileMatrix == null) {
|
---|
368 | return ""; // no matrix, probably unsupported CRS selected.
|
---|
369 | }
|
---|
370 |
|
---|
371 | return url.replaceAll("\\{layer\\}", this.currentLayer.name)
|
---|
372 | .replaceAll("\\{format\\}", this.currentLayer.format)
|
---|
373 | .replaceAll("\\{TileMatrixSet\\}", this.currentTileMatrixSet.identifier)
|
---|
374 | .replaceAll("\\{TileMatrix\\}", tileMatrix.identifier)
|
---|
375 | .replaceAll("\\{TileRow\\}", Integer.toString(tiley))
|
---|
376 | .replaceAll("\\{TileCol\\}", Integer.toString(tilex))
|
---|
377 | .replaceAll("\\{Style\\}", this.style);
|
---|
378 | }
|
---|
379 |
|
---|
380 | /**
|
---|
381 | *
|
---|
382 | * @param zoom zoom level
|
---|
383 | * @return TileMatrix that's working on this zoom level
|
---|
384 | */
|
---|
385 | private TileMatrix getTileMatrix(int zoom) {
|
---|
386 | if (zoom > getMaxZoom()) {
|
---|
387 | return null;
|
---|
388 | }
|
---|
389 | if (zoom < 1) {
|
---|
390 | return null;
|
---|
391 | }
|
---|
392 | return this.currentTileMatrixSet.tileMatrix.toArray(new TileMatrix[]{})[zoom - 1];
|
---|
393 | }
|
---|
394 |
|
---|
395 | @Override
|
---|
396 | public double getDistance(double lat1, double lon1, double lat2, double lon2) {
|
---|
397 | throw new UnsupportedOperationException("Not implemented");
|
---|
398 | }
|
---|
399 |
|
---|
400 | @Override
|
---|
401 | public int lonToX(double lon, int zoom) {
|
---|
402 | throw new UnsupportedOperationException("Not implemented");
|
---|
403 | }
|
---|
404 |
|
---|
405 | @Override
|
---|
406 | public int latToY(double lat, int zoom) {
|
---|
407 | throw new UnsupportedOperationException("Not implemented");
|
---|
408 | }
|
---|
409 |
|
---|
410 | @Override
|
---|
411 | public double XToLon(int x, int zoom) {
|
---|
412 | throw new UnsupportedOperationException("Not implemented");
|
---|
413 | }
|
---|
414 |
|
---|
415 | @Override
|
---|
416 | public double YToLat(int y, int zoom) {
|
---|
417 | throw new UnsupportedOperationException("Not implemented");
|
---|
418 | }
|
---|
419 |
|
---|
420 | @Override
|
---|
421 | public double latToTileY(double lat, int zoom) {
|
---|
422 | throw new UnsupportedOperationException("Not implemented");
|
---|
423 | }
|
---|
424 |
|
---|
425 | @Override
|
---|
426 | public ICoordinate tileXYToLatLon(Tile tile) {
|
---|
427 | return tileXYToLatLon(tile.getXtile(), tile.getYtile(), tile.getZoom());
|
---|
428 | }
|
---|
429 |
|
---|
430 | @Override
|
---|
431 | public ICoordinate tileXYToLatLon(TileXY xy, int zoom) {
|
---|
432 | return tileXYToLatLon(xy.getXIndex(), xy.getYIndex(), zoom);
|
---|
433 | }
|
---|
434 |
|
---|
435 | @Override
|
---|
436 | public ICoordinate tileXYToLatLon(int x, int y, int zoom) {
|
---|
437 | TileMatrix matrix = getTileMatrix(zoom);
|
---|
438 | if (matrix == null) {
|
---|
439 | return Main.getProjection().getWorldBoundsLatLon().getCenter().toCoordinate();
|
---|
440 | }
|
---|
441 | double scale = matrix.scaleDenominator * this.crsScale;
|
---|
442 | EastNorth ret = new EastNorth(matrix.topLeftCorner.getX() + x * scale, matrix.topLeftCorner.getY() - y * scale);
|
---|
443 | return Main.getProjection().eastNorth2latlon(ret).toCoordinate();
|
---|
444 | }
|
---|
445 |
|
---|
446 | @Override
|
---|
447 | public TileXY latLonToTileXY(double lat, double lon, int zoom) {
|
---|
448 | Projection proj = Main.getProjection();
|
---|
449 | EastNorth enPoint = proj.latlon2eastNorth(new LatLon(lat, lon));
|
---|
450 | TileMatrix matrix = getTileMatrix(zoom);
|
---|
451 | if (matrix == null) {
|
---|
452 | return new TileXY(0, 0);
|
---|
453 | }
|
---|
454 | double scale = matrix.scaleDenominator * this.crsScale;
|
---|
455 | return new TileXY(
|
---|
456 | (enPoint.east() - matrix.topLeftCorner.east()) / scale,
|
---|
457 | (matrix.topLeftCorner.north() - enPoint.north()) / scale
|
---|
458 | );
|
---|
459 | }
|
---|
460 |
|
---|
461 | @Override
|
---|
462 | public TileXY latLonToTileXY(ICoordinate point, int zoom) {
|
---|
463 | return latLonToTileXY(point.getLat(), point.getLon(), zoom);
|
---|
464 | }
|
---|
465 |
|
---|
466 | @Override
|
---|
467 | public int getTileXMax(int zoom) {
|
---|
468 | return getTileXMax(zoom, Main.getProjection());
|
---|
469 | }
|
---|
470 |
|
---|
471 | @Override
|
---|
472 | public int getTileXMin(int zoom) {
|
---|
473 | return 0;
|
---|
474 | }
|
---|
475 |
|
---|
476 | @Override
|
---|
477 | public int getTileYMax(int zoom) {
|
---|
478 | return getTileYMax(zoom, Main.getProjection());
|
---|
479 | }
|
---|
480 |
|
---|
481 | @Override
|
---|
482 | public int getTileYMin(int zoom) {
|
---|
483 | return 0;
|
---|
484 | }
|
---|
485 |
|
---|
486 | @Override
|
---|
487 | public Point latLonToXY(double lat, double lon, int zoom) {
|
---|
488 | TileMatrix matrix = getTileMatrix(zoom);
|
---|
489 | if (matrix == null) {
|
---|
490 | return new Point(0, 0);
|
---|
491 | }
|
---|
492 | double scale = matrix.scaleDenominator * this.crsScale;
|
---|
493 | EastNorth point = Main.getProjection().latlon2eastNorth(new LatLon(lat, lon));
|
---|
494 | return new Point(
|
---|
495 | (int) Math.round((point.east() - matrix.topLeftCorner.east()) / scale),
|
---|
496 | (int) Math.round((matrix.topLeftCorner.north() - point.north()) / scale)
|
---|
497 | );
|
---|
498 | }
|
---|
499 |
|
---|
500 | @Override
|
---|
501 | public Point latLonToXY(ICoordinate point, int zoom) {
|
---|
502 | return latLonToXY(point.getLat(), point.getLon(), zoom);
|
---|
503 | }
|
---|
504 |
|
---|
505 | @Override
|
---|
506 | public Coordinate XYToLatLon(Point point, int zoom) {
|
---|
507 | return XYToLatLon(point.x, point.y, zoom);
|
---|
508 | }
|
---|
509 |
|
---|
510 | @Override
|
---|
511 | public Coordinate XYToLatLon(int x, int y, int zoom) {
|
---|
512 | TileMatrix matrix = getTileMatrix(zoom);
|
---|
513 | if (matrix == null) {
|
---|
514 | return new Coordinate(0, 0);
|
---|
515 | }
|
---|
516 | double scale = matrix.scaleDenominator * this.crsScale;
|
---|
517 | Projection proj = Main.getProjection();
|
---|
518 | EastNorth ret = new EastNorth(
|
---|
519 | matrix.topLeftCorner.east() + x * scale,
|
---|
520 | matrix.topLeftCorner.north() - y * scale
|
---|
521 | );
|
---|
522 | LatLon ll = proj.eastNorth2latlon(ret);
|
---|
523 | return new Coordinate(ll.lat(), ll.lon());
|
---|
524 | }
|
---|
525 |
|
---|
526 | @Override
|
---|
527 | public double lonToTileX(double lon, int zoom) {
|
---|
528 | throw new UnsupportedOperationException("Not implemented");
|
---|
529 | }
|
---|
530 |
|
---|
531 | @Override
|
---|
532 | public double tileXToLon(int x, int zoom) {
|
---|
533 | throw new UnsupportedOperationException("Not implemented");
|
---|
534 | }
|
---|
535 |
|
---|
536 | @Override
|
---|
537 | public double tileYToLat(int y, int zoom) {
|
---|
538 | throw new UnsupportedOperationException("Not implemented");
|
---|
539 | }
|
---|
540 |
|
---|
541 | @Override
|
---|
542 | public Map<String, String> getHeaders() {
|
---|
543 | return headers;
|
---|
544 | }
|
---|
545 |
|
---|
546 | @Override
|
---|
547 | public int getMaxZoom() {
|
---|
548 | if (this.currentTileMatrixSet != null) {
|
---|
549 | return this.currentTileMatrixSet.tileMatrix.size();
|
---|
550 | }
|
---|
551 | return 0;
|
---|
552 | }
|
---|
553 |
|
---|
554 | /**
|
---|
555 | * Checks if url is acceptable by this Tile Source
|
---|
556 | * @param url URL to check
|
---|
557 | */
|
---|
558 | public static void checkUrl(String url) {
|
---|
559 | CheckParameterUtil.ensureParameterNotNull(url, "url");
|
---|
560 | Matcher m = Pattern.compile("\\{[^}]*\\}").matcher(url);
|
---|
561 | while (m.find()) {
|
---|
562 | boolean isSupportedPattern = false;
|
---|
563 | for (String pattern : ALL_PATTERNS) {
|
---|
564 | if (m.group().matches(pattern)) {
|
---|
565 | isSupportedPattern = true;
|
---|
566 | break;
|
---|
567 | }
|
---|
568 | }
|
---|
569 | if (!isSupportedPattern) {
|
---|
570 | throw new IllegalArgumentException(
|
---|
571 | tr("{0} is not a valid WMS argument. Please check this server URL:\n{1}", m.group(), url));
|
---|
572 | }
|
---|
573 | }
|
---|
574 | }
|
---|
575 |
|
---|
576 | /**
|
---|
577 | * @return set of projection codes that this TileSource supports
|
---|
578 | */
|
---|
579 | public Set<String> getSupportedProjections() {
|
---|
580 | return this.currentLayer.tileMatrixSetByCRS.keySet();
|
---|
581 | }
|
---|
582 |
|
---|
583 | private int getTileYMax(int zoom, Projection proj) {
|
---|
584 | TileMatrix matrix = getTileMatrix(zoom);
|
---|
585 | if (matrix == null) {
|
---|
586 | return 0;
|
---|
587 | }
|
---|
588 | double scale = matrix.scaleDenominator * this.crsScale;
|
---|
589 | Bounds bounds = Main.getProjection().getWorldBoundsLatLon();
|
---|
590 | EastNorth min = proj.latlon2eastNorth(bounds.getMin());
|
---|
591 | EastNorth max = proj.latlon2eastNorth(bounds.getMax());
|
---|
592 | return (int) Math.ceil(Math.abs(max.getY() - min.getY()) / scale);
|
---|
593 | }
|
---|
594 |
|
---|
595 | private int getTileXMax(int zoom, Projection proj) {
|
---|
596 | TileMatrix matrix = getTileMatrix(zoom);
|
---|
597 | if (matrix == null) {
|
---|
598 | return 0;
|
---|
599 | }
|
---|
600 | double scale = matrix.scaleDenominator * this.crsScale;
|
---|
601 | Bounds bounds = Main.getProjection().getWorldBoundsLatLon();
|
---|
602 | EastNorth min = proj.latlon2eastNorth(bounds.getMin());
|
---|
603 | EastNorth max = proj.latlon2eastNorth(bounds.getMax());
|
---|
604 | return (int) Math.ceil(Math.abs(max.getX() - min.getX()) / scale);
|
---|
605 | }
|
---|
606 | }
|
---|