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

Last change on this file since 8624 was 8624, checked in by bastiK, 9 years ago

add missing svn:eol-style=native

  • Property svn:eol-style set to native
File size: 8.8 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.tilesources.TemplatedTMSTileSource;
11import org.openstreetmap.josm.JOSMFixture;
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.data.Bounds;
14import org.openstreetmap.josm.data.coor.EastNorth;
15import org.openstreetmap.josm.data.coor.LatLon;
16import org.openstreetmap.josm.data.osm.BBox;
17import org.openstreetmap.josm.data.projection.CustomProjection;
18import org.openstreetmap.josm.data.projection.Projection;
19import org.openstreetmap.josm.data.projection.Projections;
20
21public class TemplatedWMSTileSourceTest {
22
23 private ImageryInfo testImageryWMS = new ImageryInfo("test imagery", "http://localhost", "wms", null, null);
24 private ImageryInfo testImageryTMS = new ImageryInfo("test imagery", "http://localhost", "tms", null, null);
25 //private TileSource testSource = new TemplatedWMSTileSource(testImageryWMS);
26 /**
27 * Setup test.
28 */
29 @BeforeClass
30 public static void setUp() {
31 JOSMFixture.createUnitTestFixture().init();
32 }
33
34 @Test
35 public void testEPSG3857() {
36 Main.setProjection(Projections.getProjectionByCode("EPSG:3857"));
37 TemplatedWMSTileSource source = new TemplatedWMSTileSource(testImageryWMS);
38 verifyMercatorTile(source, 0, 1, 2);
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 LatLon expected = new LatLon(verifier.tileYToLat(y, z - 1), verifier.tileXToLon(x, z - 1)); //
129 assertTrue("result: " + result.toDisplayString() + " osmMercator: " + expected.toDisplayString(), result.equalsEpsilon(expected));
130 LatLon tileCenter = new Bounds(result, getTileLatLon(source, x+1, y+1, z)).getCenter();
131 TileXY backwardsResult = source.latLonToTileXY(tileCenter.toCoordinate(), z);
132 assertEquals(x, backwardsResult.getXIndex());
133 assertEquals(y, backwardsResult.getYIndex());
134 }
135
136 private void verifyLocation(TemplatedWMSTileSource source, LatLon location) {
137 for (int z = 22; z > 1; z--) {
138 verifyLocation(source, location, z);
139 }
140 }
141
142 private void verifyLocation(TemplatedWMSTileSource source, LatLon location, int z) {
143 assertTrue(
144 "Point outside world bounds",
145 Main.getProjection().getWorldBoundsLatLon().contains(location)
146 );
147
148 TileXY tileIndex = source.latLonToTileXY(location.toCoordinate(), z);
149
150 assertTrue("X index: " + tileIndex.getXIndex() + " greater than tileXmax: " + source.getTileXMax(z) + " at zoom: " + z,
151 tileIndex.getXIndex() <= source.getTileXMax(z));
152
153 assertTrue("Y index: " + tileIndex.getYIndex() + " greater than tileYmax: " + source.getTileYMax(z) + " at zoom: " + z,
154 tileIndex.getYIndex() <= source.getTileYMax(z));
155
156 // test that location is within tile bounds
157 BBox bbox = new BBox(
158 getTileLatLon(source, tileIndex, z),
159 getTileLatLon(source, tileIndex.getXIndex() + 1, tileIndex.getYIndex() + 1, z)
160 );
161 assertTrue(location.toDisplayString() + " not within " + bbox.toString() +
162 " for tile " + z + "/" + tileIndex.getXIndex() + "/" + tileIndex.getYIndex(),
163 bbox.bounds(location));
164 verifyTileSquarness(source, tileIndex.getXIndex(), tileIndex.getYIndex(), z);
165 }
166
167 private LatLon getTileLatLon(TemplatedWMSTileSource source, TileXY tileIndex, int z) {
168 return getTileLatLon(source, tileIndex.getXIndex(), tileIndex.getYIndex(), z);
169 }
170
171 private LatLon getTileLatLon(TemplatedWMSTileSource source, int x, int y, int z) {
172 return new LatLon(source.tileXYToLatLon(x, y, z));
173 }
174
175 private void verifyTileSquarness(TemplatedWMSTileSource source, int x, int y, int z) {
176 Projection proj = Main.getProjection();
177 /**
178 * t1 | t2
179 * -------
180 * t3 | t4
181 */
182 EastNorth t1 = source.getTileEastNorth(x, y, z);
183 EastNorth t2 = source.getTileEastNorth(x + 1, y, z);
184 EastNorth t3 = source.getTileEastNorth(x, y + 1, z);
185 EastNorth t4 = source.getTileEastNorth(x + 1, y + 1, z);
186 double y_size = Math.abs(t1.getY() - t4.getY());
187 double x_size = Math.abs(t1.getX() - t4.getX());
188
189 assertEquals(x_size, y_size, Math.max(x_size, y_size) * 1e-06);
190 assertEquals(y_size, Math.abs(t1.getY() - t3.getY()), y_size * 1e-06);
191 assertEquals(x_size, Math.abs(t1.getX() - t2.getX()), x_size * 1e-06);
192
193 t1 = source.getTileEastNorth(x, y, z);
194 t2 = source.getTileEastNorth(x + 1, y, z);
195 t3 = source.getTileEastNorth(x, y + 1, z);
196 t4 = source.getTileEastNorth(x + 1, y + 1, z);
197 y_size = Math.abs(t1.getY() - t4.getY());
198 x_size = Math.abs(t1.getX() - t4.getX());
199 assertEquals(x_size, y_size, Math.max(x_size, y_size) * 1e-05);
200 assertEquals(y_size, Math.abs(t1.getY() - t3.getY()), y_size * 1e-05);
201 assertEquals(x_size, Math.abs(t1.getX() - t2.getX()), x_size * 1e-05);
202 }
203
204 private TemplatedWMSTileSource getSource() {
205 TemplatedWMSTileSource source = new TemplatedWMSTileSource(testImageryWMS);
206 return source;
207 }
208}
Note: See TracBrowser for help on using the repository browser.