2

I'm just getting started with QGIS. I have a layer of points with a specific value. How do I sum up the point values ​​in a designated polygon?

In the attachment screen: enter image description here

0

4 Answers 4

6

You can also use field calculator with an expression such as:

aggregate('points','sum',"vals",filter:=intersects($geometry,geometry(@parent)))

where points is the name of your point layer and vals the fieldname containing the values. Apply this in the attribute table of your polygon layer via fieldcalculator.

Make sure both layers are in the same CRS as mentioned by @Babel.

To make this update automatically everytime you modify the polygon, you can add it as a virtual field. Simply check the box for that in fieldcalculator:

enter image description here

But note this attribute will only be available in your QGIS project. It is not stored permanently in your datasource!

enter image description here

5
  • OK, seems to work, but only if both layers are in the same CRS - if they are in differnt CRS, it doesn't work.
    – Babel
    Jan 13, 2023 at 21:18
  • 2
    That can be true as it is an old expression which probably does not support on the fly reprojection, but its also working in QGIS 3.0 :)
    – MrXsquared
    Jan 13, 2023 at 21:20
  • 1. I have a layer with values ​​called <zach_pomor> with the structure: <address/pop/LAT/LTG> 2. I create a new temporary layer called <new_test>, geometry type <Polygon>, EPSG 4326 - WGS 84, 3. Draws a polygon 4. In the created layer, "open attribute tables". I use expressions either: <array_sum (overlay_contains ('west_pomor', "pop"))> or <aggregate('sach_pomor','sum',"pop",filter:=intersects($geometry,geometry(@parent))) > , it actually counts my points from the designated area and it's great.
    – Gron Men
    Jan 17, 2023 at 14:45
  • But how to do if I wanted to edit the range of the designated area and have it automatically count the value of <pop> contained in this zone. Please help
    – Gron Men
    Jan 17, 2023 at 14:45
  • about your second question on how to update the sum on every modification on your polygon, please see the updated answer about a virtual field or the answer for the virtual layer: gis.stackexchange.com/a/449796/107424
    – MrXsquared
    Jan 17, 2023 at 17:08
5

You can use "Join Attributes by Location (summary)" from processing toolbox.

Choose the polygon layer as input and the points as join layer, your field(s) to summarize and sum as summaries to calculate.

enter image description here

3
  • Thanks for your help, I have tried this method so far but I have a few questions. First, are you doing it right? 1. I have a layer with values ​​called <zach_pomor> with the structure: <address/pop/LAT/LTG> 2. I create a new temporary layer called <new_test>, geometry type <Polygon>, EPSG 4326 - WGS 84, 3. Draws a polygon 4. I run join attributes by location 5. "Join to features in" - <new_test> 6. "by comparing it" - <zach_pomor> 7. Total field - <pop> 8. Field to be calculated - <sum> 9. Run 10. A "join layer" is created, where I reveal the labels and select <pop_sum>
    – Gron Men
    Jan 17, 2023 at 9:42
  • 2. Second: Am I able to edit the temporary layer (e.g. by manually stretching the vertices) so that it immediately updates the <pop_sum> value? thank you in advance for your help
    – Gron Men
    Jan 17, 2023 at 9:42
  • about your second question on how to update the sum on every modification on your polygon, please see the updated answer about a virtual field: gis.stackexchange.com/a/449573/107424 or the answer using a virtual layer: gis.stackexchange.com/a/449796/107424
    – MrXsquared
    Jan 17, 2023 at 17:09
4

Create a new attribute field with Field calculator and this expression, using overlay_contains() function (QGIS 3.18 or above):

array_sum (overlay_contains ('point_layer', "value"))

Replace point_layer with the point layer's name and value with the field name of the attribute that contains the value you want to sum up.

2
  • Maybe explain in a more verbose way "Create a new attribute field in the polygon layer with .."? Jan 14, 2023 at 19:07
  • I will also ask for a hint on how to do it, I will be very grateful. Should I create a temporary layer by selecting the polygon where the points whose values ​​are to be summed are located? Thank you very much for help
    – Gron Men
    Jan 17, 2023 at 9:46
1

You use a virtual layer with a query such as:

select pol.geometry, sum(poi.vals) as valsum from polygon as pol
join points as poi on ST_Intersects(pol.geometry,poi.geometry)

This will update every time you modify the polygon layer and recalculate the sum.

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .