2

Following the examples in the QGIS documentation I am able to classify a raster using the raster calculator, e.g. based on the aspect derived from a DEM:

((aspect@1 > 270) AND (aspect@1 <= 90)) * 1 + ((aspect@1 > 90) AND (aspect@1 <= 270)) * 2

This will classify all slightly northward facing slopes as 1 and all facing southward as 2.

But when I try to mask/take into account areas with little to no slope (less than 3 degrees), the classification wont work properly - not all areas with a slope less than 3 degrees will be classified accordingly.

(((aspect@1 > 270) AND (aspect@1 <= 90)) * 1 + ((aspect@1 > 90) AND (aspect@1 <= 270)) * 2) + (slope@1 <= 3) * 3

Where am I going wrong? I can't see any logic why some areas are classified as 3, and others, which should be, aren't. At least there's no wrong classification of areas above 3 degree slope as 3.

Exemplary images of my problem:

  1. The white areas are those with a slope less than 3 degrees. enter image description here

  2. The white areas from image 1 aren't uniform anymore, but they should be. enter image description here

18
  • 1
    No pixel can have a value less than 90 and greater than 270 at the same time, so it amazes me that the first condition is working as it should. Regarding the second, if it worked well, would not assign 3 to any pixel, but 4 or 5, depending on the aspect, to those with a slope less than 3 degrees, and 1 or 2 for those who do not meet that condition. Aug 18 '21 at 11:53
  • For northward facing slopes it is enough to use this expression: aspect@1 > 90 or aspect@1 < 270 to include the northern half (from a quarter to until a quarter past, to speakck in the clock-analogy). I don't know why you have such a complicated expression that is even contradictory in itself (as mentioned by @Gabriel De Luca).
    – Babel
    Aug 18 '21 at 13:02
  • If you want all the cardinal directions (N/E/S/W) to be represented by a quarter-circle, you should change 90 to 45 and 270 to 315, however. An aspect of 89 degrees is nor really oriented to the North, but rather to the East - see: see: docs.qgis.org/3.16/en/docs/user_manual/processing_algs/qgis/…
    – Babel
    Aug 18 '21 at 13:06
  • Thank you @Babel, but this is a simplified expression for question purposes only ;-)
    – Erik
    Aug 18 '21 at 13:20
  • 1
    Sorry: what does the second image show? If in the first image you had the condition slope <=3, in the second one you added an additional condition - namely: slope <=3 AND northward facing aspect, right? So it is logic that you get less pixels for which both conditions are fulfilled. The image looks perfectly plausible (as far as I can see without knowing what data you have and what exactly you want to achieve)
    – Babel
    Aug 18 '21 at 14:09
2

In order not to make mistakes classifying in the raster calculator, it is best to explicitly write the conditions in all terms, taking into account that each term is mutually exclusive from the others :

((slope@1 > 3) AND ((aspect@1 <= 90 OR aspect@1 > 270)) * 1 +
((slope@1 > 3) AND ((aspect@1 > 90 AND aspect@1 <= 270)) * 2 +
(slope@1 <= 3) * 3

Then, knowing that True = 1 and that False = 0, we can begin to simplify the algebra of sets:

(slope@1 > 3) +
(slope@1 > 3) * (aspect@1 > 90) * (aspect@1 <= 270) +
(slope@1 <= 3) * 3

Until you get to the simple form:

3 - (slope@1 > 3) * (1 + (aspect@1 > 270) + (aspect@1 <= 90))  

The algebraic operation is not intuitive, but it is useful to understand how it works.

2
  • I know why I dropped out of Mathematics in university. Because I still don't get how you got from step 2 to step 3 - but anyway, step 1 is perfectly logical to me, and seems about right.
    – Erik
    Aug 19 '21 at 7:20
  • Is not exactly math, but mainly algebra of sets. Aug 19 '21 at 10:50
2

You want to categorize your DEM to 5 categories:

  1. Slopes > 3 facing to SSE (90 to 157.5) - coded with value of 3
  2. Slopes > 3 facing to SW (157.5 to 225) - coded with value of 2
  3. Slopes > 3 facing to WNW (225 to 292.5) - coded with value of 4
  4. Slopes > 3 facing to WNW to E (below 90 and above 292.5) - coded with value of 5
  5. Slopes <= 3 (facing any direction) - coded with value of 1

So you check the DEM for two characteristics: is the slope smaller or larger than 3? And if it is larger than 3: what aspect does it have?

In your expression, however, for conditions 1 to 4 you only check for the aspect and don't include any statement for the slope. You should add an expression that multiplies it with the condition "slope@1" > 3: this returns 1 if true (slope larger 3), 0 if false (slope smaller 3). Like this, you get a value of 0 for all conditions from 1 to 4 if the slope is smaller than 3. If it is larger, you get the category values you want (2, 3, 4 or 5).

So simply replace the operator for the last condition from addition (+) to multiplication (*). The expression in the raster calculator should look like:


(
    (
        ("aspect@1" < 90) OR ("aspect@1" > 292.5)
    ) * 5 + 
    (
        ("aspect@1" >= 90) AND ("aspect@1" <157.5)
    ) * 3 + 
    (
        ("aspect@1" >= 157.5) AND ("aspect@1" < 225)
    ) * 2 + 
    (
        ("aspect@1" >= 225) AND ("aspect@1" <= 292.5)
    ) * 4   
) * 
("slope@1" > 3) * 1

This is the result, based on the DEM you used - black (pixel-value = 0) is used for more or less flat areas (slope <=3), regardless of aspect:

enter image description here

1
  • Great breakdown of the calculations/expression, thank you very much.
    – Erik
    Aug 19 '21 at 10:23

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.