2

I've been reading through question and answers on the Aggregates Function, but have been unable to obtain a clear understanding and solution to my problem. I have a polygon grid ("Grid 4km") and I have another vector polygon layer ("S1 Grid Intersection Strata"). I want to calculate the area of the polygon layers that are within a grid cell. I intersected the vector polygon layer with the grid so that you can select on polygons within the grid.

A cell from "Grid 4km" with the "S1 Grid Intersection Strata" selected

The steps I have taken so far:

  • I calculated the $area in a field called Area in "S1 Grid Intersection Strata".
  • I worked in the field calculator for "Grid 4km" to create a new field called "S1 Area" using the following:

    aggregate('S1 Grid Intersection Strata', 'sum', "area", filter:=intersects($geometry, geometry(@parent)))

Field Calculator with code

This seems to work, but when I select on individual examples and calculate by hand the sum of the area is not what is showing up in the new "S1 Area" field. The image below shows the polygons selected within the grid with the area values. The sum of the areas in the "S1 Grid Intersection Strata" do not add up to the value in the "Grid 4km" "S1 Area" field.

Fields with area values

I am hoping that someone might be able to provide a good explanation of what the @geometry component in the formula above is doing and how I can fix this to sum up the area of the intersecting polygons in the grid.

  • Do you mean the $geometry or the @parent component? ;-) – Jochen Schwarze Nov 20 '19 at 7:00
  • Both. I looked into the documentation but it wasn’t clear. Perhaps there’s a reference that would help with that I haven’t found yet. Hoping that I can solve by having a better understanding. Thanks – Mark Thompson Nov 20 '19 at 7:15
3

The syntax of your aggregate is fine. But by using intersects(), the aggregate also sums the features that touches your grid.

A visual explanation: http://www.qgis.nl/2019/08/25/select-by-location-hoe-zit-dat-met-die-opties/?lang=en

$geometry refers to the current geometry of your grid. In this case the grid with id=128

geometry(@parent) refers to the geometries of the layer that's called in the aggregate. In your case the geometries of the layer 'S1 Grid Intersection Strata'

To calculate the sum of area's you can use both within() or contains(). Be aware of the right syntax! (more info on https://docs.qgis.org/testing/en/docs/user_manual/working_with_vector/expression.html#geometry-functions)

  • contains($geometry, geometry(@parent)): contains(a,b) returns 1 (true) if and only if no points of b lie in the exterior of a, and at least one point of the interior of b lies in the interior of a
  • within(geometry(@parent), $geometry): within(a,b) tests whether a geometry is within another. Returns 1 (true) if geometry a is completely inside geometry b
| improve this answer | |
  • Thank you very much! This solved the problem and I have a better understanding of this function. I had read the manual that you had linked and should have been able reason this out. Switching the a,b in the two examples of contains v. within gives the full picture of the coding. – Mark Thompson Nov 20 '19 at 16:30

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.