4

After processing a DEM with a mask to get the aspect values of some polygons I would like to create a new field classifying those values as N, NE, E, SE, S, SW, W, NW.

I've written this but I can't get what I'm looking for:

CASE
WHEN  "Aspect"  < 22.5  THEN  'N'
WHEN  (22.5 >= "Aspect" AND "Aspect" < 67.5) THEN  'NE'
WHEN  (67.5 >= "Aspect" AND "Aspect" < 112.5) THEN  'E'
WHEN  (112.5 >= "Aspect" AND "Aspect" < 157.5) THEN  'SE'
WHEN  (157.5 >= "Aspect" AND "Aspect" < 202.5) THEN  'S'
WHEN  (202.5 >= "Aspect" AND "Aspect" < 247.5) THEN  'SW'
WHEN  (247.5 >= "Aspect" AND "Aspect" < 292.5) THEN  'W'
WHEN  (292.5 >= "Aspect" AND "Aspect" < 337.5) THEN  'NW'
WHEN  "Aspect"  >= 337.5  THEN  'N'
END

Is it possible to do this using CASE-WHEN, or is it better to use another kind of conditional?

I don't know how to write ranges using conditional sentences without getting an error except this way.

1 Answer 1

7

If the Aspect raster converted to polygon or you have a shapefile with "Aspect" field name in the attribute table and you need to put the text definition, then you are almost there, but you need to put "Aspect" before >= not after:

CASE
WHEN  "Aspect" < 22.5  THEN  'N'
WHEN  ("Aspect" >= 22.5 AND "Aspect" < 67.5) THEN  'NE'
WHEN  ("Aspect" >= 67.5 AND "Aspect" < 112.5) THEN  'E'
WHEN  ("Aspect" >= 112.5  AND "Aspect" < 157.5) THEN  'SE'
WHEN  ("Aspect" >= 157.5  AND "Aspect" < 202.5) THEN  'S'
WHEN  ("Aspect" >= 202.5  AND "Aspect" < 247.5) THEN  'SW'
WHEN  ("Aspect" >= 247.5  AND "Aspect" < 292.5) THEN  'W'
WHEN  ("Aspect" >= 292.5  AND "Aspect" < 337.5) THEN  'NW'
WHEN  "Aspect"  >= 337.5  THEN  'N'
END

Here is the output:

enter image description here

But if you want to add field to a raster data, then you cannot because QGIS does not support adding attribute table to raster data.

1
  • It worked! I'd swear I tried to put "Aspect" before >= and I didn't get what I was looking for. Now, I've just copy and paste what you wrote and works perfectly. Thanks so much!
    – GisAmateur
    Apr 3, 2018 at 15:32

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.