4

I'm a total newbie at coding. I'm working with water catchments in QGIS. Runoff of some catchments feeds other catchments. I'm trying to get the cumulative run-off.

Each feature in the table has the following fields:

  • ID - each polygon's number
  • Fed by - the numerical ID of a polygon that feeds it
  • C - the (calculated) runoff capacity of that field

I would like to create a field C-in that pulls the value of C from another feature based on the ID of that feature if it is written in Fed by field.

I imagine it is something along the lines of: When Fed by equals the value of an ID, get C of that ID, for example:

enter image description here

In particular, we have:

  • ID: 19, Fed by: 9, C: 0.5

  • ID: 20, Fed by: 19, C: 0.7

So C-in of ID=20 should be: 0.5 (forget for a moment the C-in of ID=19)

So what do I need to write in the field calculator to get this magic to happen?

7

Try the following expression:

attribute(get_feature(@layer_id,'id',"fed by"),'C')

The get_feature call finds the feature in the current layer whose id is equal to the current feature's fed by. Note double quotes on fed by since the value of this field for the current feature should be evaluated in the expression. However id has single quotes since it is a parameter to get_feature to tell it what field name to use, and should not be replaced by the current feature's id (20 in your example). Then attribute pulls out the value of C in that feature (not double quotes for same reason as before, to avoid it being replaced by 0.7 during evaluation).

Nonanswer

I was all set to suggest you create a join (in Layer Properties) to the same layer and then link on the fed by field. But it seems QGIS doesn't permit self-joins.

Remark

You could probably save some work, if you haven't gone too far down this path yet, by instead adding a field describing what each catchment flows in to, and then using the aggregate function to find and sum over all (sub)catchments that flow into your catchment. This would replace your multiple fed by # fields, but the linkage would go the other way (upstream links to single downstream, rather than downstream links to several upstream).

And all of this gets a bit more complicated if you have multiple layers of fed-bys, i.e. A feeds into B which feeds into C. Your current logic -- or my alternative in this remark -- will work if A and B both feed into C, not if they're nested, so to speak.

|improve this answer|||||

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.