Hi,

Finally, with the support from Matthias, I got a working example of my custom Python expression:

@qgsfunction(args='auto', group='Custom', usesgeometry=False)
def get_ews_anlage_nr(gemeinde, feature, parent):
AnlageLayer = QgsMapLayerRegistry.instance().mapLayersByName('Anlage')[0]
    aggParams = QgsAggregateCalculator.AggregateParameters()
    aggParams.filter = 'substr("anlage_nr",1,2) = \''+gemeinde+'\''
maxVal = AnlageLayer.aggregate(QgsAggregateCalculator.Max,'to_int(substr("anlage_nr",4,4))',aggParams)[0]
    return maxVal

When I define the QgsAggregateCalculator.AggregateParameters() upfront separately it works fine. First I was trying to set the parameter directly within AnlageLayer.aggregate() but failed to find the direct syntax of the QgsAggregateCalculator.AggregateParameters() parameters. There are hardly any examples out there. But hey - finally it works!

Have a nice weekend!
Andreas

On 2017-05-05 15:55, Neumann, Andreas wrote:

Hi,

I have a basic example using aggregates:

----------------------

@qgsfunction(args='auto', group='Custom', usesgeometry=False)
def get_ews_anlage_nr(gemeinde, feature, parent):
AnlageLayer = QgsMapLayerRegistry.instance().mapLayersByName('Anlage')[0]
maxVal = AnlageLayer.aggregate(4,'to_int(substr("anlage_nr",4,4))')[0]
return maxVal

-----------------------

But I still have two questions:

1. I still struggle to define a filter. I can't find the correct syntax how to define that filter string.

2. the first parameter "4" stands for "Max" aggregate (according to http://qgis-python.kartoza.com/docs/core/other/QgsAggregateCalculator.html#class-qgsaggregatecalculator ). Could this code be written more "readable" by specifying "Max" instead of 4?

Thanks,

Andreas

On 2017-05-05 14:31, Neumann, Andreas wrote:

Hi,

Does someone have an example how to use QgsVectorLayer.aggregate() from Python?

How would the syntax for the parameters in the QgsAggregateCalculator.calculate() method look like in Python?

Thanks,

Andreas

_______________________________________________
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

_______________________________________________
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
_______________________________________________
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