4

I have a big data set with a column name "CORINE" with strings in it. I want to change these individual strings into integers and put them into a new column so that I can create a raster layer out of the created values. I tried this code in the field calculator.

CASE 
    WHEN "CORINE" = "AG" THEN 1
    WHEN "CORINE" = "BG" THEN 2
    WHEN "CORINE" = "LW" THEN 3
    WHEN "CORINE" = "NW" THEN 4
    WHEN "CORINE" = "MW" THEN 5
    WHEN "CORINE" = "WW" THEN 6
    WHEN "CORINE" = "WA" THEN 7
    WHEN "CORINE" = "DK" THEN 8
    WHEN "CORINE" = "LN" THEN 9
    WHEN "CORINE" = "NV" THEN 11
    WHEN "CORINE" = "SU" THEN 12
    WHEN "CORINE" = "SV" THEN 13
    WHEN "CORINE" = "TM" THEN 14
    ELSE 0
END

But after the calculations, all the values in the new column are 0. What is my mistake?

enter image description here

0

2 Answers 2

5

Use single quotes ' as string indicator, not ". Double quotes are used as Field indicator in QGIS field calculator. So your expression compares two fields, where the second one does not exist or is not equal to your first field, thats why it always jumps to the else case. Try this:

CASE 
  WHEN "CORINE" = 'AG' 
  THEN 1
  WHEN "CORINE" = 'BG' 
  THEN 2
  WHEN "CORINE" = 'LW' 
  THEN 3
  WHEN "CORINE" = 'NW' 
  THEN 4
  WHEN "CORINE" = 'MW' 
  THEN 5
  WHEN "CORINE" = 'WW' 
  THEN 6
  WHEN "CORINE" = 'WA' 
  THEN 7
  WHEN "CORINE" = 'DK' 
  THEN 8
  WHEN "CORINE" = 'LN' 
  THEN 9
  WHEN "CORINE" = 'NV' 
  THEN 11
  WHEN "CORINE" = 'SU' 
  THEN 12
  WHEN "CORINE" = 'SV' 
  THEN 13
  WHEN "CORINE" = 'TM'
  THEN 14  
  ELSE 0
END
1
  • Thank you it worked!
    – Mike
    Nov 25, 2021 at 16:07
5

Here is another IMHO a bit sophisticated approach:

if("CORINE" IS NULL,
    0,
    map_get(
        map('AG',1,'BG',2,'LW',3,'NW',4,'MW',5,'WW',6,'WA',7,'DK',8,'LN',9,'NV',11,'SU',12,'SV',13,'TM',14),
        "CORINE"
        )
    )

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.