Changeset 9167 in josm


Ignore:
Timestamp:
2015-12-26T20:41:45+01:00 (4 years ago)
Author:
wiktorn
Message:
  • Move TemplatedWMSTileSource to Projection.getWorldBoundsBoxEastNorth
  • fix failing tests - move to EastNorth computations instead of LatLon,

which might be non-square, which lead to trouble checking, whether point was
within the bounds or not

See #12186

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/imagery/TemplatedWMSTileSource.java

    r8846 r9167  
    1616import java.util.regex.Pattern;
    1717
    18 import org.openstreetmap.gui.jmapviewer.OsmMercator;
    1918import org.openstreetmap.gui.jmapviewer.Tile;
    2019import org.openstreetmap.gui.jmapviewer.TileXY;
     
    2322import org.openstreetmap.gui.jmapviewer.tilesources.TMSTileSource;
    2423import org.openstreetmap.josm.Main;
    25 import org.openstreetmap.josm.data.Bounds;
     24import org.openstreetmap.josm.data.ProjectionBounds;
    2625import org.openstreetmap.josm.data.coor.EastNorth;
    2726import org.openstreetmap.josm.data.coor.LatLon;
     
    4039    private final Set<String> serverProjections;
    4140    private EastNorth topLeftCorner;
    42     private Bounds worldBounds;
    4341    private int[] tileXMax;
    4442    private int[] tileYMax;
     
    9694     */
    9795    public void initProjection(Projection proj) {
    98         this.worldBounds = getWorldBounds();
    99         EastNorth min = proj.latlon2eastNorth(worldBounds.getMin());
    100         EastNorth max = proj.latlon2eastNorth(worldBounds.getMax());
     96        ProjectionBounds worldBounds = proj.getWorldBoundsBoxEastNorth();
     97        EastNorth min = worldBounds.getMin();
     98        EastNorth max = worldBounds.getMax();
    10199        this.topLeftCorner = new EastNorth(min.east(), max.north());
    102100
    103         LatLon bottomRight = new LatLon(worldBounds.getMinLat(), worldBounds.getMaxLon());
     101        EastNorth bottomRight = new EastNorth(worldBounds.getMax().east(), worldBounds.getMin().north());
    104102
    105103        // use 256 as "tile size" to keep the scale in line with default tiles in Mercator projection
     
    113111            // this makes the zoom levels "glued" to standard TMS zoom levels
    114112            degreesPerTile[zoom] = (SCALE_DENOMINATOR_ZOOM_LEVEL_1 / Math.pow(2, zoom - 1)) * crsScale;
    115             TileXY maxTileIndex = latLonToTileXY(bottomRight.toCoordinate(), zoom);
     113            TileXY maxTileIndex = eastNorthToTileXY(bottomRight, zoom);
    116114            tileXMax[zoom] = maxTileIndex.getXIndex();
    117115            tileYMax[zoom] = maxTileIndex.getYIndex();
     
    243241        Projection proj = Main.getProjection();
    244242        EastNorth enPoint = proj.latlon2eastNorth(new LatLon(lat, lon));
     243        return eastNorthToTileXY(enPoint, zoom);
     244    }
     245
     246    private TileXY eastNorthToTileXY(EastNorth enPoint, int zoom) {
    245247        double scale = getDegreesPerTile(zoom);
    246248        return new TileXY(
     
    356358        return degreesPerTile[zoom];
    357359    }
    358 
    359     /**
    360      * returns world bounds, but detect situation, when default bounds are provided (-90, -180, 90, 180), and projection
    361      * returns very close values for both min and max X. To work around this problem, cap this projection on north and south
    362      * pole, the same way they are capped in Mercator projection, so conversions should work properly
    363      */
    364     private static Bounds getWorldBounds() {
    365         Projection proj = Main.getProjection();
    366         Bounds bounds = proj.getWorldBoundsLatLon();
    367         EastNorth min = proj.latlon2eastNorth(bounds.getMin());
    368         EastNorth max = proj.latlon2eastNorth(bounds.getMax());
    369 
    370         if (Math.abs(min.getX() - max.getX()) < 1 && bounds.equals(new Bounds(new LatLon(-90, -180), new LatLon(90, 180)))) {
    371             return new Bounds(
    372                     new LatLon(OsmMercator.MIN_LAT, bounds.getMinLon()),
    373                     new LatLon(OsmMercator.MAX_LAT, bounds.getMaxLon())
    374                     );
    375         }
    376         return bounds;
    377     }
    378360}
  • trunk/test/unit/org/openstreetmap/josm/data/imagery/TemplatedWMSTileSourceTest.java

    r8857 r9167  
    1515import org.openstreetmap.josm.data.coor.EastNorth;
    1616import org.openstreetmap.josm.data.coor.LatLon;
    17 import org.openstreetmap.josm.data.osm.BBox;
    1817import org.openstreetmap.josm.data.projection.CustomProjection;
    1918import org.openstreetmap.josm.data.projection.Projection;
     
    107106        TemplatedWMSTileSource source = getSource();
    108107
    109         verifyLocation(source, new LatLon(60, 18), 3);
    110         verifyLocation(source, new LatLon(60, 18));
     108        verifyTileSquarness(source, 0, 1, 4);
     109        verifyLocation(source, new LatLon(60, 18.1), 3);
     110        verifyLocation(source, new LatLon(60, 18.1));
    111111    }
    112112
     
    158158                tileIndex.getYIndex() <= source.getTileYMax(z));
    159159
     160        EastNorth locationEN = Main.getProjection().latlon2eastNorth(location);
     161        EastNorth x1 = Main.getProjection().latlon2eastNorth(getTileLatLon(source, tileIndex, z));
     162        EastNorth x2 = Main.getProjection().latlon2eastNorth(getTileLatLon(source, tileIndex.getXIndex() + 1, tileIndex.getYIndex() + 1, z));
    160163        // test that location is within tile bounds
    161         BBox bbox = new BBox(
    162                 getTileLatLon(source, tileIndex, z),
    163                 getTileLatLon(source, tileIndex.getXIndex() + 1, tileIndex.getYIndex() + 1, z)
    164                 );
    165         assertTrue(location.toDisplayString() + " not within " + bbox.toString() +
     164        assertTrue(locationEN.toString() + " not within " + bboxStr(x1, x2) +
    166165                " for tile " + z + "/" + tileIndex.getXIndex() + "/" + tileIndex.getYIndex(),
    167                 bbox.bounds(location));
     166                isWithin(locationEN, x1, x2));
    168167        verifyTileSquarness(source, tileIndex.getXIndex(), tileIndex.getYIndex(), z);
     168    }
     169
     170    private static boolean isWithin(EastNorth point, EastNorth topLeft, EastNorth bottomRight) {
     171        return Math.min(topLeft.east(), bottomRight.east()) <= point.east() &&
     172                point.east() <= Math.max(topLeft.east(), bottomRight.east())  &&
     173                Math.min(topLeft.north(), bottomRight.north()) <= point.north() &&
     174                point.north() <= Math.max(topLeft.north(), bottomRight.north());
     175    }
     176
     177    private static String bboxStr(EastNorth x1, EastNorth x2) {
     178        return "[" + x1.east() +", " + x1.north() + ", " + x2.east() + ", " + x2.north() +"]";
    169179    }
    170180
Note: See TracChangeset for help on using the changeset viewer.