0

I am looking to utilise gdal_calc to batch binarise a few hundred rasters. My problem is in relation to gdal_calc and the correct syntax for --calc to do what I wish to achieve. Using QGIS Raster Calculator and the following script:

("raster@1" <= -15 AND "raster@1" >= -20)*255

I can achieve the desired result, but I cant replicate this using gdal_calc.

To clarify, I am trying to make all the values between -20 and -15 in the raster = 1 or 255 and everything else = 0, making 0 nodata.

1
3

You can use gdal_calc.py with conditional statements.
This should do the trick:

gdal_calc.py -A input.tif --outfile=result.tif --calc="(A>=-20)*(A<=-15)" --NoDataValue=0

or

gdal_calc.py -A input.tif --outfile=result.tif --calc="logical_and(A>=-20,A<=-15)" --NoDataValue=0

This returns values 0 and 1. Add "255*" if you want values 0 and 255.

1
  • Thank you for that I had worked something similar but it failed gdal_calc --calc"255*(logical_and(A<= -20, A>= -15))" --format GTiff --type Float32 -A input.tif --A_band 1 --outfile output.tif --NoDataValue 0 on reflection it was my less/more than symbols which may have caused the issue. once again thanks!!! – Richard McDonnell Dec 12 '17 at 17:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.