mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Created Mercator projection to convert between Geo location and planar projection point.
- MapGeo and MapPoint encapsulate geo location and planar projection point into classes. #story[928]
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package seng302.models.map;
|
||||
|
||||
public class MercatorProjection {
|
||||
|
||||
private double MERCATOR_RANGE = 256;
|
||||
private double pixelsPerLngDegree, pixelsPerLngRadian;
|
||||
|
||||
|
||||
public MercatorProjection() {
|
||||
pixelsPerLngDegree = MERCATOR_RANGE / 360.0;
|
||||
pixelsPerLngRadian = MERCATOR_RANGE / (2 * Math.PI);
|
||||
}
|
||||
|
||||
/**
|
||||
* A help function keeps the value in bound between -0.9999 and 0.9999.
|
||||
* @param value in bound value
|
||||
* @return the value in bound
|
||||
*/
|
||||
private double bound(double value) {
|
||||
return Math.min(Math.max(value, -0.9999), 0.9999);
|
||||
}
|
||||
|
||||
/**
|
||||
* Projects a Geo Location (lat, lng) on a planar
|
||||
* @param geo MapGeo (lat, lng) location to be projected
|
||||
* @return the projection GeoPoint (x, y) on planar
|
||||
*/
|
||||
public MapPoint toMapPoint(MapGeo geo) {
|
||||
MapPoint point = new MapPoint(0, 0);
|
||||
MapPoint origin = new MapPoint(MERCATOR_RANGE / 2.0, MERCATOR_RANGE / 2.0);
|
||||
point.setX(origin.getX() + geo.getLng() * pixelsPerLngDegree);
|
||||
|
||||
// NOTE(appleton): Truncating to 0.9999 effectively limits latitude to
|
||||
// 89.189. This is about a third of a tile past the edge of the world tile.
|
||||
double sinY = bound(Math.sin(Math.toRadians(geo.getLat())));
|
||||
point.setY(origin.getY() + 0.5 * Math.log((1 + sinY) / (1 - sinY)) * (-pixelsPerLngRadian));
|
||||
return point;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the planar projection (x, y) back to Geo Location (lat, lng)
|
||||
* @param point MapPoint (x, y) to be converted back
|
||||
* @return the original Geo location converted from the given projection point
|
||||
*/
|
||||
public MapGeo toMapGeo(MapPoint point) {
|
||||
MapPoint origin = new MapPoint(MERCATOR_RANGE / 2.0, MERCATOR_RANGE / 2.0);
|
||||
double lng = (point.getX() - origin.getX()) / pixelsPerLngDegree;
|
||||
double latRadians = (point.getY() - origin.getY()) / (-pixelsPerLngRadian);
|
||||
double lat = Math.toDegrees(2 * Math.atan(Math.exp(latRadians)) - Math.PI / 2.0);
|
||||
return new MapGeo(lat, lng);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user