Dear Nyall,

On Mon, May 20, 2019 at 9:01 AM Anita Graser <anitagra...@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
_______________________________________________
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

Reply via email to