<div dir="auto">This is such an instructive reply it should be added to stackexchange straight away! :)</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, May 20, 2019, 07:37 Nyall Dawson <<a href="mailto:nyall.dawson@gmail.com">nyall.dawson@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Mon, 20 May 2019 at 01:21, Anita Graser <<a href="mailto:anitagraser@gmx.at" target="_blank" rel="noreferrer">anitagraser@gmx.at</a>> wrote:<br>
><br>
> Hi,<br>
><br>
> I'd like to add an example of an aggregate expression to my recent PyQGIS 101 tutorial on expressions [0]. I got expressions for individual features working but cannot figure out how to set the context for aggregate expressions correctly.<br>
><br>
> As far as I can tell, the PyQGIS Cookbook doesn't contain any information on this either [1].<br>
<br>
You aren't correctly constructing your expression context:<br>
<br>
context = QgsExpressionContext()<br>
scope = QgsExpressionContextScope()<br>
context.appendScope(scope)<br>
<br>
This is effectively doing nothing -- you're creating a context, but<br>
not putting anything in that context.<br>
<br>
What you need to do is populate the context with relevant scopes,<br>
depending on what's available at the time you're evaluating the<br>
expression. You get "prebuilt" scopes by using the methods from<br>
QgsExpressionContextUtils. E.g.<br>
<br>
context = QgsExpressionContext()<br>
context.append(QgsExpressionContextUtils.globalScope())<br>
context.append(QgsExpressionContextUtils.projectScope(QgsProject.instance()))<br>
<br>
That's effectively the **minimum** you'd ever populate a context using<br>
-- every expression evaluated anywhere should have access to global<br>
and project information.<br>
<br>
But usually, you'd also want to be adding a map layer scope:<br>
<br>
context.append(QgsExpressionContextUtils.layerScope( my_layer ))<br>
<br>
This is what's missing from your examples, and it's required for<br>
calculation of aggregate based expressions (and other expressions,<br>
e.g. those which use field references from the layer).<br>
<br>
This global/project/layer scope is used so often there's even a shortcut for it:<br>
<br>
context=QgsExpressionContext()<br>
context.appendScopes(<br>
QgsExpressionContextUtils.globalProjectLayerScopes( my_layer ) )<br>
<br>
( this expands out to the same as adding the global, project, layer<br>
scopes individually by hand)<br>
<br>
***IMPORTANT***<br>
<br>
Keep in mind the order of scopes when you're adding them. You always<br>
want to go from "most generic" to "most specific". I.e., you want<br>
global variables to be overridden by project variables, to be<br>
overridden by layer variables (and not the opposite).<br>
<br>
A documentation update covering this would be most welcome ;)<br>
<br>
Nyall<br>
<br>
<br>
<br>
<br>
<br>
><br>
> I'd appreciate any examples!<br>
><br>
> Regards,<br>
> Anita<br>
><br>
> [0] <a href="https://anitagraser.com/pyqgis-101-introduction-to-qgis-python-programming-for-non-programmers/pyqgis-101-using-expressions-to-compute-new-field-values/" rel="noreferrer noreferrer" target="_blank">https://anitagraser.com/pyqgis-101-introduction-to-qgis-python-programming-for-non-programmers/pyqgis-101-using-expressions-to-compute-new-field-values/</a><br>
> [1] <a href="https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/expressions.html#evaluating-expressions" rel="noreferrer noreferrer" target="_blank">https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/expressions.html#evaluating-expressions</a><br>
> _______________________________________________<br>
> QGIS-Developer mailing list<br>
> <a href="mailto:QGIS-Developer@lists.osgeo.org" target="_blank" rel="noreferrer">QGIS-Developer@lists.osgeo.org</a><br>
> List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
> Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
_______________________________________________<br>
QGIS-Developer mailing list<br>
<a href="mailto:QGIS-Developer@lists.osgeo.org" target="_blank" rel="noreferrer">QGIS-Developer@lists.osgeo.org</a><br>
List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a></blockquote></div>