TransWikia.com

Convert GPS coordinates to Web Mercator EPSG:3857 using python/pyproj

Geographic Information Systems Asked by TPPZ on June 6, 2021

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?

2 Answers

from pyproj import Proj, transform
print(transform(Proj(init='epsg:4326'), Proj(init='epsg:3857'), -0.1285907, 51.50809))  # longitude first, latitude second.
# output (meters east of 0, meters north of 0): (-14314.651244750548, 6711665.883938471)

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."

Correct answer by sgillies on June 6, 2021

With the latest version of pyproj, the recommended way is

from pyproj import Transformer


TRAN_4326_TO_3857 = Transformer.from_crs("EPSG:4326", "EPSG:3857")

def mytransform(lon, lat):
  return TRAN_4326_TO_3857.transform(lon, lat)

This can be orders of magnitude faster, especially if you need to convert many coordinates

See: https://pyproj4.github.io/pyproj/dev/advanced_examples.html#advanced-examples

Answered by Jacopofar on June 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP