13

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?

11

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()
  • Anyone have a solution that doesn't require osgeo, which doesn't seem to work for Python 3? – Dan Nguyen Feb 22 '16 at 2:51
  • OP asked about Proj4. Did you mean ExportToProj4() in the last line instead? – astrojuanlu Jun 23 '17 at 8:08
2

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.

  • thanks, but i need to do that programmatically. – Cui Pengfei 崔鹏飞 Apr 14 '11 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. Apr 14 '11 at 17:11
  • That's interesting – Pablo Apr 14 '11 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, privacy policy and cookie policy

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