2

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?

4

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")

  • PERFECT! aggregate is my function. I've been updated my QGIS version to the 2.18 and I have achieved my purpose. Thanks! – Litus78 May 11 '17 at 8:56
  • Glad I could help. If you want to help others you can accept the answer, so others will see more easily how you solved the problem ;-). – swad May 11 '17 at 10:34
3

The number of containers for every recycling point is already stored so, if I understand your question, you only want to sum them.

You may use this expression:

sum("num_containers")

and it will return the sum of the values stored in the "num_containers" field.

Furthermore, if you want to group the sums by another field (e.g. a field that separates different neighborhoods), you may add the "group_by" parameter:

sum("num_containers", group_by:="neighborhood_field")

EDIT

If you are working with many layers, you may use something like this:

CASE
WHEN
layer_property(@layer_name, 'name') = 'recycling_points'
THEN
sum("num_containers")
END

for running the sum only when the layer's name is 'recycling_points'.

  • Thanks for the answer, but I need that the expression select previosly from what layer select the sum("num_containers") because I want to insert this expression (in a text label) in the map composer and my qgs project has a lot of layers. – Litus78 May 10 '17 at 9:33
  • @Litus78 I can't find the part of your question where you specified that you are working with a lot of layers. Please, see the edit on my answer: if it still doesn't fit your needs, consider to edit your original question by adding more information on which is the situation you are starting from and to where you are pointing to. – mgri May 10 '17 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.