15

Some shapefiles have a .prj file associated with it, and the .prj file contains the projection info of the shapefile in the format of WKT. Sometimes I need to convert WKT to proj4 string, and sometimes I need to convert it back.

Is there any ready-made library to do this?

4 Answers 4

14

The OGR Spatial Reference part of GDAL should do the trick. capooti provided an excellent answer to another question which demonstrates how to peform the translation from a shapefile to WKT. You may also want to check out the class reference. The reverse is simply:

from osgeo import osr

srs = osr.SpatialReference()
wkt_text = 'GEOGCS["GCS_WGS_1984",DATUM["WGS_1984",' \
           'SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],'\
           'UNIT["Degree",0.017453292519943295]]'
# Imports WKT to Spatial Reference Object
srs.ImportFromWkt(wkt_text)
srs.MorphToESRI() # converts the WKT to an ESRI-compatible format
print "ESRI compatible WKT for use as .prj:" % srs.ExportToWkt()
2
  • Anyone have a solution that doesn't require osgeo, which doesn't seem to work for Python 3?
    – dancow
    Commented Feb 22, 2016 at 2:51
  • OP asked about Proj4. Did you mean ExportToProj4() in the last line instead?
    – user99665
    Commented Jun 23, 2017 at 8:08
3

You can also use PyCRS:

import pycrs

print(pycrs.parser.from_esri_wkt(wkt_text).to_proj4())
# +proj=longlat +ellps=WGS84 +a=6378137.0 +f=298.257223563 +pm=0.0  +no_defs
1

I don't know any library, but you can use this site to get the translations: http://spatialreference.org/

EDIT: I found a python script that works with ogr python bindings to do that. Here it is.

3
  • thanks, but i need to do that programmatically. Commented Apr 14, 2011 at 16:11
  • 1
    After some googling: spatialreference.org is powered by GDAL as well & uses the same code path (more or less), it seems.
    – Dan S.
    Commented Apr 14, 2011 at 17:11
  • That's interesting
    – Pablo
    Commented Apr 14, 2011 at 18:33
0

I need to pragrammatically transform to custom projection based on proj4text string, so used

projection = '+proj=lcc +lat_1=53 +lat_2=70 +lat_0=0 +lon_0=136 +x_0=0 +y_0=0 +ellps=intl +units=m +no_defs'

source = osr.SpatialReference() source.ImportFromEPSG(4326) target = osr.SpatialReference() target.ImportFromProj4(projection) transform = osr.CoordinateTransformation(source, target)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.