[QGIS-Developer] Expressions with aggregate in PyQGIS

Nyall Dawson nyall.dawson at gmail.com
Mon May 20 15:15:23 PDT 2019


On Tue, 21 May 2019 at 05:53, Anita Graser <anitagraser at gmx.at> wrote:
>
> Dear Nyall,
>
> for f in vl.getFeatures():
>     scope[-1].setFeature(f)

This works, but isn't very nice. Better to use

    context.setFeature(f)

(behind the scenes it's doing the same, but is more descriptive and
has some extra checks, e.g. starting with an empty context:
    context = QgsExpressionContext()
    context.setFeature(f)
will automatically add an initial scope in which to set the feature)

> scope = QgsExpressionContextUtils.globalProjectLayerScopes(vl)

Being pedantic, this should be "scopes = ...", since the call returns
a list of scopes.

Nyall


More information about the QGIS-Developer mailing list