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

DelazJ delazj at gmail.com
Thu Feb 11 18:54:38 PST 2021


Hi,

Le jeu. 11 févr. 2021 à 23:49, Nyall Dawson <nyall.dawson at gmail.com> a
écrit :

> 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.
>
> So should we not pythonify the pyqgis docs (
https://qgis.org/pyqgis/master/core/QgsGeometry.html#qgis.core.QgsGeometry.constParts)
and avoid misleading beginners (like me in the docs PR previously
mentioned)?

Regards,
Harrissou

> > 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
> _______________________________________________
> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20210212/5c01f4cc/attachment.html>


More information about the QGIS-Developer mailing list