Converting projected coordinates to lat/lon using Python

The simplest way to transform coordinates in Python is pyproj, i.e. the Python interface to PROJ.4 library. In fact:

from pyproj import Proj, transform

inProj = Proj(init='epsg:3857')
outProj = Proj(init='epsg:4326')
x1,y1 = -11705274.6374,4826473.6922
x2,y2 = transform(inProj,outProj,x1,y1)
print x2,y2

returns -105.150271116 39.7278572773


EDIT based on Marc's comment:

pyproj 2.4 gives a FutureWarning about deprecated Proj initialization with the init= syntax. The updated syntax is identical but without the init=. Like this:

inProj = Proj('epsg:3857')
outProj = Proj('epsg:4326')

By default the site you linked to uses the Spatial Reference System EPSG 3857 (WGS84 Web Mercator). I found this information here.

You can either specify another Spatial Reference System by entering the desired EPSG into the form under Spatial Reference or you can convert the returned coordinates with Python.

For instance you can use the GDAL Python bindings to convert this point from the projected coordinate system (EPSG 3857) to a geographic coordinate system (EPSG 4326).

import ogr, osr

pointX = -11705274.6374 
pointY = 4826473.6922

# Spatial Reference System
inputEPSG = 3857
outputEPSG = 4326

# create a geometry from coordinates
point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(pointX, pointY)

# create coordinate transformation
inSpatialRef = osr.SpatialReference()
inSpatialRef.ImportFromEPSG(inputEPSG)

outSpatialRef = osr.SpatialReference()
outSpatialRef.ImportFromEPSG(outputEPSG)

coordTransform = osr.CoordinateTransformation(inSpatialRef, outSpatialRef)

# transform point
point.Transform(coordTransform)

# print point in EPSG 4326
print point.GetX(), point.GetY()

This returns for your point the coordinates of -105.150271116 39.7278572773.


afalciano has the right answer but wanted to include a variant usage of pyproj.

It does require you know the proj4 string and is a tiny bit faster.

import pyproj
p = pyproj.Proj("+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378137 +b=6378137 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs")
lon, lat = p(x, y, inverse=True)
print lat, lon