[GIS] Merge two rasters with no data value

gdalgrassnodataqgisraster

I have two rasters: Raster A and B. Raster A has has only values of 0. The white area in Raster B are No Data values. I want to merge them to one raster (Raster C). So basically I want all pixels in Raster B with a No Data value to have a value of 0.

I tried to do this with the raster calculator in QGIS by adding Raster A to Raster B, but it only returns again Raster B.

I am looking for solutions with QGIS, GDAL, or Grass GIS.

enter image description here

Best Answer

One solution may be this batch script:

set nodatavalue=-99999
gdalwarp -srcnodata None rasterB_with_NoData.tif rasterB_without_NoData.tif
python gdal_calc.py -A rasterB_without_NoData.tif --outfile=rasterB_updated_with_NoData.tif --calc="(A<>%nodatavalue%)*A"
gdalwarp -srcnodata None rasterB_updated_with_NoData.tif rasterB_updated_without_NoData.tif

The trick is -srcnodata None offered by gdalwarp, which is very useful in order to ignore intrinsic nodata settings on the source dataset (from the doc). In detail, we need to apply it two times: firstly because we need to substitute the NoData value with 0 and so we have to include it in the calculation, secondly because gdal_calc.py introduces NoData value again.

Related Question