[Gdal-dev] help with OGR->python

Julien Demaria dem at acri-st.fr
Fri Feb 4 10:14:56 EST 2005


Dylan,

The trick is that in the OGR C and Python APIs, a Geometry can contains 
others Geometries (accessible with Geometry::GetGeometryCount() and 
Geometry::GetGeometryRef( i )), and that a Polygon has not directly points, 
but has one exterior ring and zero ore more interior rings.
The exterior ring is the first contained geometry, the interiors the others.

example of code doing this :

ds = ogr.Open( 'toto.shp' )
la = ds.GetLayer( 0 )
f = la.GetNextFeature()
polyGeom = f.GetGeometryRef() # get the first geometry (supposed a polygon)
exteriorRing = polyGeom.GetGeometryRef(0)
print exteriorRing.GetX(0), exteriorRing.GetY(0)
firstInteriorRing = polyGeom.GetGeometryRef(1)

The C++ API is different : you have to use polyGeom->getExteriorRing() 
instead of polyGeom.GetGeometryRef(0)


Regards,

Julien

At 03:06 04/02/2005, 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 Beaudette
>Soils and Biogeochemistry Graduate Group
>University of California at Davis
>530.754.7341
>_______________________________________________
>Gdal-dev mailing list
>Gdal-dev at xserve.flids.com
>http://xserve.flids.com/mailman/listinfo/gdal-dev




More information about the Gdal-dev mailing list