1

I have a polygon layer of ski resorts call skiResorts. I have a liner layer of pistes called pistes. The pistes layer has 2 several attributes and include aa_piste_length and aa_piste_difficulty. aa_piste_difficulty includes 3 categories, namely, beginner, intermediate and advanced.

In the skiResorts layer, I would like to make 3 new calculated fields, namely, beginner_length, intermediate_length and advanced_length which sums the value of aa_piste_length. For example, beginner_length in the skiResorts layer would be the sum of all aa_piste_difficulty that equals beginner and is within that particular skiReport polygon.

I am very confused as to how to do this. Do I need to make a one to many relationship between skiResorts and pistes? Can I just use aggregate in the Field Calculator to do this?

I have tried below but still have questions how to complete it.

aggregate(layer:= 'pistes',
      aggregate:= 'sum',
      expression:= ?? what does here?? ,
      filter:= ?? how to say where piste_difficulty = beginner ??
      )

1 Answer 1

2

First solution: use overlay_intersects()

I recommend using overlay_intersects() function (available since QGIS 3.16). It is easier to handle when it involves (as here) spatial relationship (take into consideration only overlapping pistes).

Use the follwong expression. You get an array of all aa_piste_length values from layer pistes, filtered by the value defined at the end of line 5. Use array_sum() to get the sum:

array_sum(
    overlay_intersects (
        'pistes',
        aa_piste_length,
        filter:=aa_piste_difficulty='beginner'
    )
)

Second solution: use aggregate()

Just for curiosity since you asked how to use aggregate() function. The expression argument is simply the field name whose values you want to sum. The filter, howver, has to take into consideration intersection features only as well as category values. The expression looks like this:

aggregate(
    layer:= 'pistes',
    aggregate:= 'sum',
    expression:= "aa_piste_length",
    filter:=intersects (geometry(@parent), $geometry) and "aa_piste_difficulty"='beginner' 
)

Edit

See help of the aggregate() function how the optional filter expression to limit the features used for calculating the aggregate works:

Fields and geometry are from the features on the joined layer. The source feature can be accessed with the variable @parent.

So here, we calculate the intersection of the geometry of the current (=joined) layer - $geometry - with the geometry of the source feature - geometry (@parent)

Remark: in QGIS versions 3.28 and later, $geometry is replaced by @geometry.

2
  • I used the aggregate() function, however, I do not quite understand how geometry(@parent),$geometry) works. Thank you for the multiple solutions! Commented May 27, 2023 at 23:32
  • 1
    See updated answer/Edit at the bottom of the answer for this.
    – Babel
    Commented May 28, 2023 at 9:17

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