Transforming geographical and projected coordinates in Python

pyproj is a Python interface to the PROJ.4 (http://trac.osgeo.org/proj/) functions. It allows you to transform coordinates between coordinate systems.

Install pyproj using pip

Assuming you're using pip to install Python packages:

$ pip install pyproj

Example

My apartment is located around 12.5996, 55.6644 in long/lat. Where is it located in epsg:25832?

p1 = Proj(init='epsg:25832')
x1, y1 = p1(12.5996, 55.6644) # long/lat
# x1,y1 is 726386.5282272529, 6174604.596763284

Another way to initialize p1 is to use a PROJ4 string (http://spatialreference.org/ref/epsg/25832/proj4/):

p1b = Proj('+proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs')
p1b(12.601, 55.6617) == p1(12.601, 55.6617)
# True

Transform coordinate back to long/lat (epsg:4326):

g1 = p1(x1,y1,inverse=True)
# g1 is (12.600999999971785, 55.6616999997963)

As can be seen, these are not the original coordinates, but close enough.

Transformations are done with the transform function:

p2 = Proj(init='epsg:4326')
g2 = transform(p1,p2,x1,y1)
# g2 is (12.600999999971785, 55.6616999997963)
g1 == g2
# True

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.