6

I'm looking to study the math involved to see how a given latitude and longitude in decimal degrees can be converted to epsg 3857. Can anybody point to a good reference or a possible python/c/c++/java library source code for checking the math?

3
from pyproj import Proj, transform

P3857 = Proj(init='epsg:3857')
P4326 = Proj(init='epsg:4326')

x,y = transform(P4326, P3857, lon, lat)
2

This code snippet is C# and using an array called vertex to hold [x, y]

double smRadius = 6378136.98;
double smRange = smRadius * Math.PI * 2.0;
double smLonToX = smRange / 360.0;
double smRadiansOverDegrees = Math.PI / 180.0;

...

// compute x-map-unit
vertex[0] *= smLonToX;

double y = vertex[1];

// compute y-map-unit
if (y > 86.0)
{
    vertex[1] = smRange;
}
else if (y < -86.0)
{
    vertex[1] = -smRange;
}
else
{
    y *= smRadiansOverDegrees;
    y = Math.Log(Math.Tan(y) + (1.0 / Math.Cos(y)), Math.E);
    vertex[1] = y * smRadius; 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.