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