0

I've got a line layer called "Water Lines - Proposed" that has multiple line features. Each feature has a 'Type of Feature' field (multiple features may share the same value), a 'Name' field (each feature has a unique name), and a 'Length' field, which is calculated.

enter image description here

I've got a second layer with a value relation field allowing me to select multiple features from the "Water Lines - Proposed" layer by 'Name' from a filtered list with [Spillway (sill)] as the 'Type of Feature'. I would like to then calculate the total length of all features that have been selected in this list ('Spillway (sill) Length').

enter image description here

The following expression worked for getting the length of a single feature, but I cannot figure out how to modify it now that I have allowed for multiple selection.

enter image description here

1 Answer 1

0

You can use the relation_aggregate function in the field calculator on the parent layer like so:

relation_aggregate(
    relation:='my_relation_name',         -- the name of the relation
    aggregate:='sum',                     -- the type of aggregation
    expression:="Length (ft)"             -- the field in the child layer to aggregate
)

The child layer:

enter image description here

The parent layer (with the calculated field):

enter image description here

2
  • Thanks @matt - I'm confused about what to put for the "my_relation_name" part of the expression? The list of spillway names that I can select from in the "Associated Spillway (sill) Feature" prompt is provided by a value relation widget in the form, with "Water Lines - Proposed" selected as the layer and "Name" selected as both the Key column and Value column.
    – Brightside
    Commented Nov 11, 2022 at 0:29
  • Ah, I didn't realise you were using a widget. The expression works when you set up the relation through the project properties. Try the expression in this answer instead. It doesn't use a relation per se, but it matches features using a common field.
    – Matt
    Commented Nov 11, 2022 at 7:06

Your Answer

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

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