QGIS Raster Calculator – How to Perform Classification

classificationqgisraster-calculator

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

Best Answer

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.

Related Question