I've created a couple of functions using the math
package from python 3.5 to transform GPS coordinates to x/y web mercator (pseudo mercator) as explained here: http://wiki.openstreetmap.org/wiki/Mercator#Java
I took the Java version and ported it to Python:
import math
# derived from the Java version explained here: http://wiki.openstreetmap.org/wiki/Mercator
RADIUS = 6378137.0 # in meters on the equator
def lat2y(a):
return math.log(math.tan(math.pi / 4 + math.radians(a) / 2)) * RADIUS
def lon2x(a):
return math.radians(a) * RADIUS
Then I can use them to get for example the projection of Trafalgar Square (taking the GPS coordinates from the Google Maps URL):
ts_gm = [51.50809,-0.1285907]
print('latitude web mercator y: {} longitude web mercator x: {}'.format(lat2y(ts_gm[0]), lon2x(ts_gm[1])))
Which gives back:
latitude web mercator y: 6711665.883938471 longitude web mercator x: -14314.651244750603
However I would like to achieve this using pyproj
syntax, but I am lost in the details of the string I should pass to build a projection using Web Mercator EPSG:3857.
I suspect the formulas above have some sort of precision issue and I would like to rely on a library like pyproj
hoping to minimise projection errors.
Is there a minimum working example I could see to achieve the mapping in python using pyproj?
Best Answer
The "trick" is to use these shortcuts for Web Mercator (EPSG 3857) and WGS 84 longitude and latitude (EPSG 4326). The
init
key means "initialize this projection by reading the definition for 3857 from the 'epsg' file."