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

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

sonar - Unused private method should be removed
sonar - Unused protected methods should be removed
sonar - Sections of code should not be "commented out"
sonar - Empty statements should be removed
sonar - squid:S1172 - Unused method parameters should be removed
sonar - squid:S1481 - Unused local variables should be removed

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