1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.imagery;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertAll;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
---|
8 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
9 | import static org.junit.jupiter.api.Assumptions.assumeFalse;
|
---|
10 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
11 |
|
---|
12 | import java.io.File;
|
---|
13 | import java.io.IOException;
|
---|
14 | import java.lang.reflect.Field;
|
---|
15 | import java.net.MalformedURLException;
|
---|
16 | import java.nio.charset.StandardCharsets;
|
---|
17 | import java.nio.file.Files;
|
---|
18 | import java.nio.file.Paths;
|
---|
19 | import java.util.ArrayList;
|
---|
20 | import java.util.Collections;
|
---|
21 | import java.util.List;
|
---|
22 | import java.util.concurrent.TimeUnit;
|
---|
23 |
|
---|
24 | import org.apache.commons.io.FileUtils;
|
---|
25 | import org.junit.jupiter.api.Disabled;
|
---|
26 | import org.junit.jupiter.api.Test;
|
---|
27 | import org.junit.jupiter.api.Timeout;
|
---|
28 | import org.junit.jupiter.api.io.TempDir;
|
---|
29 | import org.junit.jupiter.params.ParameterizedTest;
|
---|
30 | import org.junit.jupiter.params.provider.ValueSource;
|
---|
31 | import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
|
---|
32 | import org.openstreetmap.gui.jmapviewer.TileXY;
|
---|
33 | import org.openstreetmap.gui.jmapviewer.tilesources.TemplatedTMSTileSource;
|
---|
34 | import org.openstreetmap.josm.TestUtils;
|
---|
35 | import org.openstreetmap.josm.data.Bounds;
|
---|
36 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
37 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
38 | import org.openstreetmap.josm.data.imagery.WMTSTileSource.WMTSGetCapabilitiesException;
|
---|
39 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
---|
40 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
41 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
42 | import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
|
---|
43 | import org.openstreetmap.josm.testutils.annotations.BasicWiremock;
|
---|
44 | import org.openstreetmap.josm.testutils.annotations.Projection;
|
---|
45 | import org.openstreetmap.josm.tools.ReflectionUtils;
|
---|
46 |
|
---|
47 | import com.github.tomakehurst.wiremock.WireMockServer;
|
---|
48 | import com.github.tomakehurst.wiremock.client.WireMock;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Unit tests for class {@link WMTSTileSource}.
|
---|
52 | */
|
---|
53 | @BasicWiremock
|
---|
54 | @BasicPreferences
|
---|
55 | @Projection
|
---|
56 | @Timeout(value = 5, unit = TimeUnit.MINUTES)
|
---|
57 | class WMTSTileSourceTest {
|
---|
58 | @BasicWiremock
|
---|
59 | WireMockServer tileServer;
|
---|
60 |
|
---|
61 | private final ImageryInfo testImageryTMS = new ImageryInfo("test imagery", "http://localhost", "tms", null, null);
|
---|
62 | private final ImageryInfo testImageryPSEUDO_MERCATOR = getImagery(TestUtils.getTestDataRoot() + "wmts/getcapabilities-pseudo-mercator.xml");
|
---|
63 | private final ImageryInfo testImageryTOPO_PL = getImagery(TestUtils.getTestDataRoot() + "wmts/getcapabilities-TOPO.xml");
|
---|
64 | private final ImageryInfo testImageryORTO_PL = getImagery(TestUtils.getTestDataRoot() + "wmts/getcapabilities-ORTO.xml");
|
---|
65 | private final ImageryInfo testImageryWIEN = getImagery(TestUtils.getTestDataRoot() + "wmts/getCapabilities-wien.xml");
|
---|
66 | private final ImageryInfo testImageryWALLONIE = getImagery(TestUtils.getTestDataRoot() + "wmts/WMTSCapabilities-Wallonie.xml");
|
---|
67 | private final ImageryInfo testImageryOntario = getImagery(TestUtils.getTestDataRoot() + "wmts/WMTSCapabilities-Ontario.xml");
|
---|
68 | private final ImageryInfo testImageryGeoAdminCh = getImagery(TestUtils.getTestDataRoot() + "wmts/WMTSCapabilities-GeoAdminCh.xml");
|
---|
69 | private final ImageryInfo testImagery12168 = getImagery(TestUtils.getTestDataRoot() + "wmts/bug12168-WMTSCapabilities.xml");
|
---|
70 | private final ImageryInfo testImageryORT2LT = getImagery(TestUtils.getTestDataRoot() + "wmts/WMTSCapabilities-Lithuania.xml");
|
---|
71 | private final ImageryInfo testLotsOfLayers = getImagery(TestUtils.getTestDataRoot() + "wmts/getCapabilities-lots-of-layers.xml");
|
---|
72 | private final ImageryInfo testDuplicateTags = getImagery(TestUtils.getTestDataRoot() + "wmts/bug12573-wmts-identifier.xml");
|
---|
73 | private final ImageryInfo testMissingStyleIdentifier = getImagery(TestUtils.getTestDataRoot() +
|
---|
74 | "wmts/bug12573-wmts-missing-style-identifier.xml");
|
---|
75 | private final ImageryInfo testMultipleTileMatrixForLayer = getImagery(TestUtils.getTestDataRoot() +
|
---|
76 | "wmts/bug13975-multiple-tile-matrices-for-one-layer-projection.xml");
|
---|
77 | private final ImageryInfo testImageryGisKtnGvAt = getImagery(TestUtils.getTestDataRoot() + "wmts/gis.ktn.gv.at.xml");
|
---|
78 |
|
---|
79 | private static ImageryInfo getImagery(String path) {
|
---|
80 | try {
|
---|
81 | ImageryInfo ret = new ImageryInfo(
|
---|
82 | "test",
|
---|
83 | new File(path).toURI().toURL().toString()
|
---|
84 | );
|
---|
85 | ret.setImageryType(ImageryType.WMTS);
|
---|
86 | return ret;
|
---|
87 | } catch (MalformedURLException e) {
|
---|
88 | e.printStackTrace();
|
---|
89 | return null;
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Test
|
---|
94 | void testPseudoMercator() throws IOException, WMTSGetCapabilitiesException {
|
---|
95 | ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:3857"));
|
---|
96 | WMTSTileSource testSource = new WMTSTileSource(testImageryPSEUDO_MERCATOR);
|
---|
97 | testSource.initProjection(ProjectionRegistry.getProjection());
|
---|
98 |
|
---|
99 | verifyMercatorTile(testSource, 0, 0, 1);
|
---|
100 | verifyMercatorTile(testSource, 0, 0, 2);
|
---|
101 | verifyMercatorTile(testSource, 1, 1, 2);
|
---|
102 | for (int x = 0; x < 4; x++) {
|
---|
103 | for (int y = 0; y < 4; y++) {
|
---|
104 | verifyMercatorTile(testSource, x, y, 3);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | for (int x = 0; x < 8; x++) {
|
---|
108 | for (int y = 0; y < 4; y++) {
|
---|
109 | verifyMercatorTile(testSource, x, y, 4);
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | verifyMercatorTile(testSource, 2 << 9 - 1, 2 << 8 - 1, 10);
|
---|
114 |
|
---|
115 | assertEquals(1, testSource.getTileXMax(0), "TileXMax");
|
---|
116 | assertEquals(1, testSource.getTileYMax(0), "TileYMax");
|
---|
117 | assertEquals(2, testSource.getTileXMax(1), "TileXMax");
|
---|
118 | assertEquals(2, testSource.getTileYMax(1), "TileYMax");
|
---|
119 | assertEquals(4, testSource.getTileXMax(2), "TileXMax");
|
---|
120 | assertEquals(4, testSource.getTileYMax(2), "TileYMax");
|
---|
121 | }
|
---|
122 |
|
---|
123 | @Test
|
---|
124 | void testWALLONIE() throws IOException, WMTSGetCapabilitiesException {
|
---|
125 | ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:31370"));
|
---|
126 | WMTSTileSource testSource = new WMTSTileSource(testImageryWALLONIE);
|
---|
127 | testSource.initProjection(ProjectionRegistry.getProjection());
|
---|
128 |
|
---|
129 | assertEquals("http://geoservices.wallonie.be/arcgis/rest/services/DONNEES_BASE/FOND_PLAN_ANNOTATIONS_2012_RW_NB/"
|
---|
130 | + "MapServer/WMTS/tile/1.0.0/DONNEES_BASE_FOND_PLAN_ANNOTATIONS_2012_RW_NB/default/default028mm/5/1219/1063.png",
|
---|
131 | testSource.getTileUrl(5, 1063, 1219));
|
---|
132 |
|
---|
133 | // +bounds=2.54,49.51,6.4,51.5
|
---|
134 | Bounds wallonieBounds = new Bounds(
|
---|
135 | new LatLon(49.485372459967245, 2.840548314430268),
|
---|
136 | new LatLon(50.820959517561256, 6.427849693016202)
|
---|
137 | );
|
---|
138 | verifyBounds(wallonieBounds, testSource, 5, 1063, 1219);
|
---|
139 | verifyBounds(wallonieBounds, testSource, 10, 17724, 20324);
|
---|
140 | }
|
---|
141 |
|
---|
142 | @Test
|
---|
143 | @Disabled("disable this test, needs further working") // XXX
|
---|
144 | void testWALLONIENoMatrixDimension() throws IOException, WMTSGetCapabilitiesException {
|
---|
145 | ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:31370"));
|
---|
146 | WMTSTileSource testSource = new WMTSTileSource(getImagery("test/data/wmts/WMTSCapabilities-Wallonie-nomatrixdimension.xml"));
|
---|
147 | testSource.initProjection(ProjectionRegistry.getProjection());
|
---|
148 |
|
---|
149 | Bounds wallonieBounds = new Bounds(
|
---|
150 | new LatLon(49.485372459967245, 2.840548314430268),
|
---|
151 | new LatLon(50.820959517561256, 6.427849693016202)
|
---|
152 | );
|
---|
153 |
|
---|
154 | verifyBounds(wallonieBounds, testSource, 6, 1063, 1219);
|
---|
155 | verifyBounds(wallonieBounds, testSource, 11, 17724, 20324);
|
---|
156 | }
|
---|
157 |
|
---|
158 | private void verifyBounds(Bounds bounds, WMTSTileSource testSource, int z, int x, int y) {
|
---|
159 | LatLon ret = CoordinateConversion.coorToLL(testSource.tileXYToLatLon(x, y, z));
|
---|
160 | assertTrue(bounds.contains(ret), ret.toDisplayString() + " doesn't lie within: " + bounds);
|
---|
161 | int tileXmax = testSource.getTileXMax(z);
|
---|
162 | int tileYmax = testSource.getTileYMax(z);
|
---|
163 | assertTrue(tileXmax >= x, "tile x: " + x + " is greater than allowed max: " + tileXmax);
|
---|
164 | assertTrue(tileYmax >= y, "tile y: " + y + " is greater than allowed max: " + tileYmax);
|
---|
165 | }
|
---|
166 |
|
---|
167 | @Test
|
---|
168 | void testWIEN() throws IOException, WMTSGetCapabilitiesException {
|
---|
169 | ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:3857"));
|
---|
170 | WMTSTileSource testSource = new WMTSTileSource(testImageryWIEN);
|
---|
171 | testSource.initProjection(ProjectionRegistry.getProjection());
|
---|
172 | int zoomOffset = 10;
|
---|
173 |
|
---|
174 | verifyMercatorTile(testSource, 0, 0, 1, zoomOffset);
|
---|
175 | verifyMercatorTile(testSource, 1105, 709, 2, zoomOffset);
|
---|
176 | verifyMercatorTile(testSource, 1, 1, 1, zoomOffset);
|
---|
177 | verifyMercatorTile(testSource, 2, 2, 1, zoomOffset);
|
---|
178 | verifyMercatorTile(testSource, 0, 0, 2, zoomOffset);
|
---|
179 | verifyMercatorTile(testSource, 1, 1, 2, zoomOffset);
|
---|
180 |
|
---|
181 | for (int x = 0; x < 4; x++) {
|
---|
182 | for (int y = 0; y < 4; y++) {
|
---|
183 | verifyMercatorTile(testSource, x, y, 3, zoomOffset);
|
---|
184 | }
|
---|
185 | }
|
---|
186 | for (int x = 0; x < 8; x++) {
|
---|
187 | for (int y = 0; y < 4; y++) {
|
---|
188 | verifyMercatorTile(testSource, x, y, 4, zoomOffset);
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | verifyMercatorTile(testSource, 2 << 9 - 1, 2 << 8 - 1, 2, zoomOffset);
|
---|
193 |
|
---|
194 | verifyMercatorMax(testSource, 1, zoomOffset);
|
---|
195 | verifyMercatorMax(testSource, 2, zoomOffset);
|
---|
196 | verifyMercatorMax(testSource, 3, zoomOffset);
|
---|
197 | }
|
---|
198 |
|
---|
199 | private void verifyMercatorMax(WMTSTileSource testSource, int zoom, int zoomOffset) {
|
---|
200 | TemplatedTMSTileSource verifier = new TemplatedTMSTileSource(testImageryTMS);
|
---|
201 | int result = testSource.getTileXMax(zoom);
|
---|
202 | int expected = verifier.getTileXMax(zoom + zoomOffset);
|
---|
203 | assertTrue(Math.abs(result - expected) < 5, "TileXMax expected: " + expected + " got: " + result);
|
---|
204 | result = testSource.getTileYMax(zoom);
|
---|
205 | expected = verifier.getTileYMax(zoom + zoomOffset);
|
---|
206 | assertTrue(Math.abs(result - expected) < 5, "TileYMax expected: " + expected + " got: " + result);
|
---|
207 | }
|
---|
208 |
|
---|
209 | @Test
|
---|
210 | void testGeoportalTOPOPL() throws IOException, WMTSGetCapabilitiesException {
|
---|
211 | ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:4326"));
|
---|
212 | WMTSTileSource testSource = new WMTSTileSource(testImageryTOPO_PL);
|
---|
213 | testSource.initProjection(ProjectionRegistry.getProjection());
|
---|
214 | verifyTile(new LatLon(56, 12), testSource, 0, 0, 1);
|
---|
215 | verifyTile(new LatLon(56, 12), testSource, 0, 0, 2);
|
---|
216 | verifyTile(new LatLon(51.13231917844218, 16.867680821557823), testSource, 1, 1, 1);
|
---|
217 |
|
---|
218 | assertEquals(2, testSource.getTileXMax(0), "TileXMax");
|
---|
219 | assertEquals(1, testSource.getTileYMax(0), "TileYMax");
|
---|
220 | assertEquals(3, testSource.getTileXMax(1), "TileXMax");
|
---|
221 | assertEquals(2, testSource.getTileYMax(1), "TileYMax");
|
---|
222 | assertEquals(6, testSource.getTileXMax(2), "TileXMax");
|
---|
223 | assertEquals(4, testSource.getTileYMax(2), "TileYMax");
|
---|
224 | assertEquals(
|
---|
225 | "http://mapy.geoportal.gov.pl/wss/service/WMTS/guest/wmts/TOPO?SERVICE=WMTS&REQUEST=GetTile&"
|
---|
226 | + "VERSION=1.0.0&LAYER=MAPA TOPOGRAFICZNA&STYLE=default&FORMAT=image/jpeg&tileMatrixSet=EPSG:4326&"
|
---|
227 | + "tileMatrix=EPSG:4326:0&tileRow=1&tileCol=1",
|
---|
228 | testSource.getTileUrl(0, 1, 1));
|
---|
229 | }
|
---|
230 |
|
---|
231 | @Test
|
---|
232 | void testGeoportalORTOPL4326() throws IOException, WMTSGetCapabilitiesException {
|
---|
233 | ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:4326"));
|
---|
234 | WMTSTileSource testSource = new WMTSTileSource(testImageryORTO_PL);
|
---|
235 | testSource.initProjection(ProjectionRegistry.getProjection());
|
---|
236 | verifyTile(new LatLon(53.60205873528009, 19.552206794646956), testSource, 12412, 3941, 13);
|
---|
237 | verifyTile(new LatLon(49.79005619189761, 22.778262259134397), testSource, 17714, 10206, 13);
|
---|
238 | }
|
---|
239 |
|
---|
240 | @Test
|
---|
241 | void testGeoportalORTOPL2180() throws IOException, WMTSGetCapabilitiesException {
|
---|
242 | ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:2180"));
|
---|
243 | WMTSTileSource testSource = new WMTSTileSource(testImageryORTO_PL);
|
---|
244 | testSource.initProjection(ProjectionRegistry.getProjection());
|
---|
245 |
|
---|
246 | verifyTile(new LatLon(53.59940948387726, 19.560544913270064), testSource, 6453, 3140, 13);
|
---|
247 | verifyTile(new LatLon(49.782984840526055, 22.790064966993445), testSource, 9932, 9305, 13);
|
---|
248 | }
|
---|
249 |
|
---|
250 | @Test
|
---|
251 | void testTicket12168() throws IOException, WMTSGetCapabilitiesException {
|
---|
252 | ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:3857"));
|
---|
253 | WMTSTileSource testSource = new WMTSTileSource(testImagery12168);
|
---|
254 | testSource.initProjection(ProjectionRegistry.getProjection());
|
---|
255 | assertEquals(
|
---|
256 | "http://www.ngi.be/cartoweb/1.0.0/topo/default/3857/7/1/1.png",
|
---|
257 | testSource.getTileUrl(0, 1, 1));
|
---|
258 | }
|
---|
259 |
|
---|
260 | @Test
|
---|
261 | void testProjectionWithENUAxis() throws IOException, WMTSGetCapabilitiesException {
|
---|
262 | ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:3346"));
|
---|
263 | WMTSTileSource testSource = new WMTSTileSource(testImageryORT2LT);
|
---|
264 | testSource.initProjection(ProjectionRegistry.getProjection());
|
---|
265 | TileXY tileXY0 = testSource.latLonToTileXY(55.31083718860799, 22.172052608196587, 0);
|
---|
266 | double delta = 1e-10;
|
---|
267 | assertEquals(27.09619727782481, tileXY0.getX(), delta);
|
---|
268 | assertEquals(19.03524443532604, tileXY0.getY(), delta);
|
---|
269 | TileXY tileXY2 = testSource.latLonToTileXY(55.31083718860799, 22.172052608196587, 2);
|
---|
270 | assertEquals(81.28859183347444, tileXY2.getX(), delta);
|
---|
271 | assertEquals(57.10573330597811, tileXY2.getY(), delta);
|
---|
272 | }
|
---|
273 |
|
---|
274 | @Test
|
---|
275 | void testTwoTileSetsForOneProjection() throws Exception {
|
---|
276 | ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:3857"));
|
---|
277 | ImageryInfo ontario = getImagery(TestUtils.getTestDataRoot() + "wmts/WMTSCapabilities-Ontario.xml");
|
---|
278 | ontario.setDefaultLayers(Collections.singletonList(new DefaultLayer(ImageryType.WMTS, "Basemap_Imagery_2014", null, "default028mm")));
|
---|
279 | WMTSTileSource testSource = new WMTSTileSource(ontario);
|
---|
280 | testSource.initProjection(ProjectionRegistry.getProjection());
|
---|
281 | assertEquals(
|
---|
282 | "http://maps.ottawa.ca/arcgis/rest/services/Basemap_Imagery_2014/MapServer/WMTS/tile/1.0.0/Basemap_Imagery_2014/default/"
|
---|
283 | + "default028mm/4/2932/2371.jpg",
|
---|
284 | testSource.getTileUrl(4, 2371, 2932));
|
---|
285 | verifyTile(new LatLon(45.4601306, -75.7617187), testSource, 2372, 2932, 4);
|
---|
286 | verifyTile(new LatLon(45.4602510, -75.7617187), testSource, 607232, 750591, 12);
|
---|
287 | }
|
---|
288 |
|
---|
289 | @Test
|
---|
290 | void testTwoTileSetsForOneProjectionSecondLayer() throws Exception {
|
---|
291 | ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:3857"));
|
---|
292 | ImageryInfo ontario = getImagery(TestUtils.getTestDataRoot() + "wmts/WMTSCapabilities-Ontario.xml");
|
---|
293 | ontario.setDefaultLayers(Collections.singletonList(
|
---|
294 | new DefaultLayer(ImageryType.WMTS, "Basemap_Imagery_2014", null, "GoogleMapsCompatible")
|
---|
295 | ));
|
---|
296 | WMTSTileSource testSource = new WMTSTileSource(ontario);
|
---|
297 | testSource.initProjection(ProjectionRegistry.getProjection());
|
---|
298 | assertEquals(
|
---|
299 | "http://maps.ottawa.ca/arcgis/rest/services/Basemap_Imagery_2014/MapServer/WMTS/tile/1.0.0/Basemap_Imagery_2014/default/"
|
---|
300 | + "GoogleMapsCompatible/4/2932/2371.jpg",
|
---|
301 | testSource.getTileUrl(4, 2371, 2932));
|
---|
302 | verifyMercatorTile(testSource, 74, 91, 8);
|
---|
303 | verifyMercatorTile(testSource, 37952, 46912, 17);
|
---|
304 | }
|
---|
305 |
|
---|
306 | @Test
|
---|
307 | void testManyLayersScrollbars() throws Exception {
|
---|
308 | ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:3857"));
|
---|
309 | WMTSTileSource testSource = new WMTSTileSource(testLotsOfLayers);
|
---|
310 | testSource.initProjection(ProjectionRegistry.getProjection());
|
---|
311 | }
|
---|
312 |
|
---|
313 | @Test
|
---|
314 | void testParserForDuplicateTags() throws Exception {
|
---|
315 | ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:3857"));
|
---|
316 | WMTSTileSource testSource = new WMTSTileSource(testDuplicateTags);
|
---|
317 | testSource.initProjection(ProjectionRegistry.getProjection());
|
---|
318 | assertEquals(
|
---|
319 | "http://tile.informatievlaanderen.be/ws/raadpleegdiensten/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=grb_bsk&"
|
---|
320 | + "STYLE=&FORMAT=image/png&tileMatrixSet=GoogleMapsVL&tileMatrix=1&tileRow=1&tileCol=1",
|
---|
321 | testSource.getTileUrl(1, 1, 1)
|
---|
322 | );
|
---|
323 | }
|
---|
324 |
|
---|
325 | @Test
|
---|
326 | void testParserForMissingStyleIdentifier() throws Exception {
|
---|
327 | ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:3857"));
|
---|
328 | WMTSTileSource testSource = new WMTSTileSource(testMissingStyleIdentifier);
|
---|
329 | testSource.initProjection(ProjectionRegistry.getProjection());
|
---|
330 | }
|
---|
331 |
|
---|
332 | @Test
|
---|
333 | void testForMultipleTileMatricesForOneLayerProjection() throws Exception {
|
---|
334 | ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:3857"));
|
---|
335 | ImageryInfo copy = new ImageryInfo(testMultipleTileMatrixForLayer);
|
---|
336 | List<DefaultLayer> defaultLayers = new ArrayList<>(1);
|
---|
337 | defaultLayers.add(new DefaultLayer(ImageryType.WMTS, "Mashhad_BaseMap_1", null, "default028mm"));
|
---|
338 | copy.setDefaultLayers(defaultLayers);
|
---|
339 | WMTSTileSource testSource = new WMTSTileSource(copy);
|
---|
340 | testSource.initProjection(ProjectionRegistry.getProjection());
|
---|
341 | assertEquals(
|
---|
342 | "http://188.253.0.155:6080/arcgis/rest/services/Mashhad_BaseMap_1/MapServer/WMTS/tile/1.0.0/Mashhad_BaseMap_1"
|
---|
343 | + "/default/default028mm/1/3/2",
|
---|
344 | testSource.getTileUrl(1, 2, 3)
|
---|
345 | );
|
---|
346 | }
|
---|
347 |
|
---|
348 | /**
|
---|
349 | * Test WMTS dimension.
|
---|
350 | * @throws IOException if any I/O error occurs
|
---|
351 | * @throws WMTSGetCapabilitiesException if any error occurs
|
---|
352 | */
|
---|
353 | @Test
|
---|
354 | void testDimension() throws IOException, WMTSGetCapabilitiesException {
|
---|
355 | ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:21781"));
|
---|
356 | ImageryInfo info = new ImageryInfo(testImageryGeoAdminCh);
|
---|
357 | List<DefaultLayer> defaultLayers = new ArrayList<>(1);
|
---|
358 | defaultLayers.add(new DefaultLayer(ImageryType.WMTS, "ch.are.agglomerationen_isolierte_staedte", null, "21781_26"));
|
---|
359 | info.setDefaultLayers(defaultLayers);
|
---|
360 | WMTSTileSource testSource = new WMTSTileSource(info);
|
---|
361 | testSource.initProjection(ProjectionRegistry.getProjection());
|
---|
362 | assertEquals(
|
---|
363 | "http://wmts.geo.admin.ch/1.0.0/ch.are.agglomerationen_isolierte_staedte/default/20140101/21781/1/3/2.png",
|
---|
364 | testSource.getTileUrl(1, 2, 3)
|
---|
365 | );
|
---|
366 | }
|
---|
367 |
|
---|
368 | @Test
|
---|
369 | void testDefaultLayer() throws Exception {
|
---|
370 | // https://gibs.earthdata.nasa.gov/wmts/epsg3857/best/1.0.0/WMTSCapabilities.xml
|
---|
371 | // do not use withFileBody as it needs different directory layout :(
|
---|
372 |
|
---|
373 | tileServer.stubFor(
|
---|
374 | WireMock.get("/getcapabilities.xml")
|
---|
375 | .willReturn(
|
---|
376 | WireMock.aResponse()
|
---|
377 | .withBody(Files.readAllBytes(
|
---|
378 | Paths.get(TestUtils.getTestDataRoot() + "wmts/getCapabilities-lots-of-layers.xml"))
|
---|
379 | )
|
---|
380 | )
|
---|
381 | );
|
---|
382 |
|
---|
383 | tileServer.stubFor(
|
---|
384 | WireMock.get("//maps")
|
---|
385 | .willReturn(
|
---|
386 | WireMock.aResponse().withBody(
|
---|
387 | "<?xml version='1.0' encoding='UTF-8'?>\n" +
|
---|
388 | "<imagery xmlns=\"http://josm.openstreetmap.de/maps-1.0\">\n" +
|
---|
389 | "<entry>\n" +
|
---|
390 | "<name>Landsat</name>\n" +
|
---|
391 | "<id>landsat</id>\n" +
|
---|
392 | "<type>wmts</type>\n" +
|
---|
393 | "<category>photo</category>\n" +
|
---|
394 | "<url><![CDATA[" + tileServer.url("/getcapabilities.xml") + "]]></url>\n" +
|
---|
395 | "<default-layers>" +
|
---|
396 | "<layer name=\"GEOGRAPHICALGRIDSYSTEMS.MAPS\" />" +
|
---|
397 | "</default-layers>" +
|
---|
398 | "</entry>\n" +
|
---|
399 | "</imagery>"
|
---|
400 | )));
|
---|
401 |
|
---|
402 | Config.getPref().putList("imagery.layers.sites", Collections.singletonList(tileServer.url("//maps")));
|
---|
403 | ImageryLayerInfo.instance.loadDefaults(true, null, false);
|
---|
404 |
|
---|
405 | assertEquals(1, ImageryLayerInfo.instance.getDefaultLayers().size());
|
---|
406 | ImageryInfo wmtsImageryInfo = ImageryLayerInfo.instance.getDefaultLayers().get(0);
|
---|
407 | assertEquals(1, wmtsImageryInfo.getDefaultLayers().size());
|
---|
408 | assertEquals("GEOGRAPHICALGRIDSYSTEMS.MAPS", wmtsImageryInfo.getDefaultLayers().get(0).getLayerName());
|
---|
409 | WMTSTileSource tileSource = new WMTSTileSource(wmtsImageryInfo);
|
---|
410 | tileSource.initProjection(Projections.getProjectionByCode("EPSG:3857"));
|
---|
411 | assertEquals("http://wxs.ign.fr/61fs25ymczag0c67naqvvmap/geoportail/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&"
|
---|
412 | + "LAYER=GEOGRAPHICALGRIDSYSTEMS.MAPS"
|
---|
413 | + "&STYLE=normal&FORMAT=image/jpeg&tileMatrixSet=PM&tileMatrix=1&tileRow=1&tileCol=1", tileSource.getTileUrl(1, 1, 1));
|
---|
414 |
|
---|
415 | }
|
---|
416 |
|
---|
417 | private void verifyTile(LatLon expected, WMTSTileSource source, int x, int y, int z) {
|
---|
418 | LatLon ll = CoordinateConversion.coorToLL(source.tileXYToLatLon(x, y, z));
|
---|
419 | assertEquals(expected.lat(), ll.lat(), 1e-05, "Latitude");
|
---|
420 | assertEquals(expected.lon(), ll.lon(), 1e-05, "Longitude");
|
---|
421 | }
|
---|
422 |
|
---|
423 | private void verifyMercatorTile(WMTSTileSource testSource, int x, int y, int z) {
|
---|
424 | verifyMercatorTile(testSource, x, y, z, 0);
|
---|
425 | }
|
---|
426 |
|
---|
427 | private void verifyMercatorTile(WMTSTileSource testSource, int x, int y, int z, int zoomOffset) {
|
---|
428 | TemplatedTMSTileSource verifier = new TemplatedTMSTileSource(testImageryTMS);
|
---|
429 | LatLon result = CoordinateConversion.coorToLL(testSource.tileXYToLatLon(x, y, z));
|
---|
430 | LatLon expected = CoordinateConversion.coorToLL(verifier.tileXYToLatLon(x, y, z + zoomOffset));
|
---|
431 | assertEquals(0.0, LatLon.normalizeLon(expected.lon() - result.lon()), 1e-04, "Longitude");
|
---|
432 | assertEquals(expected.lat(), result.lat(), 1e-04, "Latitude");
|
---|
433 | }
|
---|
434 |
|
---|
435 | @Test
|
---|
436 | void testGisKtnGvAt() throws IOException, WMTSGetCapabilitiesException {
|
---|
437 | ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:31258"));
|
---|
438 | final WMTSTileSource source = new WMTSTileSource(testImageryGisKtnGvAt);
|
---|
439 | source.initProjection(ProjectionRegistry.getProjection());
|
---|
440 | final TileXY tile = source.latLonToTileXY(46.6103, 13.8558, 11);
|
---|
441 | assertEquals("https://gis.ktn.gv.at/arcgis/rest/services/tilecache/Ortho_2013_2015" +
|
---|
442 | "/MapServer/WMTS/tile/1.0.0/tilecache_Ortho_2013_2015/default/default028mm/11/6299/7373.jpg",
|
---|
443 | source.getTileUrl(11, tile.getXIndex(), tile.getYIndex()));
|
---|
444 | }
|
---|
445 |
|
---|
446 | @Test
|
---|
447 | void testApiKeyValid() {
|
---|
448 | assumeFalse(testImagery12168 == null);
|
---|
449 | try {
|
---|
450 | ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:4326"));
|
---|
451 | FeatureAdapter.registerApiKeyAdapter(id -> TestUtils.getTestDataRoot().replace('\\', '/'));
|
---|
452 | ImageryInfo testImageryWMTS = new ImageryInfo(testImagery12168);
|
---|
453 | testImageryWMTS.setUrl(testImageryWMTS.getUrl().replace(TestUtils.getTestDataRoot().replace('\\', '/'), "{apikey}"));
|
---|
454 | assertTrue(testImageryWMTS.getUrl().contains("{apikey}"), testImageryWMTS.getUrl());
|
---|
455 | testImageryWMTS.setId("WMTSTileSourceTest#testApiKeyValid");
|
---|
456 | WMTSTileSource ts = assertDoesNotThrow(() -> new WMTSTileSource(testImageryWMTS, ProjectionRegistry.getProjection()));
|
---|
457 | assertEquals("http://www.ngi.be/cartoweb/1.0.0/topo/default/3812/1/3/2.png",
|
---|
458 | ts.getTileUrl(1, 2, 3));
|
---|
459 | } finally {
|
---|
460 | FeatureAdapter.registerApiKeyAdapter(new FeatureAdapter.DefaultApiKeyAdapter());
|
---|
461 | }
|
---|
462 | }
|
---|
463 |
|
---|
464 | @Test
|
---|
465 | void testApiKeyInvalid() {
|
---|
466 | assumeFalse(testImagery12168 == null);
|
---|
467 | try {
|
---|
468 | ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:4326"));
|
---|
469 | FeatureAdapter.registerApiKeyAdapter(id -> null);
|
---|
470 | ImageryInfo testImageryWMTS = new ImageryInfo(testImagery12168);
|
---|
471 | testImageryWMTS.setUrl(testImageryWMTS.getUrl().replace(TestUtils.getTestDataRoot().replace('\\', '/'), "{apikey}"));
|
---|
472 | assertTrue(testImageryWMTS.getUrl().contains("{apikey}"), testImageryWMTS.getUrl());
|
---|
473 | testImageryWMTS.setId("WMTSTileSourceTest#testApiKeyInvalid");
|
---|
474 | org.openstreetmap.josm.data.projection.Projection projection = Projections.getProjectionByCode("EPSG:4326");
|
---|
475 | IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
|
---|
476 | () -> new WMTSTileSource(testImageryWMTS, projection));
|
---|
477 | assertEquals(tr("Could not retrieve API key for imagery with id={0}. Cannot add layer.\n" +
|
---|
478 | "API key for imagery with id=WMTSTileSourceTest#testApiKeyInvalid may not be available.",
|
---|
479 | testImageryWMTS.getId()),
|
---|
480 | exception.getMessage());
|
---|
481 | } finally {
|
---|
482 | FeatureAdapter.registerApiKeyAdapter(new FeatureAdapter.DefaultApiKeyAdapter());
|
---|
483 | }
|
---|
484 | }
|
---|
485 |
|
---|
486 | @ParameterizedTest
|
---|
487 | @ValueSource(strings = {"image/jpgpng", "image/png8", "image/png; mode=8bit", "image/jpeg", "image/jpg"})
|
---|
488 | void testSupportedMimeTypesUrlEncode(String mimeType, @TempDir File temporaryDirectory)
|
---|
489 | throws IOException, WMTSGetCapabilitiesException, ReflectiveOperationException {
|
---|
490 | final String data = FileUtils.readFileToString(new File(TestUtils.getTestDataRoot() +
|
---|
491 | "wmts/bug13975-multiple-tile-matrices-for-one-layer-projection.xml"), StandardCharsets.UTF_8)
|
---|
492 | .replace("image/jpgpng", mimeType);
|
---|
493 | File file = new File(temporaryDirectory, "testSupportedMimeTypes.xml");
|
---|
494 | FileUtils.writeStringToFile(file, data, StandardCharsets.UTF_8);
|
---|
495 | WMTSCapabilities capabilities = WMTSTileSource.getCapabilities(file.toURI().toURL().toExternalForm(), Collections.emptyMap());
|
---|
496 | assertEquals(2, capabilities.getLayers().size());
|
---|
497 | Field format = WMTSTileSource.Layer.class.getDeclaredField("format");
|
---|
498 | ReflectionUtils.setObjectsAccessible(format);
|
---|
499 | assertAll(capabilities.getLayers().stream().map(layer -> assertDoesNotThrow(() -> format.get(layer)))
|
---|
500 | .map(layer -> () -> assertEquals(mimeType.replace(" ", "%20"), layer)));
|
---|
501 | }
|
---|
502 | }
|
---|