source: josm/trunk/test/unit/org/openstreetmap/josm/data/imagery/TemplatedWMSTileSourceTest.java@ 17275

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

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
File size: 13.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertTrue;
6
7import org.junit.jupiter.api.Test;
8import org.junit.jupiter.api.extension.RegisterExtension;
9import org.openstreetmap.gui.jmapviewer.TileXY;
10import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
11import org.openstreetmap.gui.jmapviewer.tilesources.TemplatedTMSTileSource;
12import org.openstreetmap.josm.data.Bounds;
13import org.openstreetmap.josm.data.coor.EastNorth;
14import org.openstreetmap.josm.data.coor.LatLon;
15import org.openstreetmap.josm.data.projection.CustomProjection;
16import org.openstreetmap.josm.data.projection.Projection;
17import org.openstreetmap.josm.data.projection.ProjectionRegistry;
18import org.openstreetmap.josm.data.projection.Projections;
19import org.openstreetmap.josm.testutils.JOSMTestRules;
20
21import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
22
23/**
24 * Unit tests for class {@link TemplatedWMSTileSource}.
25 */
26class TemplatedWMSTileSourceTest {
27
28 private final ImageryInfo testImageryWMS = new ImageryInfo("test imagery", "http://localhost", "wms", null, null);
29 private final ImageryInfo testImageryTMS = new ImageryInfo("test imagery", "http://localhost", "tms", null, null);
30
31 /**
32 * Setup test.
33 */
34 @RegisterExtension
35 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
36 public JOSMTestRules test = new JOSMTestRules();
37
38 /**
39 * Test EPSG:3857
40 */
41 @Test
42 void testEPSG3857() {
43 Projection projection = Projections.getProjectionByCode("EPSG:3857");
44 ProjectionRegistry.setProjection(projection);
45 TemplatedWMSTileSource source = new TemplatedWMSTileSource(testImageryWMS, projection);
46 verifyMercatorTile(source, 0, 0, 1);
47 verifyMercatorTile(source, 0, 0, 2);
48 verifyMercatorTile(source, 0, 1, 2);
49 verifyMercatorTile(source, 1, 0, 2);
50 verifyMercatorTile(source, 1, 1, 2);
51 for (int x = 0; x < 4; x++) {
52 for (int y = 0; y < 4; y++) {
53 verifyMercatorTile(source, x, y, 3);
54 verifyTileSquareness(source, x, y, 3);
55 }
56 }
57 verifyTileSquareness(source, 150, 20, 18);
58 verifyTileSquareness(source, 2270, 1323, 12);
59 verifyLocation(source, new LatLon(53.5937132, 19.5652017));
60 verifyLocation(source, new LatLon(53.501565692302854, 18.54455233898721));
61 }
62
63 /**
64 * Test EPSG:4326
65 */
66 @Test
67 void testEPSG4326() {
68 Projection projection = Projections.getProjectionByCode("EPSG:4326");
69 ProjectionRegistry.setProjection(projection);
70 TemplatedWMSTileSource source = getSource(projection);
71
72 verifyLocation(source, new LatLon(53.5937132, 19.5652017));
73 verifyLocation(source, new LatLon(53.501565692302854, 18.54455233898721));
74 verifyTileSquareness(source, 2, 2, 2);
75 verifyTileSquareness(source, 150, 20, 18);
76 verifyTileSquareness(source, 2270, 1323, 12);
77 }
78
79 /**
80 * Test EPSG:4326 - wide bounds
81 */
82 @Test
83 void testEPSG4326widebounds() {
84 Projection projection = new CustomProjection("+proj=lonlat +datum=WGS84 +axis=neu +bounds=-180,53,180,54");
85 ProjectionRegistry.setProjection(projection);
86 TemplatedWMSTileSource source = getSource(projection);
87
88 verifyLocation(source, new LatLon(53.5937132, 19.5652017));
89 verifyLocation(source, new LatLon(53.501565692302854, 18.54455233898721));
90 }
91
92 /**
93 * Test EPSG:4326 - narrow bounds
94 */
95 @Test
96 void testEPSG4326narrowbounds() {
97 Projection projection = new CustomProjection("+proj=lonlat +datum=WGS84 +axis=neu +bounds=18,-90,20,90");
98 ProjectionRegistry.setProjection(projection);
99 TemplatedWMSTileSource source = getSource(projection);
100
101 verifyLocation(source, new LatLon(53.5937132, 19.5652017));
102 verifyLocation(source, new LatLon(53.501565692302854, 18.54455233898721));
103 }
104
105 /**
106 * Test EPSG:2180
107 */
108 @Test
109 void testEPSG2180() {
110 Projection projection = Projections.getProjectionByCode("EPSG:2180");
111 ProjectionRegistry.setProjection(projection);
112 TemplatedWMSTileSource source = getSource(projection);
113
114 verifyLocation(source, new LatLon(53.5937132, 19.5652017));
115 verifyLocation(source, new LatLon(53.501565692302854, 18.54455233898721));
116
117 verifyTileSquareness(source, 2, 2, 2);
118 verifyTileSquareness(source, 150, 20, 18);
119 verifyTileSquareness(source, 2270, 1323, 12);
120 }
121
122 /**
123 * Test EPSG:3006 with bounds
124 */
125 @Test
126 void testEPSG3006withbounds() {
127 Projection projection =
128 new CustomProjection("+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 "
129 + "+units=m +no_defs +axis=neu +wmssrs=EPSG:3006 +bounds=10.5700,55.2000,24.1800,69.1000 ");
130 ProjectionRegistry.setProjection(projection);
131 TemplatedWMSTileSource source = getSource(projection);
132
133 verifyTileSquareness(source, 0, 1, 4);
134 verifyLocation(source, new LatLon(60, 18.05008), 3);
135 verifyLocation(source, new LatLon(60, 18.05008));
136 }
137
138 /**
139 * Test EPSG:3006 without bounds
140 */
141 @Test
142 void testEPSG3006withoutbounds() {
143 Projection projection =
144 new CustomProjection("+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 "
145 + "+units=m +no_defs +axis=neu +wmssrs=EPSG:3006");
146 ProjectionRegistry.setProjection(projection);
147 TemplatedWMSTileSource source = getSource(projection);
148
149 verifyTileSquareness(source, 0, 1, 4);
150 verifyLocation(source, new LatLon(60, 18.1), 3);
151 verifyLocation(source, new LatLon(60, 18.1));
152 }
153
154 /**
155 * Test getTileUrl
156 */
157 @Test
158 void testGetTileUrl() {
159 // "https://maps.six.nsw.gov.au/arcgis/services/public/NSW_Imagery_Dates/MapServer/WMSServer?
160 // SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&CRS={proj}&BBOX={bbox}&WIDTH={width}&HEIGHT={height}
161 // &LAYERS=0&STYLES=&FORMAT=image/png32&DPI=96&MAP_RESOLUTION=96&FORMAT_OPTIONS=dpi:96&TRANSPARENT=TRUE",
162 Projection projection = Projections.getProjectionByCode("EPSG:4326");
163 ProjectionRegistry.setProjection(projection);
164 ImageryInfo testImageryWMS = new ImageryInfo("test imagery",
165 "https://maps.six.nsw.gov.au/arcgis/services/public/NSW_Imagery_Dates/MapServer/WMSServer?SERVICE=WMS&VERSION=1.3.0&"
166 + "REQUEST=GetMap&CRS={proj}&BBOX={bbox}&WIDTH={width}&HEIGHT={height}&LAYERS=0&STYLES=&FORMAT=image/png32&DPI=96&"
167 + "MAP_RESOLUTION=96&FORMAT_OPTIONS=dpi:96&TRANSPARENT=TRUE",
168 "wms",
169 null,
170 null);
171 TemplatedWMSTileSource ts = new TemplatedWMSTileSource(testImageryWMS, projection);
172 assertEquals("https://maps.six.nsw.gov.au/arcgis/services/public/NSW_Imagery_Dates/MapServer/WMSServer?SERVICE=WMS&"
173 + "VERSION=1.3.0&REQUEST=GetMap&CRS=EPSG:4326&BBOX=-1350.0000000,540.0000000,-990.0000000,900.0000000&WIDTH=512&"
174 + "HEIGHT=512&LAYERS=0&STYLES=&FORMAT=image/png32&DPI=96&MAP_RESOLUTION=96&FORMAT_OPTIONS=dpi:96&TRANSPARENT=TRUE",
175 ts.getTileUrl(1, 2, 3));
176 assertEquals("https://maps.six.nsw.gov.au/arcgis/services/public/NSW_Imagery_Dates/MapServer/WMSServer?SERVICE=WMS&"
177 + "VERSION=1.3.0&REQUEST=GetMap&CRS=EPSG:4326&BBOX=-90.0000000,0.0000000,-0.0000000,90.0000000&WIDTH=512&HEIGHT=512&"
178 + "LAYERS=0&STYLES=&FORMAT=image/png32&DPI=96&MAP_RESOLUTION=96&FORMAT_OPTIONS=dpi:96&TRANSPARENT=TRUE",
179 ts.getTileUrl(3, 2, 1));
180 testImageryWMS = new ImageryInfo("test imagery",
181 "https://services.slip.wa.gov.au/public/services/SLIP_Public_Services/Transport/MapServer/WMSServer?LAYERS=8&"
182 + "TRANSPARENT=TRUE&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&FORMAT=image%2Fpng&SRS={proj}&BBOX={bbox}&"
183 + "WIDTH={width}&HEIGHT={height}",
184 "wms",
185 null,
186 null);
187 ts = new TemplatedWMSTileSource(testImageryWMS, projection);
188 assertEquals("https://services.slip.wa.gov.au/public/services/SLIP_Public_Services/Transport/MapServer/WMSServer?LAYERS=8&"
189 + "TRANSPARENT=TRUE&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&FORMAT=image%2Fpng&SRS=EPSG:4326&"
190 + "BBOX=540.0000000,-1350.0000000,900.0000000,-990.0000000&WIDTH=512&HEIGHT=512",
191 ts.getTileUrl(1, 2, 3));
192 assertEquals("https://services.slip.wa.gov.au/public/services/SLIP_Public_Services/Transport/MapServer/WMSServer?LAYERS=8&"
193 + "TRANSPARENT=TRUE&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&FORMAT=image%2Fpng&SRS=EPSG:4326&"
194 + "BBOX=0.0000000,-90.0000000,90.0000000,-0.0000000&WIDTH=512&HEIGHT=512", ts.getTileUrl(3, 2, 1));
195 testImageryWMS.setTileSize(256);
196 ts = new TemplatedWMSTileSource(testImageryWMS, projection);
197 assertEquals("https://services.slip.wa.gov.au/public/services/SLIP_Public_Services/Transport/MapServer/WMSServer?LAYERS=8&"
198 + "TRANSPARENT=TRUE&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&FORMAT=image%2Fpng&SRS=EPSG:4326&"
199 + "BBOX=540.0000000,-1350.0000000,900.0000000,-990.0000000&WIDTH=256&HEIGHT=256",
200 ts.getTileUrl(1, 2, 3));
201 }
202
203 private void verifyMercatorTile(TemplatedWMSTileSource source, int x, int y, int z) {
204 TemplatedTMSTileSource verifier = new TemplatedTMSTileSource(testImageryTMS);
205 LatLon result = getTileLatLon(source, x, y, z);
206 ICoordinate expected = verifier.tileXYToLatLon(x, y, z - 1);
207 assertEquals(expected.getLat(), result.lat(), 1e-4);
208 assertEquals(0.0, LatLon.normalizeLon(expected.getLon() - result.lon()), 1e-4);
209 LatLon tileCenter = new Bounds(result, getTileLatLon(source, x+1, y+1, z)).getCenter();
210 TileXY backwardsResult = source.latLonToTileXY(CoordinateConversion.llToCoor(tileCenter), z);
211 assertEquals(x, backwardsResult.getXIndex());
212 assertEquals(y, backwardsResult.getYIndex());
213 }
214
215 private void verifyLocation(TemplatedWMSTileSource source, LatLon location) {
216 for (int z = source.getMaxZoom(); z > source.getMinZoom() + 1; z--) {
217 if (source.getTileXMax(z) != source.getTileXMin(z) && source.getTileYMax(z) != source.getTileYMin(z)) {
218 // do the tests only where there is more than one tile
219 verifyLocation(source, location, z);
220 }
221 }
222 }
223
224 private void verifyLocation(TemplatedWMSTileSource source, LatLon location, int z) {
225 Projection projection = ProjectionRegistry.getProjection();
226 assertTrue(projection.getWorldBoundsLatLon().contains(location), "Point outside world bounds");
227
228 TileXY tileIndex = source.latLonToTileXY(CoordinateConversion.llToCoor(location), z);
229
230 assertTrue(tileIndex.getXIndex() <= source.getTileXMax(z),
231 "X index: " + tileIndex.getXIndex() + " greater than tileXmax: " + source.getTileXMax(z) + " at zoom: " + z);
232
233 assertTrue(tileIndex.getYIndex() <= source.getTileYMax(z),
234 "Y index: " + tileIndex.getYIndex() + " greater than tileYmax: " + source.getTileYMax(z) + " at zoom: " + z);
235
236 ICoordinate x1 = source.tileXYToLatLon(tileIndex.getXIndex(), tileIndex.getYIndex(), z);
237 ICoordinate x2 = source.tileXYToLatLon(tileIndex.getXIndex() + 1, tileIndex.getYIndex() + 1, z);
238 if (x1.getLon() > x2.getLon()) {
239 x2.setLon(180);
240 }
241 Bounds bounds = new Bounds(x1.getLat(), x1.getLon(), true);
242 bounds.extend(x2.getLat(), x2.getLon());
243 // test that location is within tile bounds
244 assertTrue(bounds.contains(location),
245 location + " not within " + bounds + " for tile " + z + "/" + tileIndex.getXIndex() + "/" + tileIndex.getYIndex());
246 verifyTileSquareness(source, tileIndex.getXIndex(), tileIndex.getYIndex(), z);
247 }
248
249 private LatLon getTileLatLon(TemplatedWMSTileSource source, int x, int y, int z) {
250 return CoordinateConversion.coorToLL(source.tileXYToLatLon(x, y, z));
251 }
252
253 private void verifyTileSquareness(TemplatedWMSTileSource source, int x, int y, int z) {
254 /**
255 * t1 | t2
256 * -------
257 * t3 | t4
258 */
259 EastNorth t1 = source.getTileEastNorth(x, y, z);
260 EastNorth t2 = source.getTileEastNorth(x + 1, y, z);
261 EastNorth t3 = source.getTileEastNorth(x, y + 1, z);
262 EastNorth t4 = source.getTileEastNorth(x + 1, y + 1, z);
263 double y_size = Math.abs(t1.getY() - t4.getY());
264 double x_size = Math.abs(t1.getX() - t4.getX());
265
266 assertEquals(x_size, y_size, Math.max(x_size, y_size) * 1e-06);
267 assertEquals(y_size, Math.abs(t1.getY() - t3.getY()), y_size * 1e-06);
268 assertEquals(x_size, Math.abs(t1.getX() - t2.getX()), x_size * 1e-06);
269
270 t1 = source.getTileEastNorth(x, y, z);
271 t2 = source.getTileEastNorth(x + 1, y, z);
272 t3 = source.getTileEastNorth(x, y + 1, z);
273 t4 = source.getTileEastNorth(x + 1, y + 1, z);
274 y_size = Math.abs(t1.getY() - t4.getY());
275 x_size = Math.abs(t1.getX() - t4.getX());
276 assertEquals(x_size, y_size, Math.max(x_size, y_size) * 1e-05);
277 assertEquals(y_size, Math.abs(t1.getY() - t3.getY()), y_size * 1e-05);
278 assertEquals(x_size, Math.abs(t1.getX() - t2.getX()), x_size * 1e-05);
279 }
280
281 private TemplatedWMSTileSource getSource(Projection projection) {
282 return new TemplatedWMSTileSource(testImageryWMS, projection);
283 }
284}
Note: See TracBrowser for help on using the repository browser.