| 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.Rule;
|
|---|
| 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.Main;
|
|---|
| 13 | import org.openstreetmap.josm.data.Bounds;
|
|---|
| 14 | import org.openstreetmap.josm.data.coor.EastNorth;
|
|---|
| 15 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 16 | import org.openstreetmap.josm.data.projection.CustomProjection;
|
|---|
| 17 | import org.openstreetmap.josm.data.projection.Projection;
|
|---|
| 18 | import org.openstreetmap.josm.data.projection.Projections;
|
|---|
| 19 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
|---|
| 20 |
|
|---|
| 21 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
|---|
| 22 |
|
|---|
| 23 | /**
|
|---|
| 24 | * Unit tests for class {@link TemplatedWMSTileSource}.
|
|---|
| 25 | */
|
|---|
| 26 | public class TemplatedWMSTileSourceTest {
|
|---|
| 27 |
|
|---|
| 28 | private final ImageryInfo testImageryWMS = new ImageryInfo("test imagery", "http://localhost", "wms", null, null);
|
|---|
| 29 | private final ImageryInfo testImageryTMS = new ImageryInfo("test imagery", "http://localhost", "tms", null, null);
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * Setup test.
|
|---|
| 33 | */
|
|---|
| 34 | @Rule
|
|---|
| 35 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
|---|
| 36 | public JOSMTestRules test = new JOSMTestRules();
|
|---|
| 37 |
|
|---|
| 38 | /**
|
|---|
| 39 | * Test EPSG:3857
|
|---|
| 40 | */
|
|---|
| 41 | @Test
|
|---|
| 42 | public void testEPSG3857() {
|
|---|
| 43 | Projection projection = Projections.getProjectionByCode("EPSG:3857");
|
|---|
| 44 | Main.setProjection(projection);
|
|---|
| 45 | TemplatedWMSTileSource source = new TemplatedWMSTileSource(testImageryWMS, projection);
|
|---|
| 46 | verifyMercatorTile(source, 0, 0, 1);
|
|---|
| 47 | verifyMercatorTile(source, 0, 0, 2);
|
|---|
| 48 | verifyMercatorTile(source, 0, 1, 2);
|
|---|
| 49 | verifyMercatorTile(source, 1, 0, 2);
|
|---|
| 50 | verifyMercatorTile(source, 1, 1, 2);
|
|---|
| 51 | for (int x = 0; x < 4; x++) {
|
|---|
| 52 | for (int y = 0; y < 4; y++) {
|
|---|
| 53 | verifyMercatorTile(source, x, y, 3);
|
|---|
| 54 | verifyTileSquarness(source, x, y, 3);
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 | verifyTileSquarness(source, 150, 20, 18);
|
|---|
| 58 | verifyTileSquarness(source, 2270, 1323, 12);
|
|---|
| 59 | verifyLocation(source, new LatLon(53.5937132, 19.5652017));
|
|---|
| 60 | verifyLocation(source, new LatLon(53.501565692302854, 18.54455233898721));
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * Test EPSG:4326
|
|---|
| 65 | */
|
|---|
| 66 | @Test
|
|---|
| 67 | public void testEPSG4326() {
|
|---|
| 68 | Projection projection = Projections.getProjectionByCode("EPSG:4326");
|
|---|
| 69 | Main.setProjection(projection);
|
|---|
| 70 | TemplatedWMSTileSource source = getSource(projection);
|
|---|
| 71 |
|
|---|
| 72 | verifyLocation(source, new LatLon(53.5937132, 19.5652017));
|
|---|
| 73 | verifyLocation(source, new LatLon(53.501565692302854, 18.54455233898721));
|
|---|
| 74 | verifyTileSquarness(source, 2, 2, 2);
|
|---|
| 75 | verifyTileSquarness(source, 150, 20, 18);
|
|---|
| 76 | verifyTileSquarness(source, 2270, 1323, 12);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /**
|
|---|
| 80 | * Test EPSG:4326 - wide bounds
|
|---|
| 81 | */
|
|---|
| 82 | @Test
|
|---|
| 83 | public void testEPSG4326widebounds() {
|
|---|
| 84 | Projection projection = new CustomProjection("+proj=lonlat +datum=WGS84 +axis=neu +bounds=-180,53,180,54");
|
|---|
| 85 | Main.setProjection(projection);
|
|---|
| 86 | TemplatedWMSTileSource source = getSource(projection);
|
|---|
| 87 |
|
|---|
| 88 | verifyLocation(source, new LatLon(53.5937132, 19.5652017));
|
|---|
| 89 | verifyLocation(source, new LatLon(53.501565692302854, 18.54455233898721));
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /**
|
|---|
| 93 | * Test EPSG:4326 - narrow bounds
|
|---|
| 94 | */
|
|---|
| 95 | @Test
|
|---|
| 96 | public void testEPSG4326narrowbounds() {
|
|---|
| 97 | Projection projection = new CustomProjection("+proj=lonlat +datum=WGS84 +axis=neu +bounds=18,-90,20,90");
|
|---|
| 98 | Main.setProjection(projection);
|
|---|
| 99 | TemplatedWMSTileSource source = getSource(projection);
|
|---|
| 100 |
|
|---|
| 101 | verifyLocation(source, new LatLon(53.5937132, 19.5652017));
|
|---|
| 102 | verifyLocation(source, new LatLon(53.501565692302854, 18.54455233898721));
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /**
|
|---|
| 106 | * Test EPSG:2180
|
|---|
| 107 | */
|
|---|
| 108 | @Test
|
|---|
| 109 | public void testEPSG2180() {
|
|---|
| 110 | Projection projection = Projections.getProjectionByCode("EPSG:2180");
|
|---|
| 111 | Main.setProjection(projection);
|
|---|
| 112 | TemplatedWMSTileSource source = getSource(projection);
|
|---|
| 113 |
|
|---|
| 114 | verifyLocation(source, new LatLon(53.5937132, 19.5652017));
|
|---|
| 115 | verifyLocation(source, new LatLon(53.501565692302854, 18.54455233898721));
|
|---|
| 116 |
|
|---|
| 117 | verifyTileSquarness(source, 2, 2, 2);
|
|---|
| 118 | verifyTileSquarness(source, 150, 20, 18);
|
|---|
| 119 | verifyTileSquarness(source, 2270, 1323, 12);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | * Test EPSG:3006 with bounds
|
|---|
| 124 | */
|
|---|
| 125 | @Test
|
|---|
| 126 | public void testEPSG3006withbounds() {
|
|---|
| 127 | Projection projection =
|
|---|
| 128 | new CustomProjection("+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 "
|
|---|
| 129 | + "+units=m +no_defs +axis=neu +wmssrs=EPSG:3006 +bounds=10.5700,55.2000,24.1800,69.1000 ");
|
|---|
| 130 | Main.setProjection(projection);
|
|---|
| 131 | TemplatedWMSTileSource source = getSource(projection);
|
|---|
| 132 |
|
|---|
| 133 | verifyTileSquarness(source, 0, 1, 4);
|
|---|
| 134 | verifyLocation(source, new LatLon(60, 18.1), 3);
|
|---|
| 135 | verifyLocation(source, new LatLon(60, 18.1));
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | /**
|
|---|
| 139 | * Test EPSG:3006 without bounds
|
|---|
| 140 | */
|
|---|
| 141 | @Test
|
|---|
| 142 | public void testEPSG3006withoutbounds() {
|
|---|
| 143 | Projection projection =
|
|---|
| 144 | new CustomProjection("+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 "
|
|---|
| 145 | + "+units=m +no_defs +axis=neu +wmssrs=EPSG:3006");
|
|---|
| 146 | Main.setProjection(projection);
|
|---|
| 147 | TemplatedWMSTileSource source = getSource(projection);
|
|---|
| 148 |
|
|---|
| 149 | verifyTileSquarness(source, 0, 1, 4);
|
|---|
| 150 | verifyLocation(source, new LatLon(60, 18.1), 3);
|
|---|
| 151 | verifyLocation(source, new LatLon(60, 18.1));
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | private void verifyMercatorTile(TemplatedWMSTileSource source, int x, int y, int z) {
|
|---|
| 155 | TemplatedTMSTileSource verifier = new TemplatedTMSTileSource(testImageryTMS);
|
|---|
| 156 | LatLon result = getTileLatLon(source, x, y, z);
|
|---|
| 157 | ICoordinate expected = verifier.tileXYToLatLon(x, y, z - 1);
|
|---|
| 158 | assertEquals(expected.getLat(), result.lat(), 1e-4);
|
|---|
| 159 | assertEquals(LatLon.normalizeLon(expected.getLon() - result.lon()), 0.0, 1e-4);
|
|---|
| 160 | LatLon tileCenter = new Bounds(result, getTileLatLon(source, x+1, y+1, z)).getCenter();
|
|---|
| 161 | TileXY backwardsResult = source.latLonToTileXY(tileCenter.toCoordinate(), z);
|
|---|
| 162 | assertEquals(x, backwardsResult.getXIndex());
|
|---|
| 163 | assertEquals(y, backwardsResult.getYIndex());
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | private void verifyLocation(TemplatedWMSTileSource source, LatLon location) {
|
|---|
| 167 | for (int z = source.getMaxZoom(); z > source.getMinZoom() + 1; z--) {
|
|---|
| 168 | if (source.getTileXMax(z) != source.getTileXMin(z) && source.getTileYMax(z) != source.getTileYMin(z)) {
|
|---|
| 169 | // do the tests only where there is more than one tile
|
|---|
| 170 | verifyLocation(source, location, z);
|
|---|
| 171 | }
|
|---|
| 172 | }
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | private void verifyLocation(TemplatedWMSTileSource source, LatLon location, int z) {
|
|---|
| 176 | assertTrue(
|
|---|
| 177 | "Point outside world bounds",
|
|---|
| 178 | Main.getProjection().getWorldBoundsLatLon().contains(location)
|
|---|
| 179 | );
|
|---|
| 180 |
|
|---|
| 181 | TileXY tileIndex = source.latLonToTileXY(location.toCoordinate(), z);
|
|---|
| 182 |
|
|---|
| 183 | assertTrue("X index: " + tileIndex.getXIndex() + " greater than tileXmax: " + source.getTileXMax(z) + " at zoom: " + z,
|
|---|
| 184 | tileIndex.getXIndex() <= source.getTileXMax(z));
|
|---|
| 185 |
|
|---|
| 186 | assertTrue("Y index: " + tileIndex.getYIndex() + " greater than tileYmax: " + source.getTileYMax(z) + " at zoom: " + z,
|
|---|
| 187 | tileIndex.getYIndex() <= source.getTileYMax(z));
|
|---|
| 188 |
|
|---|
| 189 | EastNorth locationEN = Main.getProjection().latlon2eastNorth(location);
|
|---|
| 190 | EastNorth x1 = Main.getProjection().latlon2eastNorth(getTileLatLon(source, tileIndex, z));
|
|---|
| 191 | EastNorth x2 = Main.getProjection().latlon2eastNorth(getTileLatLon(source, tileIndex.getXIndex() + 1, tileIndex.getYIndex() + 1, z));
|
|---|
| 192 | // test that location is within tile bounds
|
|---|
| 193 | assertTrue(locationEN.toString() + " not within " + bboxStr(x1, x2) +
|
|---|
| 194 | " for tile " + z + "/" + tileIndex.getXIndex() + "/" + tileIndex.getYIndex(),
|
|---|
| 195 | isWithin(locationEN, x1, x2));
|
|---|
| 196 | verifyTileSquarness(source, tileIndex.getXIndex(), tileIndex.getYIndex(), z);
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | private static boolean isWithin(EastNorth point, EastNorth topLeft, EastNorth bottomRight) {
|
|---|
| 200 | return Math.min(topLeft.east(), bottomRight.east()) <= point.east() &&
|
|---|
| 201 | point.east() <= Math.max(topLeft.east(), bottomRight.east()) &&
|
|---|
| 202 | Math.min(topLeft.north(), bottomRight.north()) <= point.north() &&
|
|---|
| 203 | point.north() <= Math.max(topLeft.north(), bottomRight.north());
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | private static String bboxStr(EastNorth x1, EastNorth x2) {
|
|---|
| 207 | return "[" + x1.east() +", " + x1.north() + ", " + x2.east() + ", " + x2.north() +"]";
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | private LatLon getTileLatLon(TemplatedWMSTileSource source, TileXY tileIndex, int z) {
|
|---|
| 211 | return getTileLatLon(source, tileIndex.getXIndex(), tileIndex.getYIndex(), z);
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | private LatLon getTileLatLon(TemplatedWMSTileSource source, int x, int y, int z) {
|
|---|
| 215 | return new LatLon(source.tileXYToLatLon(x, y, z));
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | private void verifyTileSquarness(TemplatedWMSTileSource source, int x, int y, int z) {
|
|---|
| 219 | /**
|
|---|
| 220 | * t1 | t2
|
|---|
| 221 | * -------
|
|---|
| 222 | * t3 | t4
|
|---|
| 223 | */
|
|---|
| 224 | EastNorth t1 = source.getTileEastNorth(x, y, z);
|
|---|
| 225 | EastNorth t2 = source.getTileEastNorth(x + 1, y, z);
|
|---|
| 226 | EastNorth t3 = source.getTileEastNorth(x, y + 1, z);
|
|---|
| 227 | EastNorth t4 = source.getTileEastNorth(x + 1, y + 1, z);
|
|---|
| 228 | double y_size = Math.abs(t1.getY() - t4.getY());
|
|---|
| 229 | double x_size = Math.abs(t1.getX() - t4.getX());
|
|---|
| 230 |
|
|---|
| 231 | assertEquals(x_size, y_size, Math.max(x_size, y_size) * 1e-06);
|
|---|
| 232 | assertEquals(y_size, Math.abs(t1.getY() - t3.getY()), y_size * 1e-06);
|
|---|
| 233 | assertEquals(x_size, Math.abs(t1.getX() - t2.getX()), x_size * 1e-06);
|
|---|
| 234 |
|
|---|
| 235 | t1 = source.getTileEastNorth(x, y, z);
|
|---|
| 236 | t2 = source.getTileEastNorth(x + 1, y, z);
|
|---|
| 237 | t3 = source.getTileEastNorth(x, y + 1, z);
|
|---|
| 238 | t4 = source.getTileEastNorth(x + 1, y + 1, z);
|
|---|
| 239 | y_size = Math.abs(t1.getY() - t4.getY());
|
|---|
| 240 | x_size = Math.abs(t1.getX() - t4.getX());
|
|---|
| 241 | assertEquals(x_size, y_size, Math.max(x_size, y_size) * 1e-05);
|
|---|
| 242 | assertEquals(y_size, Math.abs(t1.getY() - t3.getY()), y_size * 1e-05);
|
|---|
| 243 | assertEquals(x_size, Math.abs(t1.getX() - t2.getX()), x_size * 1e-05);
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | private TemplatedWMSTileSource getSource(Projection projection) {
|
|---|
| 247 | return new TemplatedWMSTileSource(testImageryWMS, projection);
|
|---|
| 248 | }
|
|---|
| 249 | }
|
|---|