[QGIS-Developer] Expressions with aggregate in PyQGIS
Anita Graser
anitagraser at gmx.at
Mon May 20 12:53:17 PDT 2019
Dear Nyall,
On Mon, May 20, 2019 at 9:01 AM Anita Graser <anitagraser at gmx.at> wrote:
> Thank you Nyall, that's really helpful! The expression system is awesome
> and powerful but I don't think anyone could guess how to use it correctly
> in PyQGIS.
> I'll write it up into a tutorial and the documentation team is always
> welcome to reuse the content in the official documentation.
>
The following code works for me. However, I'm uncertain if the
scope.setFeature approach below is how it's meant to be used. Is there
something I should change?
context = QgsExpressionContext()
scope = QgsExpressionContextUtils.globalProjectLayerScopes(vl)
context.appendScopes(scope)
expression1 = QgsExpression('Revenue/Employees')
expression2 = QgsExpression('sum(Revenue)')
vl.startEditing()
for f in vl.getFeatures():
scope[-1].setFeature(f)
f['Rev. per employee'] = expression1.evaluate(context)
f['Sum'] = expression2.evaluate(context)
vl.updateFeature(f)
vl.commitChanges()
Regards,
Anita
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20190520/1fd91f84/attachment.html>
More information about the QGIS-Developer
mailing list