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.
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)))
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.
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.
Answer
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 awithin(geometry(@parent), $geometry)
:within(a,b)
tests whether a geometry is within another. Returns 1 (true) if geometry a is completely inside geometry b
No comments:
Post a Comment