3

I've got a line layer shapefile with lots of values in two columns ("Verstoord" and "Beleid"). I'm trying to fill a third column ("Advies") with values based on the first two columns. This is the code I'm using:

if("Verstoord" = 'ja' OR "Beleid" = '8' OR '9' OR 'Vrij' OR 'Laag' OR 'Geen' OR 'Z5','geen',if("Verstoord" = 'nee'  AND "Beleid" = '3'  OR 'Z2' OR 'Z3', 'Archeologische begeleiding', if("verstoord" = 'nee' AND "Beleid" = '5' OR '6' OR 'Hoog' OR 'Z4s', 'Verkennend booronderzoek (25 meter)', if("Verstoord" = 'nee' AND "Beleid" = '7' OR 'Middelhoog' OR 'Z5s','Verkennend booronderzoek (50 meter)',"Advies"))))

But it keeps giving the Eval error that certain values, mainly those with letters in them, can't be converted to boolean.

I'm working with QGIS 3.34.10.

I changed the code, and it helped a bit, but not only the first 'if' is executed

if("Verstoord" = 'ja' OR "Beleid" IN ('8', '9', 'Vrij', 'Laag', 'Geen', 'Z5'),'geen',if("Verstoord" = 'nee'  AND "Beleid" IN ('3', 'Z2', 'Z3'), 'Archeologische begeleiding', if("verstoord" = 'nee' AND "Beleid" IN ('5', '6', 'Hoog', 'Z4s'), 'Verkennend booronderzoek (25 meter)', if("Verstoord" = 'nee' AND "Beleid" IN ('7', 'Middelhoog', 'Z5s'),'Verkennend booronderzoek (50 meter)',"Advies"))))
3
  • 3
    It is supposed to be "Beleid" = '8' OR "Beleid" = '9' and not "Beleid" = '8' OR '9', however, the best practices encourage to use the IN-operator, i.e. "Beleid" IN (8, 9). If the "Beleid" field is of integer type, quotation marks can be skipped 9 and not '9'. Commented Apr 4 at 7:27
  • Thank you! This has helped, though it doesn't work for all values. It has only done the first if, for "Beleid"values 8, 9, Vrij, Laag, Geen or Z5 Commented Apr 4 at 7:56
  • 1
    Not sure if it matters, but you forgot to capitalize the field "Verstoord" at one point. Commented Apr 4 at 8:41

1 Answer 1

3

If you have nested if conditions, it is better to use case...when conditions, which is easier to understand and control:

CASE is used to evaluate a series of conditions and return a result for the first condition met.

And instead of IN (which should also work and is probably easier), you can also use array_contains(). So try this expression:

case
    when "Verstoord" = 'ja' OR array_contains (array ('8','9','Vrij','Laag','Geen','Z5'), "Beleid")
    then 'geen'

    when "Verstoord" = 'nee'  AND  array_contains (array ('3','Z2','Z3'), "Beleid")
    then 'Archeologische begeleiding'

    when "verstoord" = 'nee' AND array_contains (array ('5','6','Hoog','Z4s'), "Beleid")
    then 'Verkennend booronderzoek (25 meter)'

    when "Verstoord" = 'nee' AND array_contains (array ('7','Middelhoog','Z5s'), "Beleid")
    then 'Verkennend booronderzoek (50 meter)'

    else "Advies"
end

These two expressions do the same:

  • "Beleid" IN ('8', '9', 'Vrij', 'Laag', 'Geen', 'Z5')
  • array_contains (array ('8','9','Vrij','Laag','Geen','Z5'), "Beleid")
4
  • If I'm reading this correctly, the array is now selected from the field "Verstoord" instead of "Beleid"? And does the else "Advies" not need a value to fill the "Advies" field with? Commented Apr 4 at 8:27
  • Sorry, your question is uncomplete and I only try to guess from your code to find out what your intention is. I provide a working code with what I guess was your intention. So to get a complete answer, describe a bit more in detail what you want to do. Commented Apr 4 at 8:29
  • No, array_contains works on the field Beleid, see at the end of the function, see also documentation of the function with the syntax: docs.qgis.org/3.40/en/docs/user_manual/expressions/…. And else works if all of the conditions above are false, then the value of the field Advies is returned. Don't know, however, if that is what you want? Commented Apr 4 at 8:31
  • Ah okay, now I understand better. Apologies for the missing information, I usually don't use these extensive expressions a lot, so I didn't know how much was needed. I'll play around with your suggestions to see what works best! Commented Apr 4 at 8:39

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.