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