Many examples on gis.se that refer to gdal_calc give a boolean example. example 1 example 2

If I need to calculate a conditional statement on a float:

If Raster_Value >= 1024, then 1024; else Raster_Value.

How do I perform that operation in the gdal raster calculator command line

EDIT I tried radouxju answer and it worked, except for the cell values that didn't. here are 3 kinds of results I got

enter image description here

Here is the tool gui and the parameters I've entered enter image description here


you can define the output type using the "type" option

--type=TYPE output datatype, must be one of ['Int32', 'Int16', 'Float64', 'UInt16', 'Byte', 'UInt32', 'Float32']

so it would look like this

gdal_calc.py -A yourInput.tif --outfile=yourOutput.tif --calc="1024*(A>=1024)+A*(A<1024)" --NoDataValue=0 --type='Float32'
  • Well, that seemed to work, apart from the fact that certain cells below threshold were changed too. i68.tinypic.com/10pxcig.png
    – HDunn
    Dec 10 '15 at 11:52
  • this is strange. Do you also have unexplained results with Float64 ?
    – radouxju
    Dec 10 '15 at 13:42
  • I have a similar question, so, how can I change the pixel value to entire raster? In SAGA GIS it's simple: ifelse (a=0,1,a). How to do this via gdal_calc.py? Feb 8 '18 at 14:20
  • 2
    I would use "--calc="(a==0)*(1)+(A!=0)*(A)" Nov 6 '19 at 3:58

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.