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:

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'

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.


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:

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'

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.

| improve this answer | |
  • 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 '18 at 15:32
  • Please accept the answer if it solved your problem so that it can help others. – ahmadhanb Apr 3 '18 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.