11

My data: 1 polygon layer + 1 point layer
My target: Calculate the number of points and the sum of a field within each polygon using the aggregate function in the QGIS field calculator.

I know I can simply use the QGIS functions "Join attributes by location (summary)" or "Count points in polygon" but I'm interested in the aggregate function in the field calculator since I don ́t understand exactly its meaning.

Using

aggregate('hh', 'sum', "sum_pop")

'hh' - the name of the point layer
'sum_pop' - the name of the attribute field from the hh layer (sum of the population of a point)

I get the sum of the whole field in the attribute table, so each feature has the same value.

aggregate_result_nok

How can I change the code for the aggregate function to calculate the sum of a value based on the number of points in each polygon feature and the number of features within a polygon so I get different values for each polygon?

example1

ecample2

evaluation error

Taras
35.7k5 gold badges77 silver badges151 bronze badges
asked Jul 29, 2019 at 16:24
2
  • 1
    You need to add a group_by parameter. Does the point layer have a polygon_ID field that tells you which polygon each point belongs to? If so, use "polygon_ID" as the group_by parameter. Commented Jul 29, 2019 at 17:54
  • @csk The point layer does not have a field with the polygon affiliation. But using the function Join attributes by location this field can be generated. Commented Jul 30, 2019 at 9:03

2 Answers 2

12

This expression will count the number of populated places for each country:

aggregate(layer:='ne_110m_populated_places',
 aggregate:='count',
 expression:=name,
 filter:=contains(geometry(@parent), $geometry)
 )

you can use the same expression to get the sum, just change the aggregate to 'sum' and the expression to the field name to summarize.

Taras
35.7k5 gold badges77 silver badges151 bronze badges
answered Jul 29, 2019 at 17:54
3
  • Thank you for your quick response. For point counts the expression functions well (the expression parameter as string needs to be in parentheses). Although I don´t exactly understand the meaning of this expression parameter (in this case "sub expression") since I can write there whatever I want but ok. For 'sum' I just get NULL values (changing the expression parameter to "pop_sum" as the field name) or an error (see above). pop_sum field is integer. Commented Jul 30, 2019 at 9:16
  • aggregate(layer:= 'hh', aggregate:= 'sum', expression:= 'sum_pop_in', filter:= contains(geometry(@parent), $geometry)) Commented Jul 30, 2019 at 9:16
  • 1
    the expression should be without any quotation marks: expression:= sum_pop_in Commented Jul 30, 2019 at 17:38
8

Since QGIS 3.16 (see the Changelog) there is also a possibility using the overlay_contains() function

With the following expression, it is possible to count the number of features within another feature (see example below)

array_length(overlay_contains('random_points_test', expression:=1))

example

answered Jul 29, 2021 at 6:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.