16

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?

nmtoken
  • 13,355
  • 5
  • 38
  • 87

4 Answers4

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

Glorfindel
  • 1,096
  • 2
  • 9
  • 14
scw
  • 16,391
  • 6
  • 64
  • 101
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
astrojuanlu
  • 131
  • 4
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.

Pablo
  • 9,827
  • 6
  • 43
  • 73
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)

Jane
  • 1,066
  • 7
  • 20