[gdal-dev] OGR Geometry methods

Marius Jigmond mariusjigmond at hotmail.com
Fri Jul 15 21:19:26 EDT 2011


Thanks Frank, excellent tip to be aware of. It's working great now.

-marius

On Wed, 2011-07-13 at 22:19 -0700, Frank Warmerdam wrote:

> On Wed, Jul 13, 2011 at 4:23 PM, Marius Jigmond
> <mariusjigmond at hotmail.com> wrote:
> > aqfeat = aqLayer.GetNextFeature()
> > aqgeom = aqfeat.GetGeometryRef()
> > geomList = [aqgeom]
> >
> > while aqfeat is not None: #loop thru features in case of islands/multiple
> > polygons
> >   aqfeat = aqLayer.GetNextFeature()
> >   if aqfeat is not None:
> >     geomList.append(aqfeat.GetGeometryRef())
> 
> Marius,
> 
> When you read a new
> feature aqfeat is replaced, and the old feature is cleaned up
> by the garbage collector.  At that point it's associate geometry
> is also destroyed even though it is referenced from Python.  This
> is a weakness caused by the way object lifetimes are handled
> in OGR that doesn't map well to Python.  So you should be cloning
> the geometry when you add it to your list.
> 
> eg.
>   geomList = [aqgeom.Clone()]
> ...
>   geomList.append( aqfeat.GetGeometryRef().Clone() )
> 
> The rule of thumb is that ogr.Datastore and ogr.Feature are well
> handled according to Python reference counting semantics.
> But ogr.Layer and ogr.Geometry objects are destroyed when
> their owning ogr.Datastore or ogr.Feature is destroyed.
> Freestanding ogr.Geometry objects are ok.  There is no such
> thing as a freestanding ogr.Layer.
> 
> This is a common gotcha.
> 
> Best regards,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20110715/2d06337e/attachment.html


More information about the gdal-dev mailing list