3

I am using QGIS 3.12. I have a layer of trees and a virtual field 'tree number' that counts all trees that I add to the layer ($id). I want to set that if a height of a tree is more than 3 (in field 'height') it will not be counted in the 'tree number' field. Can it be done?

1
  • Thank you very much!
    – YuvalM
    Sep 6, 2020 at 20:23

2 Answers 2

3

Use if("height" > 3,NULL,$id) as expression for your virtual field.

2
  • 1
    I think your solution is not full, cause as soon as a value out range occurs the id-chain will be broken. Also, I will probably write your expression it as if("height">3, $id, NULL).
    – Taras
    Sep 7, 2020 at 5:05
  • @Taras yes, you are almost right. Guess I misunderstood the question a little. Your answer is the one to go then, except to my logic, you transposed case_true and case_false.
    – MrXsquared
    Sep 7, 2020 at 12:48
3

Despite a suggestion from @MrXsquared, I think this task has to be tackled a slightly different way.

To achieve a continuous id-chain, try the following expression in the Field Calculator

if("height">3, array_find(array_agg($id, filter:="height">3), $id), NULL)

Resulting in

result

If the "id"s have to start with 1, then use the following expression

if("height">3, array_find(array_agg($id, filter:="height">3), $id) + 1, NULL)
2
  • That looks greate. I thank you very very much.
    – YuvalM
    Sep 7, 2020 at 5:33
  • please accept one of the answers as a valid one
    – Taras
    Sep 7, 2020 at 5:36

Your Answer

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

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