I assume this is a basic question but I can't seem to find or recognize the solution.

This site returns

X: -11705274.6374
Y: 4826473.6922

when you searching with the first key value of 000090 as an example. I guess this is a spatial reference and I kinda get what that is.

I am looking for instructions or examples of how to convert this to Latitude and longitude 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

  • 4
    Yep. Pyproj all the way. – sgillies Dec 2 '13 at 15:14
  • It works for me – lenhhoxung Aug 24 '18 at 12:50

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()

outSpatialRef = osr.SpatialReference()

coordTransform = osr.CoordinateTransformation(inSpatialRef, outSpatialRef)

# transform point

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

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


The output is not a spatial/coordinate reference system, it's a pair of coordinates. You need to know what the spatial reference is to reproject the coordinates.

However, that's not required in this case. Just pass an appropriate output spatial reference to the service and it will return the coordinates in Lon/Lat.

Here is the page with output coordinates in Lon/Lat format using the WGS-84 geographic spatial reference system (EPSG 4326).


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
  • 2
    You don't need the proj4 string, substitute the second line for p = pyproj.Proj(init='epsg:3857') and the result is the same. – alphabetasoup Sep 15 '16 at 0:08
  • 1
    The result is the same but last I checked this was a tiny bit faster. – Marcel Wilson Sep 17 '16 at 17:03

Tried the code suggested by Marcel Wilson and it is faster:

from pyproj import Proj, transform
import time
import pyproj

# Test 1 - 0.0006158 s
inProj = Proj(init='epsg:3857')
outProj = Proj(init='epsg:4326')
x1,y1 = -11705274.6374,4826473.6922
x2,y2 = transform(inProj,outProj,x1,y1)
print('%.7f' % (end-start))

# Test 2 - 0.0000517 s --- factor 11,9
x,y = -11705274.6374,4826473.6922
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)
print('%.7f' % (end-start))

39.72785727727918 -105.15027111593008
39.72785727727918 -105.15027111593008

I found this post when looking for ways of doing this within QGIS. As described here, the method used looks like so:

def convertProjection(self,x,y,from_crs,to_crs):
    crsSrc = QgsCoordinateReferenceSystem(from_crs)
    crsDest = QgsCoordinateReferenceSystem(to_crs)
    xform = QgsCoordinateTransform(crsSrc, crsDest)
    pt = xform.transform(QgsPoint(x,y))
    return pt.x, pt.y

# Remove the "EPSG:" part
from_crs = 3857
to_crs = 4326
x = -11705274.6374    
y = 4826473.6922
lon, lat = self.convertProjection(x,y,from_crs, to_crs)
  • 2
    Note, there's a breaking API change in QGIS 3, so if using v3.x you'll need to use xform = QgsCoordinateTransform(crsSrc, crsDest, QgsProject.instance()) – Jonny Jan 17 at 15:38

Please note that the transform function of pyproj accepts also arrays, which is quite useful when it comes to dataframes

import pandas as pd
from pyproj import Proj, transform

df = pd.DataFrame({'x': [-11705274.6374]*100, 
                   'y': [4826473.6922]*100})
inProj, outProj = Proj(init='epsg:3857'), Proj(init='epsg:4326')
df['x2'], df['y2'] = transform(inProj, outProj, df['x'].tolist(), df['y'].tolist())
import cv2
import numpy as np
def onMouse(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
       # draw circle here (etc...)
       print('x = %f, y = %f'%((x*2/100),(y*2/100)))
a=float(input("enter length of original dimension in cm"))
b=float(input("enter width of original dimension in cm"))
print("origional image coordinates")
cv2.setMouseCallback('image', onMouse)

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

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.