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 java.io.File;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.net.MalformedURLException;
|
---|
10 |
|
---|
11 | import org.junit.BeforeClass;
|
---|
12 | import org.junit.Test;
|
---|
13 | import org.openstreetmap.gui.jmapviewer.TileXY;
|
---|
14 | import org.openstreetmap.gui.jmapviewer.tilesources.TemplatedTMSTileSource;
|
---|
15 | import org.openstreetmap.josm.JOSMFixture;
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.data.Bounds;
|
---|
18 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
19 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
20 |
|
---|
21 | public class WMTSTileSourceTest {
|
---|
22 |
|
---|
23 | private ImageryInfo testImageryTMS = new ImageryInfo("test imagery", "http://localhost", "tms", null, null);
|
---|
24 | private ImageryInfo testImageryPSEUDO_MERCATOR = getImagery("test/data/wmts/getcapabilities-pseudo-mercator.xml");
|
---|
25 | private ImageryInfo testImageryTOPO_PL = getImagery("test/data/wmts/getcapabilities-TOPO.xml");
|
---|
26 | private ImageryInfo testImageryORTO_PL = getImagery("test/data/wmts/getcapabilities-ORTO.xml");
|
---|
27 | private ImageryInfo testImageryWIEN = getImagery("test/data/wmts/getCapabilities-wien.xml");
|
---|
28 | private ImageryInfo testImageryWALLONIE = getImagery("test/data/wmts/WMTSCapabilities-Wallonie.xml");
|
---|
29 |
|
---|
30 | @BeforeClass
|
---|
31 | public static void setUp() {
|
---|
32 | JOSMFixture.createUnitTestFixture().init();
|
---|
33 | }
|
---|
34 |
|
---|
35 | private static ImageryInfo getImagery(String path) {
|
---|
36 | try {
|
---|
37 | return new ImageryInfo(
|
---|
38 | "test",
|
---|
39 | new File(path).toURI().toURL().toString()
|
---|
40 | );
|
---|
41 | } catch (MalformedURLException e) {
|
---|
42 | e.printStackTrace();
|
---|
43 | return null;
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Test
|
---|
48 | public void testPseudoMercator() throws MalformedURLException, IOException {
|
---|
49 | Main.setProjection(Projections.getProjectionByCode("EPSG:3857"));
|
---|
50 | WMTSTileSource testSource = new WMTSTileSource(testImageryPSEUDO_MERCATOR);
|
---|
51 |
|
---|
52 | verifyMercatorTile(testSource, 0, 0, 1);
|
---|
53 | verifyMercatorTile(testSource, 0, 0, 2);
|
---|
54 | verifyMercatorTile(testSource, 1, 1, 2);
|
---|
55 | for (int x = 0; x < 4; x++) {
|
---|
56 | for (int y = 0; y < 4; y++) {
|
---|
57 | verifyMercatorTile(testSource, x, y, 3);
|
---|
58 | }
|
---|
59 | }
|
---|
60 | for (int x = 0; x < 8; x++) {
|
---|
61 | for (int y = 0; y < 4; y++) {
|
---|
62 | verifyMercatorTile(testSource, x, y, 4);
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | verifyMercatorTile(testSource, 2 << 9 - 1, 2 << 8 - 1, 10);
|
---|
67 |
|
---|
68 | assertEquals("TileXMax", 1, testSource.getTileXMax(1));
|
---|
69 | assertEquals("TileYMax", 1, testSource.getTileYMax(1));
|
---|
70 | assertEquals("TileXMax", 2, testSource.getTileXMax(2));
|
---|
71 | assertEquals("TileYMax", 2, testSource.getTileYMax(2));
|
---|
72 | assertEquals("TileXMax", 5, testSource.getTileXMax(3));
|
---|
73 | assertEquals("TileYMax", 4, testSource.getTileYMax(3));
|
---|
74 |
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Test
|
---|
78 | public void testWALLONIE() throws MalformedURLException, IOException {
|
---|
79 | Main.setProjection(Projections.getProjectionByCode("EPSG:31370"));
|
---|
80 | WMTSTileSource testSource = new WMTSTileSource(testImageryWALLONIE);
|
---|
81 | Bounds wallonieBounds = new Bounds(
|
---|
82 | new LatLon(49.485372459967245, 2.840548314430268),
|
---|
83 | new LatLon(50.820959517561256, 6.427849693016202)
|
---|
84 | );
|
---|
85 | verifyBounds(wallonieBounds, testSource, 10, 20324, 17724);
|
---|
86 |
|
---|
87 | }
|
---|
88 |
|
---|
89 | private void verifyBounds(Bounds bounds, WMTSTileSource testSource, int z, int x, int y) {
|
---|
90 | LatLon ret = new LatLon(testSource.tileXYToLatLon(y, y, z));
|
---|
91 | assertTrue(ret.toDisplayString() + " doesn't lie within: " + bounds.toString(), bounds.contains(ret));
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 |
|
---|
96 | @Test
|
---|
97 | public void testWIEN() throws MalformedURLException, IOException {
|
---|
98 | Main.setProjection(Projections.getProjectionByCode("EPSG:3857"));
|
---|
99 | WMTSTileSource testSource = new WMTSTileSource(testImageryWIEN);
|
---|
100 | int zoomOffset = 10;
|
---|
101 |
|
---|
102 | // Linz - 11/1105/709.png
|
---|
103 | verifyMercatorTile(testSource, 0, 0, 1, zoomOffset);
|
---|
104 | verifyMercatorTile(testSource, 1105, 709, 2, zoomOffset);
|
---|
105 | verifyMercatorTile(testSource, 1, 1, 1, zoomOffset);
|
---|
106 | verifyMercatorTile(testSource, 2, 2, 1, zoomOffset);
|
---|
107 | verifyMercatorTile(testSource, 0, 0, 2, zoomOffset);
|
---|
108 | verifyMercatorTile(testSource, 1, 1, 2, zoomOffset);
|
---|
109 |
|
---|
110 |
|
---|
111 | LatLon ll = new LatLon(testSource.tileXYToLatLon(500, 500, 1));
|
---|
112 |
|
---|
113 | TileXY xy = testSource.latLonToTileXY(new LatLon(48.21, 14.24).toCoordinate(), 1);
|
---|
114 | assertTrue("X index is negative: " + xy.getXIndex(), xy.getXIndex() > 0);
|
---|
115 | assertTrue(xy.getYIndex() > 0);
|
---|
116 | for(int x = 0; x < 4; x++) {
|
---|
117 | for(int y = 0; y < 4; y++) {
|
---|
118 | verifyMercatorTile(testSource, x, y, 3, zoomOffset);
|
---|
119 | }
|
---|
120 | }
|
---|
121 | for(int x = 0; x < 8; x++) {
|
---|
122 | for(int y = 0; y < 4; y++) {
|
---|
123 | verifyMercatorTile(testSource, x, y, zoomOffset);
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | verifyMercatorTile(testSource, 2<<9 - 1, 2<<8 - 1, zoomOffset);
|
---|
128 |
|
---|
129 | assertEquals("TileXMax", 1, testSource.getTileXMax(1));
|
---|
130 | assertEquals("TileYMax", 1, testSource.getTileYMax(1));
|
---|
131 | assertEquals("TileXMax", 2, testSource.getTileXMax(2));
|
---|
132 | assertEquals("TileYMax", 2, testSource.getTileYMax(2));
|
---|
133 | assertEquals("TileXMax", 4, testSource.getTileXMax(3));
|
---|
134 | assertEquals("TileYMax", 4, testSource.getTileYMax(3));
|
---|
135 | }
|
---|
136 |
|
---|
137 | @Test
|
---|
138 | public void testGeoportalTOPOPL() throws IOException {
|
---|
139 | Main.setProjection(Projections.getProjectionByCode("EPSG:4326"));
|
---|
140 | WMTSTileSource testSource = new WMTSTileSource(testImageryTOPO_PL);
|
---|
141 | verifyTile(new LatLon(56, 12), testSource, 0, 0, 1);
|
---|
142 | verifyTile(new LatLon(56, 12), testSource, 0, 0, 2);
|
---|
143 | verifyTile(new LatLon(51.1268639, 16.8731360), testSource, 1, 1, 2);
|
---|
144 |
|
---|
145 | assertEquals("TileXMax", 37, testSource.getTileXMax(1));
|
---|
146 | assertEquals("TileYMax", 19, testSource.getTileYMax(1));
|
---|
147 | assertEquals("TileXMax", 74, testSource.getTileXMax(2));
|
---|
148 | assertEquals("TileYMax", 37, testSource.getTileYMax(2));
|
---|
149 | assertEquals("TileXMax", 148, testSource.getTileXMax(3));
|
---|
150 | assertEquals("TileYMax", 74, testSource.getTileYMax(3));
|
---|
151 | assertEquals(
|
---|
152 | "http://mapy.geoportal.gov.pl/wss/service/WMTS/guest/wmts/TOPO?SERVICE=WMTS&REQUEST=GetTile&"
|
---|
153 | + "VERSION=1.0.0&LAYER=MAPA TOPOGRAFICZNA&STYLE=&FORMAT=image/jpeg&tileMatrixSet=EPSG:4326&"
|
---|
154 | + "tileMatrix=EPSG:4326:0&tileRow=1&tileCol=1",
|
---|
155 | testSource.getTileUrl(1, 1, 1));
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Test
|
---|
159 | public void testGeoportalORTOPL4326() throws IOException {
|
---|
160 | Main.setProjection(Projections.getProjectionByCode("EPSG:4326"));
|
---|
161 | WMTSTileSource testSource = new WMTSTileSource(testImageryORTO_PL);
|
---|
162 | verifyTile(new LatLon(53.5993712684958, 19.560669777688176), testSource, 12412, 3941, 14);
|
---|
163 |
|
---|
164 | verifyTile(new LatLon(49.783096954497786, 22.79034127751704), testSource, 17714, 10206, 14);
|
---|
165 | }
|
---|
166 |
|
---|
167 | @Test
|
---|
168 | public void testGeoportalORTOPL2180() throws IOException {
|
---|
169 | Main.setProjection(Projections.getProjectionByCode("EPSG:2180"));
|
---|
170 | WMTSTileSource testSource = new WMTSTileSource(testImageryORTO_PL);
|
---|
171 |
|
---|
172 | verifyTile(new LatLon(53.59940948387726, 19.560544913270064), testSource, 6453, 3140, 14);
|
---|
173 | verifyTile(new LatLon(49.782984840526055, 22.790064966993445), testSource, 9932, 9305, 14);
|
---|
174 | }
|
---|
175 |
|
---|
176 | private void verifyTile(LatLon expected, WMTSTileSource source, int x, int y, int z) {
|
---|
177 | LatLon ll = new LatLon(source.tileXYToLatLon(x, y, z));
|
---|
178 | assertEquals("Latitude", expected.lat(), ll.lat(), 1e-05);
|
---|
179 | assertEquals("Longitude", expected.lon(), ll.lon(), 1e-05);
|
---|
180 |
|
---|
181 | }
|
---|
182 |
|
---|
183 | private void verifyMercatorTile(WMTSTileSource testSource, int x, int y, int z) {
|
---|
184 | verifyMercatorTile(testSource, x, y, z, -1);
|
---|
185 | }
|
---|
186 |
|
---|
187 | private void verifyMercatorTile(WMTSTileSource testSource, int x, int y, int z, int zoomOffset) {
|
---|
188 | TemplatedTMSTileSource verifier = new TemplatedTMSTileSource(testImageryTMS);
|
---|
189 | LatLon result = new LatLon(testSource.tileXYToLatLon(x, y, z));
|
---|
190 | LatLon expected = new LatLon(verifier.tileXYToLatLon(x, y, z + zoomOffset));
|
---|
191 | System.out.println(z + "/" + x + "/" + y + " - result: " + result.toDisplayString() + " osmMercator: " + expected.toDisplayString());
|
---|
192 | assertEquals("Longitude" , expected.lon(), result.lon(), 1e-04);
|
---|
193 | assertEquals("Latitude", expected.lat(), result.lat(), 1e-04);
|
---|
194 | //assertTrue("result: " + result.toDisplayString() + " osmMercator: " + expected.toDisplayString(), result.equalsEpsilon(expected));
|
---|
195 | // LatLon tileCenter = new Bounds(result, new LatLon(testSource.tileXYToLatLon(x+1, y+1, z))).getCenter();
|
---|
196 | // TileXY backwardsResult = testSource.latLonToTileXY(tileCenter.toCoordinate(), z);
|
---|
197 | //assertEquals(x, backwardsResult.getXIndex());
|
---|
198 | //assertEquals(y, backwardsResult.getYIndex());
|
---|
199 | }
|
---|
200 | }
|
---|