[gdal-dev] OGR Geometry methods
Frank Warmerdam
warmerdam at pobox.com
Thu Jul 14 01:19:25 EDT 2011
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,
--
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush | Geospatial Software Developer
More information about the gdal-dev
mailing list