[Qgis-developer] geometry operation (python) crashes QGIS

Martin Dobias wonder.sk at gmail.com
Mon Oct 29 05:44:43 PDT 2012


Hi Dominik

On Mon, Oct 29, 2012 at 12:26 PM, Dominik Fröhlich
<dominik.froehlich at venus.uni-freiburg.de> wrote:
> Der QGIS developers,
>
> I'm trying to write a python plugin to reate input data for one of our
> models. It basicly converts three shape files with special fields to a text
> file.
>
> I have a buildings vector (polygon) layer containing three polygons (which I
> created for testing). This layer is called "build" in the code. To extract
> its geometries, I wrote a little function:
>
> def extract_poly_geometry(self, layer):
> feature = QgsFeature()
> geom_list = []
> while layer.dataProvider().nextFeature(feature):
> geom_list.append(feature.geometry())
> return geom_list

The problem in your code is that you are storing QgsGeometry instances
which are owned by the QgsFeature instance. So when another feature is
stored into your QgsFeature instance (in nextFeature() call) or when
feature instance goes out of scope, the geometry instance becomes
invalid - you still have a python object, but it contains a dangling
pointer, hence the crashes. The solution is to store a copy of the
geometry:
geom_list.append( QgsGeometry( feature.geometry() ) )

Regards
Martin


More information about the Qgis-developer mailing list