[Qgis-developer] Expression context and custom functions

Hugo Mercier hugo.mercier at oslandia.com
Fri Sep 11 06:53:58 PDT 2015


Hi NYall,

On 05/09/2015 14:09, Nyall Dawson wrote:
> On 4 September 2015 at 01:12, Hugo Mercier <hugo.mercier at oslandia.com> wrote:
>> Hi,
>>
>> I am using custom expressions functions for a plugin.
>> Something like this :
>>
>> class MyFunction( QgsExpression.Function ):
>>     def func( self, values, feature, parent ):
>>         feature.geometry()
>>         ...
>>
>> The problem is that "feature" is now a QgsExpressionContext(), not a
>> QgsFeature anymore.
>> I cannot find how to access the feature from the context. Did I miss
>> something ?
> 
> The old API is fixed in 9133538 - sorry about that. I've renamed the
> new method to funcV2 in the Python bindings.

Great, thanks.

> 
>> But even with the decorator, the problem is still the same: the
>> 'feature' parameter of the custom function is not a QgsFeature anymore
>> as it was on previous versions, but a QgsExpressionContext. And it seems
>> there is no way to access the feature through the expression context.
> 
>> Looking at the source, the feature is stored in a variable called
>> "_feature_", but trying to access this from the function returns None.
> 
> Should also be fixed with 9133538, but I'd appreciate confirmation.

Not tested yet. I will do it.

Another question: I am trying to access the current atlas geometry from
a plugin, but fail to do so.
>From the python console, with a atlas in preview mode :
QgsExpression.specialColumn("$atlasgeometry") -> None
QgsExpression("$atlasgeometry").evaluate() -> None

However, if I set a layer's label to "$atlasfeatureid", I get the right
label ...

Is it intended ? I guess that is because the "global" expression
evaluation has no atlas context ?



More information about the Qgis-developer mailing list