Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I got the geometry of a feature of the shapefile, and i want to save that geometry in the postgis (in WKB formats like it happens when we import shapefiles using shp2pgsql and psql commands). How do I convert then

To get Geometry i have used OSGeo OGR library eg:

feat = layer.GetFeature(0)
geometry = feat.GetGeometryRef()

and I have got

<osgeo.ogr.Geometry; proxy of <Swig Object of type 'OGRGeometryShadow *' at 0x0096A2D8> >

so how will convert it into the WKB geometry? I am using Python for this.

share|improve this question
up vote 8 down vote accepted

You are almost there. You just need to call the ExportToWkb function.

import ogr
# Get the driver
driver = ogr.GetDriverByName('ESRI Shapefile')
# Open a shapefile
shapefileName = "D:/temp/myshapefile.shp"
dataset = driver.Open(shapefileName, 0)

layer = dataset.GetLayer()
for index in xrange(layer.GetFeatureCount()):
    feature = layer.GetFeature(index)
    wkb = feature.GetGeometryRef().ExportToWkb()
share|improve this answer
Thanks geographika .. is there any resource available whare i can look for the different api's available. – Vicky Oct 19 '11 at 16:20
The Python bindings mirror the OGR C API – geographika Oct 19 '11 at 17:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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