I have a recurrent problem with gdal_calc.

Say I have two rasters I need to sum together, in which the same nodatavalue is set (e.g. --NoDataValue = 0).

In order to account for the fact that nodata + data = nodata, my formula would ideally look like this:


Unfortunately, the "real" nodatavalue (i.e. zero) is not recognized (so anything like --calc="where(A==0)... would NOT work), nor I find any other way to refer to it.

Any ideas that DO NOT include the prior use of gdal_edit -unsetnodata?

  • What happens if you replace nodatavalue with 0/0?
    – mgri
    Apr 27, 2017 at 10:14
  • If you mean: --calc="where(A==0/0,0,A)+where(B==0/0,0,B)" the result is ZeroDivisionError: integer division or modulo by zero
    – lobelius
    Apr 27, 2017 at 10:21
  • Yes, assuming that the expression proposed by you works using another value for A and B.
    – mgri
    Apr 27, 2017 at 10:23
  • Thanks mgri for your reply, but actually, as I wrote, using 0/0 as nodatavalue in the formula results in an obvious error, so my question is still open.
    – lobelius
    Apr 27, 2017 at 12:39

3 Answers 3


An alternative approach (without altering your input data with gdal_edit) could be using GDAL VRT files as intermediate operands in gdal_calc and -a_nodata none in gdal_translate to skip the NoData values. For instance, if the original NoData value = 0:

gdal_translate -of VRT raster1.tif raster1.vrt -a_nodata none
gdal_translate -of VRT raster2.tif raster2.vrt -a_nodata none
gdal_calc.py -A raster1.vrt -B raster2.vrt --calc="A+B" --outfile=result.tif

In other cases, your calc expression should be applied to filter the NoData value and set it to 0.

  • This works, but it takes my processing time from 3 seconds up to 76 seconds on a grid with about 30M cells, much of which is NoData.
    – David A
    Jun 8, 2018 at 18:03

Starting with GDAL version 3.3, a new hideNoData option has been added to gdal_calc to enable nodata values to be considered in your calc formulas, thus eliminating the need to do other preprocessing steps. I haven't been able to test this yet, but it looks promising!

  • It is not working yet in gdal 3.4.0. Very new and buggy by now.
    – nadya
    Aug 20, 2022 at 0:13
  • Thanks @nadya for the update. Are there any reported bugs/issues on the GDAL Github? github.com/OSGeo/gdal/issues Aug 22, 2022 at 13:20


But for some reason when A and B aren't null, the set (A+B)*2 ...

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.