[QGIS-Developer] Do QgsGeometry constGet() e constParts() return non-modifiable or modifiable reference in PyQGIS?

Nyall Dawson nyall.dawson at gmail.com
Thu Feb 11 14:49:05 PST 2021


On Fri, 12 Feb 2021 at 05:13, Andrea Giudiceandrea
<andreaerdna at libero.it> wrote:
>
> Hi all,
> is it expected that QgsGeometry constGet() and constParts() in PyQGIS return
> modifiable objects?

Yes -- python doesn't have any concept of "const", so ALL objects are
modifiable as a result.

> In the Python Console:
>
> >>> geom=QgsGeometry.fromWkt( 'Point(16 41)' )
> >>> geom_const=geom.constGet()
> >>> geom_const.transform(QgsCoordinateTransform(QgsCoordinateReferenceSystem("EPSG:4326"),
> >>> QgsCoordinateReferenceSystem("EPSG:25833"), QgsProject.instance()))
>
> >>> geom_const
> <QgsPoint: Point (584102.13444570498540998 4539238.58925933390855789)>
> >>> geom
> <QgsGeometry: Point (584102.13444570498540998 4539238.58925933390855789)>
>
>
> >>> geom=QgsGeometry.fromWkt( 'MultiPoint((16 41))' )
> >>> for part in geom.constParts():
>
> part.transform(QgsCoordinateTransform(QgsCoordinateReferenceSystem("EPSG:4326"),
> QgsCoordinateReferenceSystem("EPSG:25833"), QgsProject.instance()))
>
> >>> geom
> <QgsGeometry: MultiPoint ((584102.13444570498540998
> 4539238.58925933390855789))>

Those results are as expected, but somewhat of a trap! I'd usually
only recommend that developers who are well aware of the dangers here
use the constGet method, and only do so in performance critical code
where they know they aren't changing the object.

Nyall


>
>
> Regards.
>
> Andrea Giudiceandrea
>
>
>
> --
> Sent from: http://osgeo-org.1560.x6.nabble.com/QGIS-Developer-f4099106.html
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer


More information about the QGIS-Developer mailing list