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