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?
-
Thank you very much!– YuvalMSep 6, 2020 at 20:23
2 Answers
Use if("height" > 3,NULL,$id)
as expression for your virtual field.
-
1I 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)
.– TarasSep 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. Sep 7, 2020 at 12:48
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
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)