[GIS] Reclassifying rasters using GDAL and Python

gdalpythonrasterreclassify

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)
numpy.putmask(raster, temp, 7)

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?

Best Answer

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)"