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