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

Last change on this file since 8926 was 8857, checked in by Don-vip, 9 years ago

improve/cleanup unit tests

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