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

Last change on this file since 9767 was 9767, checked in by bastiK, 8 years ago

update tests for [9558] and [9642] (see #12186, see #12514)

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