Changeset 11302 in josm


Ignore:
Timestamp:
2016-11-23T23:39:53+01:00 (3 years ago)
Author:
simon04
Message:

OsmUrlToBounds: Make getZoom return the previously parsed zoom level

Might be a regression of r4706.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/OsmUrlToBounds.java

    r11299 r11302  
    191191    }
    192192
     193    private static Dimension getScreenSize() {
     194        if (Main.isDisplayingMapView()) {
     195            return new Dimension(Main.map.mapView.getWidth(), Main.map.mapView.getHeight());
     196        } else {
     197            return GuiHelper.getScreenSize();
     198        }
     199    }
     200
     201    private static final int TILE_SIZE_IN_PIXELS = 256;
     202
    193203    public static Bounds positionToBounds(final double lat, final double lon, final int zoom) {
    194         int tileSizeInPixels = 256;
    195         Dimension screenSize = GuiHelper.getScreenSize();
    196         int height = screenSize.height;
    197         int width = screenSize.width;
    198         if (Main.isDisplayingMapView()) {
    199             height = Main.map.mapView.getHeight();
    200             width = Main.map.mapView.getWidth();
    201         }
    202         double scale = (1 << zoom) * tileSizeInPixels / (2 * Math.PI * Ellipsoid.WGS84.a);
    203         double deltaX = width / 2.0 / scale;
    204         double deltaY = height / 2.0 / scale;
     204        final Dimension screenSize = getScreenSize();
     205        double scale = (1 << zoom) * TILE_SIZE_IN_PIXELS / (2 * Math.PI * Ellipsoid.WGS84.a);
     206        double deltaX = screenSize.getWidth() / 2.0 / scale;
     207        double deltaY = screenSize.getHeight() / 2.0 / scale;
    205208        final Projection mercator = Projections.getProjectionByCode("EPSG:3857");
    206209        final EastNorth projected = mercator.latlon2eastNorth(new LatLon(lat, lon));
     
    210213    }
    211214
    212     public static Pair<Double, Double> getTileOfLatLon(double lat, double lon, double zoom) {
    213         double x = Math.floor((lon + 180) / 360 * Math.pow(2.0, zoom));
    214         double y = Math.floor((1 - Math.log(Math.tan(Math.toRadians(lat)) + 1 / Math.cos(Math.toRadians(lat))) / Math.PI)
    215                 / 2 * Math.pow(2.0, zoom));
    216         return new Pair<>(x, y);
    217     }
    218 
    219     public static LatLon getLatLonOfTile(double x, double y, double zoom) {
    220         double lon = x / Math.pow(2.0, zoom) * 360.0 - 180;
    221         double lat = Math.toDegrees(Math.atan(Math.sinh(Math.PI - (2.0 * Math.PI * y) / Math.pow(2.0, zoom))));
    222         return new LatLon(lat, lon);
    223     }
    224 
    225215    /**
    226216     * Return OSM Zoom level for a given area
     
    230220     */
    231221    public static int getZoom(Bounds b) {
    232         // convert to mercator (for calculation of zoom only)
    233         double latMin = Math.log(Math.tan(Math.PI/4.0+b.getMinLat()/180.0*Math.PI/2.0))*180.0/Math.PI;
    234         double latMax = Math.log(Math.tan(Math.PI/4.0+b.getMaxLat()/180.0*Math.PI/2.0))*180.0/Math.PI;
    235         double size = Math.max(Math.abs(latMax-latMin), Math.abs(b.getMaxLon()-b.getMinLon()));
    236         int zoom = 0;
    237         while (zoom <= 20) {
    238             if (size >= 180) {
    239                 break;
    240             }
    241             size *= 2;
    242             zoom++;
    243         }
    244         return zoom;
     222        final Projection mercator = Projections.getProjectionByCode("EPSG:3857");
     223        final EastNorth min = mercator.latlon2eastNorth(b.getMin());
     224        final EastNorth max = mercator.latlon2eastNorth(b.getMax());
     225        final double deltaX = max.getX() - min.getX();
     226        final double scale = getScreenSize().getWidth() / deltaX;
     227        final double x = scale * (2 * Math.PI * Ellipsoid.WGS84.a) / TILE_SIZE_IN_PIXELS;
     228        return (int) Math.round(Math.log(x) / Math.log(2));
    245229    }
    246230
  • trunk/test/unit/org/openstreetmap/josm/tools/OsmUrlToBoundsTest.java

    r11299 r11302  
    100100        }
    101101    }
     102
     103    /**
     104     * Test for {@link OsmUrlToBounds#getZoom}.
     105     */
     106    @Test
     107    public void testGetZoom() {
     108        Assert.assertEquals(4, OsmUrlToBounds.getZoom(OsmUrlToBounds.positionToBounds(0, 0, 4)));
     109        Assert.assertEquals(10, OsmUrlToBounds.getZoom(OsmUrlToBounds.positionToBounds(5, 5, 10)));
     110        Assert.assertEquals(18, OsmUrlToBounds.getZoom(OsmUrlToBounds.positionToBounds(40, 20, 18)));
     111    }
    102112}
Note: See TracChangeset for help on using the changeset viewer.