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

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

Checkstyle 6.19: enable SingleSpaceSeparator and fix violations

  • Property svn:eol-style set to native
File size: 9.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 org.junit.BeforeClass;
8import org.junit.Test;
9import org.openstreetmap.gui.jmapviewer.TileXY;
10import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
11import org.openstreetmap.gui.jmapviewer.tilesources.TemplatedTMSTileSource;
12import org.openstreetmap.josm.JOSMFixture;
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.Bounds;
15import org.openstreetmap.josm.data.coor.EastNorth;
16import org.openstreetmap.josm.data.coor.LatLon;
17import org.openstreetmap.josm.data.projection.CustomProjection;
18import org.openstreetmap.josm.data.projection.Projections;
19
20/**
21 * Unit tests for class {@link TemplatedWMSTileSource}.
22 */
23public class TemplatedWMSTileSourceTest {
24
25 private ImageryInfo testImageryWMS = new ImageryInfo("test imagery", "http://localhost", "wms", null, null);
26 private ImageryInfo testImageryTMS = new ImageryInfo("test imagery", "http://localhost", "tms", null, null);
27
28 /**
29 * Setup test.
30 */
31 @BeforeClass
32 public static void setUp() {
33 JOSMFixture.createUnitTestFixture().init();
34 }
35
36 /**
37 * Test EPSG:3857
38 */
39 @Test
40 public void testEPSG3857() {
41 Main.setProjection(Projections.getProjectionByCode("EPSG:3857"));
42 TemplatedWMSTileSource source = new TemplatedWMSTileSource(testImageryWMS);
43 verifyMercatorTile(source, 0, 0, 1);
44 verifyMercatorTile(source, 0, 0, 2);
45 verifyMercatorTile(source, 0, 1, 2);
46 verifyMercatorTile(source, 1, 0, 2);
47 verifyMercatorTile(source, 1, 1, 2);
48 for (int x = 0; x < 4; x++) {
49 for (int y = 0; y < 4; y++) {
50 verifyMercatorTile(source, x, y, 3);
51 verifyTileSquarness(source, x, y, 3);
52 }
53 }
54 verifyTileSquarness(source, 150, 20, 18);
55 verifyTileSquarness(source, 2270, 1323, 12);
56 verifyLocation(source, new LatLon(53.5937132, 19.5652017));
57 verifyLocation(source, new LatLon(53.501565692302854, 18.54455233898721));
58 }
59
60 /**
61 * Test EPSG:4326
62 */
63 @Test
64 public void testEPSG4326() {
65 Main.setProjection(Projections.getProjectionByCode("EPSG:4326"));
66 TemplatedWMSTileSource source = getSource();
67
68 verifyLocation(source, new LatLon(53.5937132, 19.5652017));
69 verifyLocation(source, new LatLon(53.501565692302854, 18.54455233898721));
70 verifyTileSquarness(source, 2, 2, 2);
71 verifyTileSquarness(source, 150, 20, 18);
72 verifyTileSquarness(source, 2270, 1323, 12);
73 }
74
75 /**
76 * Test EPSG:4326 - wide bounds
77 */
78 @Test
79 public void testEPSG4326_widebounds() {
80 Main.setProjection(new CustomProjection("+proj=lonlat +datum=WGS84 +axis=neu +bounds=-180,53,180,54"));
81 TemplatedWMSTileSource source = getSource();
82
83 verifyLocation(source, new LatLon(53.5937132, 19.5652017));
84 verifyLocation(source, new LatLon(53.501565692302854, 18.54455233898721));
85 }
86
87 /**
88 * Test EPSG:4326 - narrow bounds
89 */
90 @Test
91 public void testEPSG4326_narrowbounds() {
92 Main.setProjection(new CustomProjection("+proj=lonlat +datum=WGS84 +axis=neu +bounds=18,-90,20,90"));
93 TemplatedWMSTileSource source = getSource();
94
95 verifyLocation(source, new LatLon(53.5937132, 19.5652017));
96 verifyLocation(source, new LatLon(53.501565692302854, 18.54455233898721));
97 }
98
99 /**
100 * Test EPSG:2180
101 */
102 @Test
103 public void testEPSG2180() {
104 Main.setProjection(Projections.getProjectionByCode("EPSG:2180"));
105 TemplatedWMSTileSource source = getSource();
106
107 verifyLocation(source, new LatLon(53.5937132, 19.5652017));
108 verifyLocation(source, new LatLon(53.501565692302854, 18.54455233898721));
109
110 verifyTileSquarness(source, 2, 2, 2);
111 verifyTileSquarness(source, 150, 20, 18);
112 verifyTileSquarness(source, 2270, 1323, 12);
113 }
114
115 /**
116 * Test EPSG:3006 with bounds
117 */
118 @Test
119 public void testEPSG3006_withbounds() {
120 Main.setProjection(
121 new CustomProjection("+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 "
122 + "+units=m +no_defs +axis=neu +wmssrs=EPSG:3006 +bounds=10.5700,55.2000,24.1800,69.1000 "));
123 TemplatedWMSTileSource source = getSource();
124
125 verifyTileSquarness(source, 0, 1, 4);
126 verifyLocation(source, new LatLon(60, 18.1), 3);
127 verifyLocation(source, new LatLon(60, 18.1));
128 }
129
130 /**
131 * Test EPSG:3006 without bounds
132 */
133 @Test
134 public void testEPSG3006_withoutbounds() {
135 Main.setProjection(
136 new CustomProjection("+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 "
137 + "+units=m +no_defs +axis=neu +wmssrs=EPSG:3006"));
138 TemplatedWMSTileSource source = getSource();
139
140 verifyTileSquarness(source, 0, 1, 4);
141 verifyLocation(source, new LatLon(60, 18.1), 3);
142 verifyLocation(source, new LatLon(60, 18.1));
143 }
144
145 private void verifyMercatorTile(TemplatedWMSTileSource source, int x, int y, int z) {
146 TemplatedTMSTileSource verifier = new TemplatedTMSTileSource(testImageryTMS);
147 LatLon result = getTileLatLon(source, x, y, z);
148 ICoordinate expected = verifier.tileXYToLatLon(x, y, z - 1);
149 assertEquals(expected.getLat(), result.lat(), 1e-4);
150 assertEquals(LatLon.normalizeLon(expected.getLon() - result.lon()), 0.0, 1e-4);
151 LatLon tileCenter = new Bounds(result, getTileLatLon(source, x+1, y+1, z)).getCenter();
152 TileXY backwardsResult = source.latLonToTileXY(tileCenter.toCoordinate(), z);
153 assertEquals(x, backwardsResult.getXIndex());
154 assertEquals(y, backwardsResult.getYIndex());
155 }
156
157 private void verifyLocation(TemplatedWMSTileSource source, LatLon location) {
158 for (int z = source.getMaxZoom(); z > source.getMinZoom() + 1; z--) {
159 if (source.getTileXMax(z) != source.getTileXMin(z) && source.getTileYMax(z) != source.getTileYMin(z)) {
160 // do the tests only where there is more than one tile
161 verifyLocation(source, location, z);
162 }
163 }
164 }
165
166 private void verifyLocation(TemplatedWMSTileSource source, LatLon location, int z) {
167 assertTrue(
168 "Point outside world bounds",
169 Main.getProjection().getWorldBoundsLatLon().contains(location)
170 );
171
172 TileXY tileIndex = source.latLonToTileXY(location.toCoordinate(), z);
173
174 assertTrue("X index: " + tileIndex.getXIndex() + " greater than tileXmax: " + source.getTileXMax(z) + " at zoom: " + z,
175 tileIndex.getXIndex() <= source.getTileXMax(z));
176
177 assertTrue("Y index: " + tileIndex.getYIndex() + " greater than tileYmax: " + source.getTileYMax(z) + " at zoom: " + z,
178 tileIndex.getYIndex() <= source.getTileYMax(z));
179
180 EastNorth locationEN = Main.getProjection().latlon2eastNorth(location);
181 EastNorth x1 = Main.getProjection().latlon2eastNorth(getTileLatLon(source, tileIndex, z));
182 EastNorth x2 = Main.getProjection().latlon2eastNorth(getTileLatLon(source, tileIndex.getXIndex() + 1, tileIndex.getYIndex() + 1, z));
183 // test that location is within tile bounds
184 assertTrue(locationEN.toString() + " not within " + bboxStr(x1, x2) +
185 " for tile " + z + "/" + tileIndex.getXIndex() + "/" + tileIndex.getYIndex(),
186 isWithin(locationEN, x1, x2));
187 verifyTileSquarness(source, tileIndex.getXIndex(), tileIndex.getYIndex(), z);
188 }
189
190 private static boolean isWithin(EastNorth point, EastNorth topLeft, EastNorth bottomRight) {
191 return Math.min(topLeft.east(), bottomRight.east()) <= point.east() &&
192 point.east() <= Math.max(topLeft.east(), bottomRight.east()) &&
193 Math.min(topLeft.north(), bottomRight.north()) <= point.north() &&
194 point.north() <= Math.max(topLeft.north(), bottomRight.north());
195 }
196
197 private static String bboxStr(EastNorth x1, EastNorth x2) {
198 return "[" + x1.east() +", " + x1.north() + ", " + x2.east() + ", " + x2.north() +"]";
199 }
200
201 private LatLon getTileLatLon(TemplatedWMSTileSource source, TileXY tileIndex, int z) {
202 return getTileLatLon(source, tileIndex.getXIndex(), tileIndex.getYIndex(), z);
203 }
204
205 private LatLon getTileLatLon(TemplatedWMSTileSource source, int x, int y, int z) {
206 return new LatLon(source.tileXYToLatLon(x, y, z));
207 }
208
209 private void verifyTileSquarness(TemplatedWMSTileSource source, int x, int y, int z) {
210 /**
211 * t1 | t2
212 * -------
213 * t3 | t4
214 */
215 EastNorth t1 = source.getTileEastNorth(x, y, z);
216 EastNorth t2 = source.getTileEastNorth(x + 1, y, z);
217 EastNorth t3 = source.getTileEastNorth(x, y + 1, z);
218 EastNorth t4 = source.getTileEastNorth(x + 1, y + 1, z);
219 double y_size = Math.abs(t1.getY() - t4.getY());
220 double x_size = Math.abs(t1.getX() - t4.getX());
221
222 assertEquals(x_size, y_size, Math.max(x_size, y_size) * 1e-06);
223 assertEquals(y_size, Math.abs(t1.getY() - t3.getY()), y_size * 1e-06);
224 assertEquals(x_size, Math.abs(t1.getX() - t2.getX()), x_size * 1e-06);
225
226 t1 = source.getTileEastNorth(x, y, z);
227 t2 = source.getTileEastNorth(x + 1, y, z);
228 t3 = source.getTileEastNorth(x, y + 1, z);
229 t4 = source.getTileEastNorth(x + 1, y + 1, z);
230 y_size = Math.abs(t1.getY() - t4.getY());
231 x_size = Math.abs(t1.getX() - t4.getX());
232 assertEquals(x_size, y_size, Math.max(x_size, y_size) * 1e-05);
233 assertEquals(y_size, Math.abs(t1.getY() - t3.getY()), y_size * 1e-05);
234 assertEquals(x_size, Math.abs(t1.getX() - t2.getX()), x_size * 1e-05);
235 }
236
237 private TemplatedWMSTileSource getSource() {
238 return new TemplatedWMSTileSource(testImageryWMS);
239 }
240}
Note: See TracBrowser for help on using the repository browser.