2

Using the field filter or advanced filter expression in the attribute table of QGIS, I want to select features that have values in the first three levels and exclude features that values other than zero in levels four and onward.

Using the example data below, I want to select A and B, but not C.

Example Attribute data (letters and brackets and '...' are not in attribute table):

A] 100-000025-000000-000000-000000-000000-000000...

B] 100-000061-136148-000000-000000-000000-000000...

C] 100-023788-278991-424721-236628-000000-000000...

The value is a 144 character string broken into 21 levels. The levels in the code represent the hierarchy of the stream network – a major river would have only Level 1 of the code populated, a stream flowing directly into a major river would have levels 1 and 2 populated, and a stream flowing into this stream would have levels 1, 2 and 3 populated, and so on.

BC FWA Watershed Code

1
  • 1
    I'm confused, you say you want to select features that have values in the first three levels, then you say you want to select A and B, but A only has values in the first two levels?
    – M Bain
    Dec 24, 2021 at 21:39

2 Answers 2

1

Use Select by expression with this expression (explanation below:

array_sum(
    array_foreach (
        generate_series (0,3),
        string_to_array (levels, delimiter:='-')[@element]>0
    )
)=3

To select features with values>0 only in the first 2 levels (include first feature), change the expression to <=3 in the last line.


Explanation: The expression turns the string to an array and checks for the first 4 (0 to 3) elements if it is >0. It then calculates the sum of the elements for which this is the case. Sum = 3 means: first three elements > 0.

enter image description here

1

Using the example data, if you want to select A and B but not C, the recommended expression is:

string_to_array (levels, delimiter:='-')[3]='000000'

the expression will select all the records that have level 4 equal to the string '000000' and therefore all the features with level 1, 2 and 3 different from '000000'

enter image description here

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.