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