Reclassifying rasters using GDAL and Python?

Using @SS_Rebelious very useful answer, I am trying to reclassify rasters using `GDAL` and `Python`.

I have a grayscale raster (with values ranging from 0 to 255) and I want to reclassify it to a binary raster.

From the answer I referenced, I'm using the following line:

``````# reclassify raster values equal 16 to 7 using Numpy
temp = numpy.equal(raster, 16)
``````

I'm experimented with something like this:

``````temp = numpy.greater_equal(raster, 1)
``````

This seems to work fine, but is this best way of reclassifying a raster using Python?

• @gene I have looked at it - but I could not find any references to reclassifying rasters. – djq Oct 7 '14 at 18:24
• There are others as GDAL performance: raster classification using NumPy – gene Oct 7 '14 at 18:55
• It depends if your raster can fit in your memory. For very large raster numpy arrays might be an issue. – radouxju Oct 7 '14 at 19:41

Yes another way exists.

Just use gdal_calc.py

For example, below will convert the values below 3 to 0 and above 3 to 1. You can use equals as well.

``````gdal_calc.py -A C:temp\raster.tif --outfile=result.tiff --calc="0*(A<3)" --calc="1*(A>3)"
``````
• Are you sure that this works? In my case it seem that only last --calc is being processed, even though I made sure that new values from previous --calc didn't fall into new condition. – Marjan Moderc Dec 13 '18 at 9:50

to accomplish what your code is doing,

``````# reclassify raster values equal 16 to 7 using Numpy
temp = numpy.equal(raster, 16)
``````

another, perhaps more intuitive way is:

``````# reclassify raster values equal 16 to 7 using Numpy
temp = raster == 16  #gives you a numpy array of bools with same shape
raster[temp] = 7

#or a short cut way:
raster[raster == 16] = 7
``````

for compound logic:

``````#reclassify values 15 and 16 to 7
#first way

#shortcut way:
raster[(raster == 15) | (raster == 16)] = 7  #note brackets are needed and the logic is not the usual ||
``````

for a 0-255 raster being converted to binary:

``````#pick your own threshold other than 150
raster[raster < 150] = 0
raster[raster >= 150] = 1
``````

I hope this helps.

Yet another way with numpy is to use the numpy.select method. Which I find really intutive.

The select method takes two lists as arguments. The first expression in the first list will be reclassified to the first value in the second list. This then follows with the second expression being reclassified to the second value in the second list and so on.

The following example reclassifies an aspect raster. Classifying north facing slopes as 1 and everything else as NoData. data is a numpy array of the aspect tif.

``````outData = numpy.select([data == -9999, (data >= 0) & (data <= 67.5),
(data > 67.5) & (data < 292), data >=272],[-9999,1,-9999,1])
``````

The first expression:

`````` data == -9999
``````

matches the first value -9999 in the second list preserving NoData values.

The second expression in the list:

``````(data >= 0) & (data <= 67.5)
``````

Corresponds to the second value in the second list 1 and will be reclassify as 1.

The third expression in the first list:

``````(data > 67.5) & (data < 292)
``````

Corresponds with the third value in te second list -9999 and classify the data as -9999.

The fourth expression in the list:

``````data >=272
``````

Corresponds with the fourth value in the second list 1 and will be reclassied it as 1.