4

I am using QGIS 3.16 (Field Calculator). I have a number of values in the attributes of my GML file that read:

  • 'Nonconiferous Trees' or
  • 'Nonconiferous Trees (Scattered)' or
  • 'Nonconiferous Trees, Scrub' or
  • 'Nonconiferous Trees, Coniferous Trees'.

I want to create a new attribute column called Type and select all those values containing the text 'Nonconiferous Trees' and create a new value in the Type column called 'Mixed Woodland'. I am running

CASE
WHEN "descriptiveTerm" ILIKE '%Nonconiferous Trees%' THEN 'Mixed Woodland Fill'
END

But all it picks up is 'Nonconiferous Trees' or 'Nonconiferous Trees (Scattered)'. Its like it doesn't like the comma?

1
  • From you comments, it becomes clear that the problem is probably the GML format you use: these are not meant to be edited (in QGIS, at least). You should stick to Geopackage (preferably) or Shapefiles if you want to edit data. See: gis.stackexchange.com/questions/226822/… - with Geopacke, even you initial expression seems to work correctly.
    – Babel
    Mar 30, 2022 at 12:15

3 Answers 3

5

Use regexp_match() to find the features that contain the string 'Nonconiferous Trees', see documentation:

case
when regexp_match( descriptiveTerm, 'Nonconiferous Trees')
then 'Mixed Woodland Fill'
end

enter image description here

2
  • Thank you for the suggestion. When I run this on my GML data it only picks up those values that have just Nonconiferious Trees or Nonconiferious Trees (Scattered). It ignores those like Nonconiferous, Scrub or Nonconiferous, Coniferous. ??? Mar 30, 2022 at 10:53
  • Can you provide sample data for testing?
    – Babel
    Mar 30, 2022 at 11:24
2

If you want to use the LIKE function, you can use it in the following way:

case
when  "FieldName" like ('Nonconiferous Trees%') then 'Mixed Woodland'
end

This means selecting all texts starting with Nonconiferous Trees and including additional texts afterward.

Here is the output:

enter image description here

1
  • Thank you for the suggestion. When I run this on my GML data it only picks up those values that have just Nonconiferious Trees or Nonconiferious Trees (Scattered). It ignores those like Nonconiferous, Scrub or Nonconiferous, Coniferous. ??? Mar 30, 2022 at 10:54
2

Thanks for everyones help. The issue was an array thing and the array_match command worked in the end:

WHEN 
    ( array_contains(descriptiveTerm, 'Nonconiferous Trees') OR 
      array_contains(descriptiveTerm, 'Nonconiferous Trees (Scattered)') ) 
    AND 
    ( array_contains(descriptiveTerm, 'Coniferous Trees') OR 
      array_contains(descriptiveTerm, 'Coniferous Trees (Scattered)') )
THEN 
    'Mixed Woodland Fill'
2
  • 2
    Could you add the expression you used to your answer? It will help others having similar issue in the future.
    – Matt
    Apr 1, 2022 at 11:12
  • Apologies for the delay. Expression used was; WHEN ( array_contains(descriptiveTerm, 'Nonconiferous Trees') OR array_contains(descriptiveTerm, 'Nonconiferous Trees (Scattered)') ) AND ( array_contains(descriptiveTerm, 'Coniferous Trees') OR array_contains(descriptiveTerm, 'Coniferous Trees (Scattered)') ) THEN 'Mixed Woodland Fill' Apr 6, 2022 at 13:23

Your Answer

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

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