5

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

1 Answer 1

5

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'
4
  • Well, that seemed to work, apart from the fact that certain cells below threshold were changed too. i68.tinypic.com/10pxcig.png
    – HDunn
    Commented Dec 10, 2015 at 11:52
  • this is strange. Do you also have unexplained results with Float64 ?
    – radouxju
    Commented Dec 10, 2015 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? Commented Feb 8, 2018 at 14:20
  • 2
    I would use "--calc="(a==0)*(1)+(A!=0)*(A)" Commented Nov 6, 2019 at 3:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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