Skip to content

Instantly share code, notes, and snippets.

@leebyron
Created May 29, 2011 03:50
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leebyron/997450 to your computer and use it in GitHub Desktop.
Save leebyron/997450 to your computer and use it in GitHub Desktop.
conversions from Lat/long to 2d map projections - for Processing
/**
* A collection of functions responsible for performing translations between
* longitude/latitude coordinates and points on a 1/1 square
* (which you could then map to pixelspace)
*
* @author leebyron
*/
/**
* lon double x coordinate in radians [-PI,PI)
* lat double y coordinate in radians
* lonOrigin double the left most edge of the map in longitude radians
* @return [x,y]
*/
double[] millerEncode(double lon, double lat, double lonOrigin) {
double[] p = new double[2];
// get x coordinate in radians
p[0] = (lon - lonOrigin);
// convert x coordinate from radians to [0,1)
p[0] = p[0] / (2.0 * PI);
while (p[0] < 0) {
p[0]++;
}
while (p[0] > 1) {
p[0]--;
}
// get y coordinate in radians with 0 being equator
p[1] = (5.0/4.0) * Math.log(Math.tan(QUARTER_PI + (2.0/5.0) * lat));
// convert out of radians
p[1] = p[1] / TWO_PI;
// move 0 to be the top edge of the screen and not upside down
p[1] = 1 - (p[1] + 0.5);
if (p[1] < 0 || p[1] > 1) {
println("out of bounds entry, lon/lat " + lon + " " + lat + " - " + p[0] + "," + p[1]);
}
return p;
}
double[] millerDecode(double x, double y) {
double[] p = new double[2];
// convert x back to radians
p[0] = x * 2.0 * PI;
// adjust y so 0 is the equator and not inverted
y = 1 - (y + 0.5);
// convert y back to radians
y = y * 2.0 * Math.PI;
// convert using inverse miller transform
p[1] = (5.0 / 2.0) * ( Math.atan( Math.exp( y * (4.0 / 5.0) ) ) - QUARTER_PI );
return p;
}
/**
* Provide x,y as a mercator pair both in the set [0,1)
* Returned is a 2-value array with lon, lat.
* Longitude is [-PI, PI). Latitude is [-0.4725*PI,0.4725*PI)
* These values create a square in mercator space
*/
double[] mercatorDecode(double x, double y){
double[] lonlat = new double[2];
lonlat[0] = x * Math.PI * 2;
lonlat[1] = 2 * Math.atan(Math.exp(Math.PI * 2 * (y - 0.5) )) - Math.PI*0.5;
return lonlat;
}
/**
* Provide longitude, latitude as a pair in the set: Longitude is [-PI, PI). Latitude is [0.4725*PI,-0.4725*PI)
* Also provide a longitude to use as the left-edge, usually -PI
* Returned is a 2-value array with x, y both in the set [0,1).
* These values create a square in mercator space
*/
double[] mercatorEncode(double lon, double lat, double lonOrigin){
double[] p = new double[2];
p[0] = (lon - lonOrigin)/(2*Math.PI);
while(p[0]<0) p[0]++;
p[1] = 1 - (Math.log(Math.tan(PI/4 + lat/2)) * 0.31830988618 + 1)/2;
return p;
}
double map(double n, double l, double h, double a, double b){
return (b-a)*(n-l)/(h-l) + a;
}
double norm(double n, double l, double h){
return (n-l)/(h-l);
}
@psaidurga13
Copy link

psaidurga13 commented Dec 19, 2017

can you please tell me what is the use of incrementing p[0] until it becomes zero in mercator encoding.....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment