I have two rasters. One with the following values: 0,1,2,3,4,5,6,7,8,9,10 and the other one with continuous values ranging from 0 to 10000. I would like to create a raster where only values of the second raster will be kept when the first raster has values from 4 to 6. How could I do this using gdal_calc.py?

I tried:

gdal_calc.py -A first_raster.tif -B second_raster.tif --outfile=Result.tif --calc="B*(3<=A>=7)" --NoDataValue=0

But I get a Value Error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Is that the correct way to introduce this condition in to the raster calculator? I thought that follows the same logic than the raster calculator used in QGIS, where the value in the ouput we want goes together with the * simbol and the condition goes in parenthesis.

1 Answer 1


I believe gdal_calc.py doesn't cope well with combined relations such as (3<=A>=7). It might be better to split it up:

--calc="B*logical_and(3 <= A, A >= 7)"
  • AND is not a thing. You mean logical_and(3 <= A, A >= 7)
    – Mike T
    Aug 18, 2021 at 0:16

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.