[GIS] Creating QGIS text label in composer calculating values from attribute table

expressionfield-calculatorlabelingprint-composerqgis

I am working with QGIS 2:14, for now, I have a qgs project with a lot of layers and I want to create a label in the print-composer with the result of a calculated expression from one of this layers.

In one of my layers (recycling_points.shp ) I have 150 records with the recycling points in the city. Then, for every record I have a field (num_containers) with the number of containers of every recycling point, and I want to create a text label in the print-composer, based on a calculated expression, with the number of containers in my city, but I don't know how can do it correctly.

I've tried with the next expression:

layer_property('recycling_points', count"num_containers") 

but is invalid.

Any idea?

Best Answer

I think you can't sum the value of all feature in Qgis 2.14 with the field calculator. The formula 'layer_property' can only count the number of features within a layer, if you enter within a text label ('insert an expression'):

  • layer_property( 'recycling_points','feature_count')

If you have to use Qgis 2.14 you could create a single feature for the number of 'num_containers', e.g. if this number is 4 there should be 4 features at that recycling_point

If you can update to (at least) Qgis 2.18 it is much easier, you can use the new 'aggregate' function and simply enter:

  • aggregate( LAYERNAME, 'sum', FIELDNAME)
    • aggregate( 'recycling_points', 'sum', "num_containers")

Related Question