I'm trying to use GDAL calc to convert values in a .tif file, I've done this before with GDAL calc but for converting to a single value. Now I'm trying to convert multiple values in this fashion:

0.0 - 0.2 -> 1

0.2 - 0.4 -> 2

0.4 - 0.6 -> 3


I've tried this:


    call([sys.executable, 'C:\Program Files (x86)\GDAL\gdal_calc.py', '-A', 
    inraster, "--outfile=" + outraster, '--calc='+calculation,'--

This results in the following error:

    0 .. evaluation of calculation [thecalculation] failed

Any ideas? Maybe I need to split up the steps of converting but this could add significantly to the run time.

  • have you tried (0<A)*(A<=0.2)*1 + ...
    – radouxju
    Commented Dec 4, 2017 at 13:39

1 Answer 1


I think the problem is chaining operators with an array. Python can handle a<b<c but only if a, b and c are scalars. In this case b is an array. For more: see docs and a discussion.

Assuming that is the problem, then based on this answer I imagine you can use something like this:


Failing that there is a useful tool called gdal_reclassify that is straight forward to use and has clear docs. Your use case would require something like this (NB only done for 4 classes):

python gdal_reclassify.py source_dataset.tif destination_dataset.tif -c "<0.2, <0.4, <0.6, <0.8" -r "1, 2, 3, 4" -d 0 -n true -p "COMPRESS=LZW"

You could build this call into your script as well.

If you're already working in a python script you could also just read to numpy array, and use numpy directly to do the reclassification.

  • gdal_reclassify worked a charm! Fantastic answer, thank you so much!
    – Francis
    Commented Dec 4, 2017 at 13:53
  • And for the record, the method with logical_and's did not work, it threw an error.
    – Francis
    Commented Dec 4, 2017 at 13:54
  • Think that was my fault with the brackets. Might work now, not that it's needed!
    – RoperMaps
    Commented Dec 4, 2017 at 14:00

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.