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

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

improve/cleanup unit tests

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