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

Last change on this file since 8599 was 8599, checked in by wiktorn, 9 years ago

Fix tests failing after [8598]

File size: 6.5 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.Projection;
18import org.openstreetmap.josm.data.projection.Projections;
19
20public class TemplatedWMSTileSourceTest {
21
22 private ImageryInfo testImageryWMS = new ImageryInfo("test imagery", "http://localhost", "wms", null, null);
23 private ImageryInfo testImageryTMS = new ImageryInfo("test imagery", "http://localhost", "tms", null, null);
24 //private TileSource testSource = new TemplatedWMSTileSource(testImageryWMS);
25 /**
26 * Setup test.
27 */
28 @BeforeClass
29 public static void setUp() {
30 JOSMFixture.createUnitTestFixture().init();
31 }
32
33 @Test
34 public void testEPSG3857() {
35 Main.setProjection(Projections.getProjectionByCode("EPSG:3857"));
36 TemplatedWMSTileSource source = new TemplatedWMSTileSource(testImageryWMS);
37 verifyMercatorTile(source, 0, 1, 2);
38 verifyMercatorTile(source, 0, 0, 1);
39 verifyMercatorTile(source, 0, 0, 2);
40 verifyMercatorTile(source, 0, 1, 2);
41 verifyMercatorTile(source, 1, 0, 2);
42 verifyMercatorTile(source, 1, 1, 2);
43 for (int x = 0; x < 4; x++) {
44 for (int y = 0; y < 4; y++) {
45 verifyMercatorTile(source, x, y, 3);
46 verifyTileSquarness(source, x, y, 3);
47 }
48 }
49 verifyTileSquarness(source, 150, 20, 18);
50 verifyTileSquarness(source, 2270, 1323, 12);
51 verifyLocation(source, new LatLon(53.5937132, 19.5652017));
52 verifyLocation(source, new LatLon(53.501565692302854, 18.54455233898721));
53
54 }
55
56 @Test
57 public void testEPSG4326() {
58 Main.setProjection(Projections.getProjectionByCode("EPSG:4326"));
59 TemplatedWMSTileSource source = getSource();
60
61 verifyLocation(source, new LatLon(53.5937132, 19.5652017));
62 verifyLocation(source, new LatLon(53.501565692302854, 18.54455233898721));
63 verifyTileSquarness(source, 2, 2, 2);
64 verifyTileSquarness(source, 150, 20, 18);
65 verifyTileSquarness(source, 2270, 1323, 12);
66 }
67
68 @Test
69 public void testEPSG2180() {
70 Main.setProjection(Projections.getProjectionByCode("EPSG:2180"));
71 TemplatedWMSTileSource source = getSource();
72
73 verifyLocation(source, new LatLon(53.5937132, 19.5652017));
74 verifyLocation(source, new LatLon(53.501565692302854, 18.54455233898721));
75
76 verifyTileSquarness(source, 2, 2, 2);
77 verifyTileSquarness(source, 150, 20, 18);
78 verifyTileSquarness(source, 2270, 1323, 12);
79 }
80
81 private void verifyMercatorTile(TemplatedWMSTileSource source, int x, int y, int z) {
82 TemplatedTMSTileSource verifier = new TemplatedTMSTileSource(testImageryTMS);
83 LatLon result = getTileLatLon(source, x, y, z);
84 LatLon expected = new LatLon(verifier.tileYToLat(y, z - 1), verifier.tileXToLon(x, z - 1)); //
85 System.out.println(z + "/" + x + "/" + y + " - result: " + result.toDisplayString() + " osmMercator: " + expected.toDisplayString());
86 assertTrue("result: " + result.toDisplayString() + " osmMercator: " + expected.toDisplayString(), result.equalsEpsilon(expected));
87 LatLon tileCenter = new Bounds(result, getTileLatLon(source, x+1, y+1, z)).getCenter();
88 TileXY backwardsResult = source.latLonToTileXY(tileCenter.toCoordinate(), z);
89 assertEquals(x, backwardsResult.getXIndex());
90 assertEquals(y, backwardsResult.getYIndex());
91 }
92
93 private void verifyLocation(TemplatedWMSTileSource source, LatLon location) {
94 for (int z = 1; z < 22; z++) {
95 TileXY tileIndex = source.latLonToTileXY(location.toCoordinate(), z);
96 BBox bbox = new BBox(
97 getTileLatLon(source, tileIndex, z),
98 getTileLatLon(source, tileIndex.getXIndex() + 1, tileIndex.getYIndex() + 1, z)
99 );
100 assertTrue(location.toDisplayString() + " not within " + bbox.toString() +
101 " for tile " + z + "/" + tileIndex.getXIndex() + "/" + tileIndex.getYIndex(),
102 bbox.bounds(location));
103 }
104 }
105
106 private LatLon getTileLatLon(TemplatedWMSTileSource source, TileXY tileIndex, int z) {
107 return getTileLatLon(source, tileIndex.getXIndex(), tileIndex.getYIndex(), z);
108 }
109
110 private LatLon getTileLatLon(TemplatedWMSTileSource source, int x, int y, int z) {
111 return new LatLon(source.tileXYToLatLon(x, y, z));
112 }
113
114 private void verifyTileSquarness(TemplatedWMSTileSource source, int x, int y, int z) {
115 Projection proj = Main.getProjection();
116 /**
117 * t1 | t2
118 * -------
119 * t3 | t4
120 */
121 EastNorth t1 = proj.latlon2eastNorth(getTileLatLon(source, x, y, z));
122 EastNorth t2 = proj.latlon2eastNorth(getTileLatLon(source, x + 1, y, z));
123 EastNorth t3 = proj.latlon2eastNorth(getTileLatLon(source, x, y + 1, z));
124 EastNorth t4 = proj.latlon2eastNorth(getTileLatLon(source, x + 1, y + 1, z));
125 double y_size = Math.abs(t1.getY() - t4.getY());
126 double x_size = Math.abs(t1.getX() - t4.getX());
127
128 assertEquals(x_size, y_size, Math.max(x_size, y_size) * 1e-05);
129 assertEquals(y_size, Math.abs(t1.getY() - t3.getY()), y_size * 1e-05);
130 assertEquals(x_size, Math.abs(t1.getX() - t2.getX()), x_size * 1e-05);
131
132 t1 = source.getTileEastNorth(x, y, z);
133 t2 = source.getTileEastNorth(x + 1, y, z);
134 t3 = source.getTileEastNorth(x, y + 1, z);
135 t4 = source.getTileEastNorth(x + 1, y + 1, z);
136 y_size = Math.abs(t1.getY() - t4.getY());
137 x_size = Math.abs(t1.getX() - t4.getX());
138 assertEquals(x_size, y_size, Math.max(x_size, y_size) * 1e-05);
139 assertEquals(y_size, Math.abs(t1.getY() - t3.getY()), y_size * 1e-05);
140 assertEquals(x_size, Math.abs(t1.getX() - t2.getX()), x_size * 1e-05);
141 }
142
143 private TemplatedWMSTileSource getSource() {
144 TemplatedWMSTileSource source = new TemplatedWMSTileSource(testImageryWMS);
145 return source;
146 }
147}
Note: See TracBrowser for help on using the repository browser.