Changeset 4706 in josm


Ignore:
Timestamp:
Dec 24, 2011 12:24:50 AM (18 months ago)
Author:
simon04
Message:

fix #3595 - JOSM downloads too small areas when pasting OSM link

File:
1 edited

Legend:

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

    r4298 r4706  
    22package org.openstreetmap.josm.tools; 
    33 
     4import java.awt.Toolkit; 
    45import java.io.UnsupportedEncodingException; 
    56import java.net.URLDecoder; 
     
    141142 
    142143    public static Bounds positionToBounds(final double lat, final double lon, final int zoom) { 
    143         final double size = 180.0 / Math.pow(2, zoom); 
     144        int tileSizeInPixels = 256; 
     145        int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height; 
     146        int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width; 
     147        double deltaX = screenWidth / 2. / tileSizeInPixels; 
     148        double deltaY = screenHeight / 2. / tileSizeInPixels; 
     149        Pair<Double, Double> center = getTileOfLatLon(lat, lon, zoom); 
    144150        return new Bounds( 
    145                 new LatLon(lat - size/2, lon - size), 
    146                 new LatLon(lat + size/2, lon + size)); 
     151                getLatLonOfTile(center.a - deltaX, center.b - deltaY, zoom), 
     152                getLatLonOfTile(center.a + deltaX, center.b + deltaY, zoom)); 
     153    } 
     154 
     155    public static Pair<Double, Double> getTileOfLatLon(double lat, double lon, double zoom) { 
     156        double x = Math.floor((lon + 180) / 360 * Math.pow(2.0, zoom)); 
     157        double y = Math.floor((1 - Math.log(Math.tan(Math.toRadians(lat)) + 1 / Math.cos(Math.toRadians(lat))) / Math.PI) 
     158                / 2 * Math.pow(2.0, zoom)); 
     159        return new Pair<Double, Double>(x, y); 
     160    } 
     161 
     162    public static LatLon getLatLonOfTile(double x, double y, double zoom) { 
     163        double lon = x / Math.pow(2.0, zoom) * 360.0 - 180; 
     164        double lat = Math.toDegrees(Math.atan(Math.sinh(Math.PI - (2.0 * Math.PI * y) / Math.pow(2.0, zoom)))); 
     165        return new LatLon(lat, lon); 
    147166    } 
    148167 
Note: See TracChangeset for help on using the changeset viewer.