[Gdal-dev] help with OGR->python

Sean Gillies sgillies at frii.com
Fri Feb 4 09:42:31 EST 2005


On Feb 3, 2005, at 7:06 PM, Dylan Beaudette wrote:

> Greetings:
>
> After struggling with trying to get vector data into GMT with GRASS, I  
> started
> playing with the OGR bindings in python.
>
> however, after 2 hours I have not been able to do something as simple  
> as
> extract the geometry of polygons in a shapefile...
>
> I am trying to construct a small python program to convert a shapefile  
> into a
> list of coordinates, with a few GMT-specific markers at the start of  
> each
> polygon.
>
> Does anyone know of an existing tool for this job? or perhaps some  
> example
> python code for reading a simply polygon shapefile?
>
> Thanks in advance!
>

Dylan,

It seems to me that the structure of an OGRGeometry is not exposed to  
the ogr.py module.  At least, when I

 >>> dir(ogr.Geometry)

there is nothing that appears to provide access to the geometry's lines  
and points.  I could be wrong and hopefully Frank will clear this up.

You are also welcome to use my code at

     
http://cvs.sourceforge.net/viewcvs.py/zmapserver/PCL/cartography/ 
spatial.py?
rev=1.2&view=markup

which provides Python-friendly geometries that can be exchanged with  
ogr.py by serializing to and from WKB format.  like so:

     ogr_feature = layer.GetNextFeature()
     ogr_geom = ogr_feature.GetGeometryRef()
     pcl_polygon = spatial.Polygon(wkb=ogr_geom.ExportToWkb())
     for ring in pcl_polygon:
         # process ring and points
         ...

Contact me offline about the spatial module so we don't go any further  
OT on this list.

cheers,
Sean

--
Sean Gillies
sgillies at frii dot com
http://users.frii.com/sgillies




More information about the Gdal-dev mailing list