4

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.

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

5 Answers 5

4

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

3

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

1

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

1

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
0

You could user ogrinfo

ogrinfo -so -al shapefile.shp

-so : Summary Only: suppress listing of individual features and show only summary information like projection, schema, feature count and extents.

-al : List all features of all layers (used instead of having to give layer names as arguments).

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.