[Qgis-developer] how to create a new geometry from an existing one with python

Martin Dobias wonder.sk at gmail.com
Sun May 13 01:24:24 PDT 2012

Hi Ricardo

On Sun, May 13, 2012 at 1:39 AM, Ricardo Filipe Soares Garcia da
<ricardo.garcia.silva at gmail.com> wrote:
> [...]
> # python code
> line_geometry =
> qgis.utils.iface.mapCanvas().currentLayer().selectedFeatures()[-1].geometry()
> # approach 1
> new_geometry = QgsGeometry(line_geometry)

This approach is correct (and other seems to be fine, too). The
problem is elsewhere: you are being bitten by this bug:

Basically the problem is that the feature containing the line_geometry
gets deleted when the first line is executed. So your line_geometry
contains an invalid pointer leading to these strange errors. To
overcome this, store the list of features and then access it without


More information about the Qgis-developer mailing list