1

I'm writing a script to import shp to postgis, using python, osgeo and psycopg2 ( see my previous question: Import shp to Postgis using Python and ogr )

I'd know if there is a way to get by osgeo library the SRID (for example 4326) of the shp.

So I can confirm the SRID or transform it using script directly.

  • franco, are you aware that ogr includes scripts (ogr2ogr in particular) that do this already, and that you don't need python? – BenjaminGolder Apr 11 '14 at 18:31
3

Go to the Python GDAL/OGR Cookbook 1.0 documentation and you'll have the answers to all your questions:

    from osgeo import ogr
    driver = ogr.GetDriverByName('ESRI Shapefile')
    shape = driver.Open('my.shp')
    layer= shape.GetLayer()
    # the crs
    crs = layer.GetSpatialRef()

and you can also create a projection file if the shapefile does not have one or reproject the shapefile with osgeo.osr

2

ogrinfo layer.shp layer -so will give you general information about the shapefile.

0

for display crs finally you can use crs.proj4

from osgeo import ogr
driver = ogr.GetDriverByName('ESRI Shapefile')
shape = driver.Open('my.shp')
layer= shape.GetLayer()
# the crs

crs = layer.GetSpatialRef()

crs.proj4()

0

If you don't want to use Python, gdalsrsinfo (which you probalby have installed if you have ogrinfo) will also work to display the SRS from a shapefile:

gdalsrsinfo shapefile.shp

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.