I have a raster with range 0 - 1. I want to polygonize them in QGIS according to their value to be later classified from "Very Low" to "Very High".

raster_value possible_polygon_value class
0-0.2 0 Very Low
0.2-0.4 1 Low
0.4-0.6 2 Medium
0.6-0.8 3 High
0.8-0.1 4 Very High

I am guessing there is a way to do it by using Raster calculator first to assign values to each pixel first. However, how? There is no "IF" and "ELSE" statement in raster calculator.

What I think:

IF ("raster@1" <= 0.2) * 0,
ELSE IF ("raster@1" > 0.2 AND "raster@1" <= 0.4) * 1,
ELSE IF ("raster@1" > 0.4 AND "raster@1" <= 0.6) * 2,
ELSE IF ("raster@1" > 0.6 AND "raster@1" <= 0.8) * 3,
ELSE IF ("raster@1" > 0.8) * 4
  • Added another solution with raster calculator as you initial idea was.
    – Babel
    May 26, 2021 at 16:47

1 Answer 1


What you want to do is the re-classify the raster before polygonizing. You can GRASS tool r.recode for that (available in QGIS).

For a solution with raster calculator, see below.

Using GRASS r.recode

  1. You need a simple text-file defining the classes. Just copy the text below in a file and save it as .txt file (you can use a text editor for that, as e.g. the simple Editor tool in Windows).

  2. Run Menu Processing / Toolbox / r.recode, set the raster as input and the saved txt-file as File containing recode rules.

  3. You get a new raster as output, containing just five values (categories): 1 to 5. Than polygonize this raster to get your polygons.

Paste this text in an otherwise empty txt file. The first two numbers, devidid by : define the range of values you want to group together to a new category, the last value is the numbering of the category:


Screenshot: A simple raster with random-values form 0.009 to 0.997 (see histogram on the right side). Recoded and vectorized. In the resulting vector layer, category 5 = very high is highlighted with red outlines and labeled accordingly: as you can see (especially in the cases marked with arrows), the red outlined polygons contain pixels with different values (shades of gray), but in a certain range (0.8 to 1):

enter image description here

Edit: to add another solution that correpsonds more to your initial idea:

Using raster calculator

Run Menu Raster / Raster calculator, paste this expression and run it:

("raster@1" * 1000 <= 200 ) *1 +
("raster@1" * 1000 <= 400 ) *1 +
("raster@1" * 1000 <= 600 ) *1 +
("raster@1" * 1000 <= 800 ) *1 +
("raster@1" * 1000 <= 1000 )*1

Explanation: There is no direct conditional implemented, but you can imitate it. For that evaluate if the pixel-value in case is smaller/equal than 0.2: "raster@1" <= 0.2. This returns 1 for true, 0 for false. Multiply this by 1 and add further conditions in steps of 0.2: each time it is fulfilled, it adds 1. For pixel value 0.9, you thus have: 0+0+0+0+1= 1. Like this, you get an output-raster with 5 categories, inverse to the r.recode solution, category of smallest values getting highest category-code and the other way round.

However, raster calculator seems not to work properly with decimal numbers. Because of that, first multiply the values by 1000. Finally, polygonize as above.

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.