source: josm/trunk/test/unit/org/openstreetmap/josm/data/imagery/WMTSTileSourceTest.java@ 11257

Last change on this file since 11257 was 11257, checked in by wiktorn, 7 years ago

Make DefaultLayer a type that can have layerName and tileMatrixSet

As WMTS specification gives possibility to have many tileMatrixSet's for
one layer and projection, default layer must specify both - layerName
and tileMatrixSet.

Accommodate to that fact layer selection dialogs and internal
computations in WMTS TileSource.

Closes: #13975

  • Property svn:eol-style set to native
File size: 14.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertTrue;
6
7import java.io.File;
8import java.io.IOException;
9import java.net.MalformedURLException;
10import java.util.ArrayList;
11import java.util.Collection;
12
13import org.junit.Ignore;
14import org.junit.Rule;
15import org.junit.Test;
16import org.openstreetmap.gui.jmapviewer.tilesources.TemplatedTMSTileSource;
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.TestUtils;
19import org.openstreetmap.josm.data.Bounds;
20import org.openstreetmap.josm.data.coor.LatLon;
21import org.openstreetmap.josm.data.projection.Projections;
22import org.openstreetmap.josm.testutils.JOSMTestRules;
23
24import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
25
26/**
27 * Unit tests for class {@link WMTSTileSource}.
28 */
29public class WMTSTileSourceTest {
30
31 private ImageryInfo testImageryTMS = new ImageryInfo("test imagery", "http://localhost", "tms", null, null);
32 private ImageryInfo testImageryPSEUDO_MERCATOR = getImagery(TestUtils.getTestDataRoot() + "wmts/getcapabilities-pseudo-mercator.xml");
33 private ImageryInfo testImageryTOPO_PL = getImagery(TestUtils.getTestDataRoot() + "wmts/getcapabilities-TOPO.xml");
34 private ImageryInfo testImageryORTO_PL = getImagery(TestUtils.getTestDataRoot() + "wmts/getcapabilities-ORTO.xml");
35 private ImageryInfo testImageryWIEN = getImagery(TestUtils.getTestDataRoot() + "wmts/getCapabilities-wien.xml");
36 private ImageryInfo testImageryWALLONIE = getImagery(TestUtils.getTestDataRoot() + "wmts/WMTSCapabilities-Wallonie.xml");
37 private ImageryInfo testImageryOntario = getImagery(TestUtils.getTestDataRoot() + "wmts/WMTSCapabilities-Ontario.xml");
38 private ImageryInfo testImagery12168 = getImagery(TestUtils.getTestDataRoot() + "wmts/bug12168-WMTSCapabilities.xml");
39 private ImageryInfo testLotsOfLayers = getImagery(TestUtils.getTestDataRoot() + "wmts/getCapabilities-lots-of-layers.xml");
40 private ImageryInfo testDuplicateTags = getImagery(TestUtils.getTestDataRoot() + "wmts/bug12573-wmts-identifier.xml");
41 private ImageryInfo testMissingStyleIdentifer = getImagery(TestUtils.getTestDataRoot() + "wmts/bug12573-wmts-missing-style-identifier.xml");
42 private ImageryInfo testMultipleTileMatrixForLayer = getImagery(TestUtils.getTestDataRoot() +
43 "wmts/bug13975-multiple-tile-matrices-for-one-layer-projection.xml");
44
45 /**
46 * Setup test.
47 */
48 @Rule
49 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
50 public JOSMTestRules test = new JOSMTestRules();
51
52 private static ImageryInfo getImagery(String path) {
53 try {
54 return new ImageryInfo(
55 "test",
56 new File(path).toURI().toURL().toString()
57 );
58 } catch (MalformedURLException e) {
59 e.printStackTrace();
60 return null;
61 }
62 }
63
64 @Test
65 public void testPseudoMercator() throws IOException {
66 Main.setProjection(Projections.getProjectionByCode("EPSG:3857"));
67 WMTSTileSource testSource = new WMTSTileSource(testImageryPSEUDO_MERCATOR);
68 testSource.initProjection(Main.getProjection());
69
70 verifyMercatorTile(testSource, 0, 0, 1);
71 verifyMercatorTile(testSource, 0, 0, 2);
72 verifyMercatorTile(testSource, 1, 1, 2);
73 for (int x = 0; x < 4; x++) {
74 for (int y = 0; y < 4; y++) {
75 verifyMercatorTile(testSource, x, y, 3);
76 }
77 }
78 for (int x = 0; x < 8; x++) {
79 for (int y = 0; y < 4; y++) {
80 verifyMercatorTile(testSource, x, y, 4);
81 }
82 }
83
84 verifyMercatorTile(testSource, 2 << 9 - 1, 2 << 8 - 1, 10);
85
86 assertEquals("TileXMax", 1, testSource.getTileXMax(0));
87 assertEquals("TileYMax", 1, testSource.getTileYMax(0));
88 assertEquals("TileXMax", 2, testSource.getTileXMax(1));
89 assertEquals("TileYMax", 2, testSource.getTileYMax(1));
90 assertEquals("TileXMax", 4, testSource.getTileXMax(2));
91 assertEquals("TileYMax", 4, testSource.getTileYMax(2));
92 }
93
94 @Test
95 public void testWALLONIE() throws IOException {
96 Main.setProjection(Projections.getProjectionByCode("EPSG:31370"));
97 WMTSTileSource testSource = new WMTSTileSource(testImageryWALLONIE);
98 testSource.initProjection(Main.getProjection());
99
100 assertEquals("http://geoservices.wallonie.be/arcgis/rest/services/DONNEES_BASE/FOND_PLAN_ANNOTATIONS_2012_RW_NB/"
101 + "MapServer/WMTS/tile/1.0.0/DONNEES_BASE_FOND_PLAN_ANNOTATIONS_2012_RW_NB/default/default028mm/5/1219/1063.png",
102 testSource.getTileUrl(5, 1063, 1219));
103
104 // +bounds=2.54,49.51,6.4,51.5
105 Bounds wallonieBounds = new Bounds(
106 new LatLon(49.485372459967245, 2.840548314430268),
107 new LatLon(50.820959517561256, 6.427849693016202)
108 );
109 verifyBounds(wallonieBounds, testSource, 5, 1063, 1219);
110 verifyBounds(wallonieBounds, testSource, 10, 17724, 20324);
111 }
112
113 @Test
114 @Ignore("disable this test, needs further working") // XXX
115 public void testWALLONIENoMatrixDimension() throws IOException {
116 Main.setProjection(Projections.getProjectionByCode("EPSG:31370"));
117 WMTSTileSource testSource = new WMTSTileSource(getImagery("test/data/wmts/WMTSCapabilities-Wallonie-nomatrixdimension.xml"));
118 testSource.initProjection(Main.getProjection());
119
120 Bounds wallonieBounds = new Bounds(
121 new LatLon(49.485372459967245, 2.840548314430268),
122 new LatLon(50.820959517561256, 6.427849693016202)
123 );
124
125 verifyBounds(wallonieBounds, testSource, 6, 1063, 1219);
126 verifyBounds(wallonieBounds, testSource, 11, 17724, 20324);
127 }
128
129 private void verifyBounds(Bounds bounds, WMTSTileSource testSource, int z, int x, int y) {
130 LatLon ret = new LatLon(testSource.tileXYToLatLon(x, y, z));
131 assertTrue(ret.toDisplayString() + " doesn't lie within: " + bounds.toString(), bounds.contains(ret));
132 int tileXmax = testSource.getTileXMax(z);
133 int tileYmax = testSource.getTileYMax(z);
134 assertTrue("tile x: " + x + " is greater than allowed max: " + tileXmax, tileXmax >= x);
135 assertTrue("tile y: " + y + " is greater than allowed max: " + tileYmax, tileYmax >= y);
136 }
137
138 @Test
139 public void testWIEN() throws IOException {
140 Main.setProjection(Projections.getProjectionByCode("EPSG:3857"));
141 WMTSTileSource testSource = new WMTSTileSource(testImageryWIEN);
142 testSource.initProjection(Main.getProjection());
143 int zoomOffset = 10;
144
145 verifyMercatorTile(testSource, 0, 0, 1, zoomOffset);
146 verifyMercatorTile(testSource, 1105, 709, 2, zoomOffset);
147 verifyMercatorTile(testSource, 1, 1, 1, zoomOffset);
148 verifyMercatorTile(testSource, 2, 2, 1, zoomOffset);
149 verifyMercatorTile(testSource, 0, 0, 2, zoomOffset);
150 verifyMercatorTile(testSource, 1, 1, 2, zoomOffset);
151
152 for (int x = 0; x < 4; x++) {
153 for (int y = 0; y < 4; y++) {
154 verifyMercatorTile(testSource, x, y, 3, zoomOffset);
155 }
156 }
157 for (int x = 0; x < 8; x++) {
158 for (int y = 0; y < 4; y++) {
159 verifyMercatorTile(testSource, x, y, 4, zoomOffset);
160 }
161 }
162
163 verifyMercatorTile(testSource, 2 << 9 - 1, 2 << 8 - 1, 2, zoomOffset);
164
165 verifyMercatorMax(testSource, 1, zoomOffset);
166 verifyMercatorMax(testSource, 2, zoomOffset);
167 verifyMercatorMax(testSource, 3, zoomOffset);
168 }
169
170 private void verifyMercatorMax(WMTSTileSource testSource, int zoom, int zoomOffset) {
171 TemplatedTMSTileSource verifier = new TemplatedTMSTileSource(testImageryTMS);
172 int result = testSource.getTileXMax(zoom);
173 int expected = verifier.getTileXMax(zoom + zoomOffset);
174 assertTrue("TileXMax expected: " + expected + " got: " + result, Math.abs(result - expected) < 5);
175 result = testSource.getTileYMax(zoom);
176 expected = verifier.getTileYMax(zoom + zoomOffset);
177 assertTrue("TileYMax expected: " + expected + " got: " + result, Math.abs(result - expected) < 5);
178 }
179
180 @Test
181 public void testGeoportalTOPOPL() throws IOException {
182 Main.setProjection(Projections.getProjectionByCode("EPSG:4326"));
183 WMTSTileSource testSource = new WMTSTileSource(testImageryTOPO_PL);
184 testSource.initProjection(Main.getProjection());
185 verifyTile(new LatLon(56, 12), testSource, 0, 0, 1);
186 verifyTile(new LatLon(56, 12), testSource, 0, 0, 2);
187 verifyTile(new LatLon(51.13231917844218, 16.867680821557823), testSource, 1, 1, 1);
188
189 assertEquals("TileXMax", 2, testSource.getTileXMax(0));
190 assertEquals("TileYMax", 1, testSource.getTileYMax(0));
191 assertEquals("TileXMax", 3, testSource.getTileXMax(1));
192 assertEquals("TileYMax", 2, testSource.getTileYMax(1));
193 assertEquals("TileXMax", 6, testSource.getTileXMax(2));
194 assertEquals("TileYMax", 4, testSource.getTileYMax(2));
195 assertEquals(
196 "http://mapy.geoportal.gov.pl/wss/service/WMTS/guest/wmts/TOPO?SERVICE=WMTS&REQUEST=GetTile&"
197 + "VERSION=1.0.0&LAYER=MAPA TOPOGRAFICZNA&STYLE=default&FORMAT=image/jpeg&tileMatrixSet=EPSG:4326&"
198 + "tileMatrix=EPSG:4326:0&tileRow=1&tileCol=1",
199 testSource.getTileUrl(0, 1, 1));
200 }
201
202 @Test
203 public void testGeoportalORTOPL4326() throws IOException {
204 Main.setProjection(Projections.getProjectionByCode("EPSG:4326"));
205 WMTSTileSource testSource = new WMTSTileSource(testImageryORTO_PL);
206 testSource.initProjection(Main.getProjection());
207 verifyTile(new LatLon(53.60205873528009, 19.552206794646956), testSource, 12412, 3941, 13);
208 verifyTile(new LatLon(49.79005619189761, 22.778262259134397), testSource, 17714, 10206, 13);
209 }
210
211 @Test
212 public void testGeoportalORTOPL2180() throws IOException {
213 Main.setProjection(Projections.getProjectionByCode("EPSG:2180"));
214 WMTSTileSource testSource = new WMTSTileSource(testImageryORTO_PL);
215 testSource.initProjection(Main.getProjection());
216
217 verifyTile(new LatLon(53.59940948387726, 19.560544913270064), testSource, 6453, 3140, 13);
218 verifyTile(new LatLon(49.782984840526055, 22.790064966993445), testSource, 9932, 9305, 13);
219 }
220
221 @Test
222 public void testTicket12168() throws IOException {
223 Main.setProjection(Projections.getProjectionByCode("EPSG:3857"));
224 WMTSTileSource testSource = new WMTSTileSource(testImagery12168);
225 testSource.initProjection(Main.getProjection());
226 assertEquals(
227 "http://www.ngi.be/cartoweb/1.0.0/topo/default/3857/7/1/1.png",
228 testSource.getTileUrl(0, 1, 1));
229 }
230
231 @Test
232 @Ignore("disabled as this needs user action") // XXX
233 public void testTwoTileSetsForOneProjection() throws Exception {
234 Main.setProjection(Projections.getProjectionByCode("EPSG:3857"));
235 WMTSTileSource testSource = new WMTSTileSource(testImageryOntario);
236 testSource.initProjection(Main.getProjection());
237 verifyTile(new LatLon(45.4105023, -75.7153702), testSource, 303751, 375502, 12);
238 verifyTile(new LatLon(45.4601306, -75.7617187), testSource, 1186, 1466, 4);
239 }
240
241 @Test
242 @Ignore("disabled as this needs user action") // XXX
243 public void testManyLayersScrollbars() throws Exception {
244 Main.setProjection(Projections.getProjectionByCode("EPSG:3857"));
245 WMTSTileSource testSource = new WMTSTileSource(testLotsOfLayers);
246 testSource.initProjection(Main.getProjection());
247
248 }
249
250 @Test
251 public void testParserForDuplicateTags() throws Exception {
252 Main.setProjection(Projections.getProjectionByCode("EPSG:3857"));
253 WMTSTileSource testSource = new WMTSTileSource(testDuplicateTags);
254 testSource.initProjection(Main.getProjection());
255 assertEquals(
256 "http://tile.informatievlaanderen.be/ws/raadpleegdiensten/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=grb_bsk&"
257 + "STYLE=&FORMAT=image/png&tileMatrixSet=GoogleMapsVL&tileMatrix=1&tileRow=1&tileCol=1",
258 testSource.getTileUrl(1, 1, 1)
259 );
260 }
261
262 @Test
263 public void testParserForMissingStyleIdentifier() throws Exception {
264 Main.setProjection(Projections.getProjectionByCode("EPSG:3857"));
265 WMTSTileSource testSource = new WMTSTileSource(testMissingStyleIdentifer);
266 testSource.initProjection(Main.getProjection());
267 }
268
269 @Test
270 public void testForMultipleTileMatricesForOneLayerProjection() throws Exception {
271 Main.setProjection(Projections.getProjectionByCode("EPSG:3857"));
272 ImageryInfo copy = new ImageryInfo(testMultipleTileMatrixForLayer);
273 Collection<DefaultLayer> defaultLayers = new ArrayList<>(1);
274 defaultLayers.add(new WMTSDefaultLayer("Mashhad_BaseMap_1", "default028mm"));
275 copy.setDefaultLayers(defaultLayers);
276 WMTSTileSource testSource = new WMTSTileSource(copy);
277 testSource.initProjection(Main.getProjection());
278 assertEquals(
279 "http://188.253.0.155:6080/arcgis/rest/services/Mashhad_BaseMap_1/MapServer/WMTS/tile/1.0.0/Mashhad_BaseMap_1"
280 + "/default/default028mm/1/3/2",
281 testSource.getTileUrl(1, 2, 3)
282 );
283 }
284
285 private void verifyTile(LatLon expected, WMTSTileSource source, int x, int y, int z) {
286 LatLon ll = new LatLon(source.tileXYToLatLon(x, y, z));
287 assertEquals("Latitude", expected.lat(), ll.lat(), 1e-05);
288 assertEquals("Longitude", expected.lon(), ll.lon(), 1e-05);
289 }
290
291 private void verifyMercatorTile(WMTSTileSource testSource, int x, int y, int z) {
292 verifyMercatorTile(testSource, x, y, z, 0);
293 }
294
295 private void verifyMercatorTile(WMTSTileSource testSource, int x, int y, int z, int zoomOffset) {
296 TemplatedTMSTileSource verifier = new TemplatedTMSTileSource(testImageryTMS);
297 LatLon result = new LatLon(testSource.tileXYToLatLon(x, y, z));
298 LatLon expected = new LatLon(verifier.tileXYToLatLon(x, y, z + zoomOffset));
299 assertEquals("Longitude", LatLon.normalizeLon(expected.lon() - result.lon()), 0.0, 1e-04);
300 assertEquals("Latitude", expected.lat(), result.lat(), 1e-04);
301 }
302}
Note: See TracBrowser for help on using the repository browser.