Ignore:
Timestamp:
2011-12-28T22:33:01+01:00 (12 years ago)
Author:
bastiK
Message:

fixed #7198 - latitude problem for south-west areas

File:
1 edited

Legend:

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

    r4706 r4736  
    88import java.util.Map;
    99
     10import org.openstreetmap.josm.Main;
    1011import org.openstreetmap.josm.data.Bounds;
    1112import org.openstreetmap.josm.data.coor.LatLon;
     
    6061            }
    6162        } catch (NumberFormatException x) {
     63            x.printStackTrace();
    6264        } catch (NullPointerException x) {
     65            x.printStackTrace();
    6366        } catch (ArrayIndexOutOfBoundsException x) {
     67            x.printStackTrace();
    6468        }
    6569        return b;
     
    141145    }
    142146
     147    public static final double R = 6378137.0;
     148
    143149    public static Bounds positionToBounds(final double lat, final double lon, final int zoom) {
    144150        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);
    150         return new Bounds(
    151                 getLatLonOfTile(center.a - deltaX, center.b - deltaY, zoom),
    152                 getLatLonOfTile(center.a + deltaX, center.b + deltaY, zoom));
     151        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
     152        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
     153        if (Main.map != null && Main.map.mapView != null) {
     154            height = Main.map.mapView.getHeight();
     155            width = Main.map.mapView.getWidth();
     156        }
     157        double scale = (1 << zoom) * tileSizeInPixels / (2 * Math.PI * R);
     158        double deltaX = width / 2.0 / scale;
     159        double deltaY = height / 2.0 / scale;
     160        double x = Math.toRadians(lon) * R;
     161        double y = mercatorY(lat);
     162        return new Bounds(invMercatorY(y - deltaY), Math.toDegrees(x - deltaX) / R, invMercatorY(y + deltaY), Math.toDegrees(x + deltaX) / R);
     163    }
     164
     165    public static double mercatorY(double lat) {
     166        return Math.log(Math.tan(Math.PI/4 + Math.toRadians(lat)/2)) * R;
     167    }
     168
     169    public static double invMercatorY(double north) {
     170        return Math.toDegrees(Math.atan(Math.sinh(north / R)));
    153171    }
    154172
Note: See TracChangeset for help on using the changeset viewer.