[QGIS-Developer] Geometry operations in Python
Nyall Dawson
nyall.dawson at gmail.com
Fri Jan 26 15:43:06 PST 2018
On 25 January 2018 at 17:30, Daan Goedkoop <dgoedkoop at gmx.net> wrote:
> Thank you for the answers!
>
> Ok, so I don't need to manipulate the QgsMultiLineString directly. Yet
> I cannot help to wonder a little bit about this (using a .shp file):
>
>>>> qgis.utils.iface.activeLayer().selectedFeatures()[0].geometry().asGeometryCollection()[0].asWkt()
> 'LineString (4.83090675976269512 52.44278398333691626,
> 4.89922802216601205 52.37283112413303598)'
>>>> qgis.utils.iface.activeLayer().selectedFeatures()[0].geometry().asWkt()
> 'MultiLineString ((4.83090675976269512 52.44278398333691626,
> 4.89922802216601205 52.37283112413303598))'
>>>> qgis.utils.iface.activeLayer().selectedFeatures()[0].geometry().asGeometryCollection()[0].asWkt()
> 'LineString (4.83090675976269512 52.44278398333691626,
> 4.89922802216601205 52.37283112413303598)'
>>>> type(qgis.utils.iface.activeLayer().selectedFeatures()[0].geometry().constGet())
> <class 'qgis._core.QgsMultiLineString'>
>>>> qgis.utils.iface.activeLayer().selectedFeatures()[0].geometry().constGet().wkbType()
> 5
>>>> qgis.utils.iface.activeLayer().selectedFeatures()[0].geometry().constGet().numGeometries()
> 0
>>>> qgis.utils.iface.activeLayer().selectedFeatures()[0].geometry().constGet().asWkt()
> <crash>
>
> Is this expected behaviour?
I suspect what's happening here is that the feature is being garbage
collected early, deleting the geometry and causing the unpredictable
behavior - try this:
f = qgis.utils.iface.activeLayer().selectedFeatures()[0]
f.geometry().constGet()...... etc
Nyall
More information about the QGIS-Developer
mailing list