Saturday, 2 January 2016

Converting projected coordinates to lat/lon using Python?


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


This site returns


Point:
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.



Answer



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

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...