org.openstreetmap.gui.jmapviewer
Class OsmMercator

java.lang.Object
  extended by org.openstreetmap.gui.jmapviewer.OsmMercator

public class OsmMercator
extends java.lang.Object

This class implements the Mercator Projection as it is used by Openstreetmap (and google). It provides methods to translate coordinates from 'map space' into latitude and longitude (on the WGS84 ellipsoid) and vice versa. Map space is measured in pixels. The origin of the map space is the top left corner. The map space origin (0,0) has latitude ~85 and longitude -180


Field Summary
private static double EARTH_RADIUS
           
static double MAX_LAT
           
static double MIN_LAT
           
private static int TILE_SIZE
           
 
Constructor Summary
OsmMercator()
           
 
Method Summary
static int falseEasting(int aZoomlevel)
           
static int falseNorthing(int aZoomlevel)
           
static double getDistance(double la1, double lo1, double la2, double lo2)
          Gets the distance using Spherical law of cosines.
static double getDistance(int x1, int y1, int x2, int y2, int zoomLevel)
          Transform pixelspace to coordinates and get the distance.
static int getMaxPixels(int aZoomlevel)
          Returns the absolut number of pixels in y or x, defined as: 2^Zoomlevel * TILE_WIDTH where TILE_WIDTH is the width of a tile in pixels
static int LatToY(double aLat, int aZoomlevel)
          Transforms latitude to pixelspace
static int LonToX(double aLongitude, int aZoomlevel)
          Transform longitude to pixelspace
static double radius(int aZoomlevel)
           
static double XToLon(int aX, int aZoomlevel)
          Transforms pixel coordinate X to longitude
static double YToLat(int aY, int aZoomlevel)
          Transforms pixel coordinate Y to latitude
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TILE_SIZE

private static int TILE_SIZE

MAX_LAT

public static final double MAX_LAT
See Also:
Constant Field Values

MIN_LAT

public static final double MIN_LAT
See Also:
Constant Field Values

EARTH_RADIUS

private static double EARTH_RADIUS
Constructor Detail

OsmMercator

public OsmMercator()
Method Detail

radius

public static double radius(int aZoomlevel)

getMaxPixels

public static int getMaxPixels(int aZoomlevel)
Returns the absolut number of pixels in y or x, defined as: 2^Zoomlevel * TILE_WIDTH where TILE_WIDTH is the width of a tile in pixels

Parameters:
aZoomlevel - zoom level to request pixel data
Returns:
number of pixels

falseEasting

public static int falseEasting(int aZoomlevel)

falseNorthing

public static int falseNorthing(int aZoomlevel)

getDistance

public static double getDistance(int x1,
                                 int y1,
                                 int x2,
                                 int y2,
                                 int zoomLevel)
Transform pixelspace to coordinates and get the distance.

Parameters:
x1 - the first x coordinate
y1 - the first y coordinate
x2 - the second x coordinate
y2 - the second y coordinate
zoomLevel - the zoom level
Returns:
the distance

getDistance

public static double getDistance(double la1,
                                 double lo1,
                                 double la2,
                                 double lo2)
Gets the distance using Spherical law of cosines.

Parameters:
la1 - the Latitude in degrees
lo1 - the Longitude in degrees
la2 - the Latitude from 2nd coordinate in degrees
lo2 - the Longitude from 2nd coordinate in degrees
Returns:
the distance

LonToX

public static int LonToX(double aLongitude,
                         int aZoomlevel)
Transform longitude to pixelspace

Mathematical optimization
x = radius(aZoomlevel) * toRadians(aLongitude) + falseEasting(aZoomLevel)
x = getMaxPixels(aZoomlevel) / (2 * PI) * (aLongitude * PI) / 180 + getMaxPixels(aZoomlevel) / 2
x = getMaxPixels(aZoomlevel) * aLongitude / 360 + 180 * getMaxPixels(aZoomlevel) / 360
x = getMaxPixels(aZoomlevel) * (aLongitude + 180) / 360

Parameters:
aLongitude - [-180..180]
Returns:
[0..2^Zoomlevel*TILE_SIZE[

LatToY

public static int LatToY(double aLat,
                         int aZoomlevel)
Transforms latitude to pixelspace

Mathematical optimization
log(u) := log((1.0 + sin(toRadians(aLat))) / (1.0 - sin(toRadians(aLat))
y = -1 * (radius(aZoomlevel) / 2 * log(u)))) - falseNorthing(aZoomlevel))
y = -1 * (getMaxPixel(aZoomlevel) / 2 * PI / 2 * log(u)) - -1 * getMaxPixel(aZoomLevel) / 2
y = getMaxPixel(aZoomlevel) / (-4 * PI) * log(u)) + getMaxPixel(aZoomLevel) / 2
y = getMaxPixel(aZoomlevel) * ((log(u) / (-4 * PI)) + 1/2)

Parameters:
aLat - [-90...90]
Returns:
[0..2^Zoomlevel*TILE_SIZE[

XToLon

public static double XToLon(int aX,
                            int aZoomlevel)
Transforms pixel coordinate X to longitude

Mathematical optimization
lon = toDegree((aX - falseEasting(aZoomlevel)) / radius(aZoomlevel))
lon = 180 / PI * ((aX - getMaxPixels(aZoomlevel) / 2) / getMaxPixels(aZoomlevel) / (2 * PI)
lon = 180 * ((aX - getMaxPixels(aZoomlevel) / 2) / getMaxPixels(aZoomlevel))
lon = 360 / getMaxPixels(aZoomlevel) * (aX - getMaxPixels(aZoomlevel) / 2)
lon = 360 * aX / getMaxPixels(aZoomlevel) - 180

Parameters:
aX - [0..2^Zoomlevel*TILE_WIDTH[
Returns:
]-180..180[

YToLat

public static double YToLat(int aY,
                            int aZoomlevel)
Transforms pixel coordinate Y to latitude

Parameters:
aY - [0..2^Zoomlevel*TILE_WIDTH[
Returns:
[MIN_LAT..MAX_LAT] is about [-85..85]


JOSM