[gdal-dev] Unable to use geom.ExportToWkt() in Python ?
Frank Warmerdam
warmerdam at pobox.com
Tue Nov 23 22:04:26 EST 2010
Gregor at HostGIS wrote:
> That's strange, the docs show ExportAsWkt as being a method of a
> Geometry, as does the source code for the Python wrapper. Well, you
> would know best...
>
> If I do spatialref.ExportAsWkt() it gives the WKT of the SRS, right?
> What if I want a feature's geometry? Would I need to generate it myself
> from the geometry type and iterating over the vertices?
Gregor,
Garr, disregard me. I had SRS WKT on the brain and forgot all about
geometry WKT.
> > driver = ogr.GetDriverByName('ESRI Shapefile')
> > datasource = driver.Open(shapefile,0)
> > layer = datasource.GetLayer()
> > spatialRef = layer.GetSpatialRef()
> > firstgeom = layer.GetFeature(0).GetGeometryRef()
> > wkt = firstgeom.ExportToWkt()
GetFeature() makes a copy of the feature, and right after the GetGeometryRef()
call the feature is destroyed because there is no obvious remaining reference
to it. This means the geometry pointer (a reference to the internal
geometry of the feature) is now pointing to a deleted object and the
ExportToWkt() crashes.
The trick is to keep around the feature till you are done with the geometry.
feat = layer.GetFeature(0)
firstgeom = feat.GetGeometryRef()
wkt = firstgeom.ExportToWkt()
There are efforts in recent versions of the bindings to avoid this sort
of problem with layers and datasources but even in trunk I suspect grabbing
a geometry handle from within a feature will not keep the feature alive.
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 Programmer for Rent
More information about the gdal-dev
mailing list