3

In this project (using QGIS 3.18.1) the filter "veg_actu1" = '600000' or "veg_actu1" = '601000' or "veg_actu1" = '601100' or "veg_actu1" = '602000' or "veg_actu1" = '603000' does not work properly (white areas should be rendered)

enter image description here

If the filter is replaced by regexp_match(veg_actu1, '(600000|601000|601100|602000|603000)') or by case when "veg_actu1" in ('600000','601000','601100','602000','603000') then 1 end it works fine. What am I missing?

Here is a link to the project: https://drive.google.com/file/d/1slBl3b2Pd8yqDFqE4RW7L3bstAujg7dU/view?usp=sharing

If only this rule is on, display is correct.

enter image description here

1
  • 3
    the regex is not the same. It looks like the field is a string, not a number. If there is a trailing space (or any other character as a matter of fact), the equality will fail but not the regex, as the matching string does not have to be the entire string (regexp_match(veg_actu1, '(6)') will return 1 here). The case when is closer to the equality comparison though.
    – JGH
    Mar 29 at 18:02
4

There may be trailing or leading spaces - as @JGH pointed out. Try using this expression:

trim("veg_actu1") LIKE '600000' or trim("veg_actu1") LIKE '601000' or trim("veg_actu1") LIKE '601100' or trim("veg_actu1") LIKE '602000' or trim("veg_actu1") LIKE '603000'

and see if you get the proper results

1
3

There are several ways to solve your problem, one involving other field-format and four avoiding the problematic or operator:

  1. Use numbers, not strings - the solution closest to what you have
  2. Use like functions - the easiest an most elegant solution
  3. Use case condition - the classic solution for such combined conditions
  4. Use if condition - works, but a bit more complicated
  5. Use in operator -tribute to @jdavid05's comment

In detail:

  1. Simply leave away the single quotes: as you don't have texts, but numbers, they are not necessary - thus not "veg_actu1"='600000', but "veg_actu1"=600000 :

    "veg_actu1" = 600000 or "veg_actu1" = 601000 or "veg_actu1" = 601100 or "veg_actu1" = 602000 or "veg_actu1" = 603000

enter image description here

  1. function like, used with wildcards (underscores: _)
    "veg_actu1" like '60____'
  1. case-condition:
    CASE 
    WHEN "veg_actu1" = '600000' then 1
    WHEN "veg_actu1" = '601000' then 1
    WHEN "veg_actu1" = '602000' then 1
    WHEN "veg_actu1" = '601100' then 1
    WHEN "veg_actu1" = '603000' then 1
    else 0
    end
  1. if-condition:
if (
    "veg_actu1" = '601100',1,
        if (
            "veg_actu1" = '600000', 1,
                if (
                    "veg_actu1" = '601000', 1,
                        if (
                            "veg_actu1" = '602000', 1,
                                if ( 
                                    "veg_actu1" = '603000',1,0
                                )
                        )
                )
        )
)
  1. in-operator (as proposed in the comment by user @jdavid05)
"veg_actu1" IN ('601100','600000','601000','602000','603000') 

Background

After some testing, it turns out that the order of the original expression you used plays a role:

This expression returns styling for some, but not all polygons:

"veg_actu1" = '600000' or "veg_actu1" = '601000' or "veg_actu1" = '602000' or "veg_actu1" = '601100' or "veg_actu1" = '603000'

But with this expression, no polygon at all gets styled:

"veg_actu1" = '601100' or "veg_actu1" = '600000' or "veg_actu1" = '601000' or "veg_actu1" = '602000'  or "veg_actu1" = '603000'

I think the problem is linking several or opeartors one after the other. The help says:

Operator OR: Returns 1 when condition a and b are true.

The examples in the help are like this:

  • 4 = 2+2 OR 1 = 1 → 1
  • 4 = 2+2 OR 1 = 2 → 1
  • 4 = 2 OR 1 = 2 → 0

But you have not something like A OR B, but A OR B OR C OR D OR E. This seems to be problematic, at least when used with strings.

7
  • Yes, it works but it is a bit strange considering that veg_actu1 data type is QString. Also, if only this rule is checked (rest of styles unchecked) in the original project, the display is correct. Some kind of bug? Mar 30 at 12:53
  • 1
    Hm, strange, indeed. So we know how to avoid the problem, but not why it is a problem. However, if you use numbers, it's always good to format them so and not to have them in a string-format.
    – Babel
    Mar 30 at 13:18
  • 1
    @Jorge Rosales see updated answer: the problem seems to be the or operator
    – Babel
    Mar 30 at 14:15
  • 1
    You could also use the IN operator. ` "veg_actu1" IN ('601100','600000','601000','602000','603000') `
    – jdavid05
    Mar 30 at 14:32
  • 1
    @Babel I agree with the format issue but in this case there are codes like 0111A0, 0121D0 or 0111E0. Mar 30 at 15:34

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.