One option to achieve this is using gdal_merge.py. But unfortunately, this tool stops my automated script with a "Python.exe has stopped working" error, as others complained here, here and here.
Another option, which actually works for me, is using SAGA's tool Mosaic raster layers, which doesn't generate any errors:
#prepare QGIS environment
import sys
from qgis.core import *
from PyQt4.QtGui import *
app = QApplication([])
QgsApplication.setPrefixPath("E:\\QGIS\\apps\\qgis", True)
QgsApplication.initQgis()
#prepare processing framework
sys.path.append('E:\\QGIS\\apps\\qgis\\python\\plugins')
from processing.core.Processing import Processing
Processing.initialize()
import processing
from processing.tools import *
#set variables
algorithm = "saga:mosaickrasterlayers"
inputMain = path + "Flood_main.tif;"
input2D = path + "Flood_cvasi2D.tif"
addInput = None
dataType = 7
interpolation = 0
overlapAreas = 1
blendDistance = 10
match = 0
fit = 0
extent = "612550.33,660022.33,679329.28,703773.28"
result = path + "Flood.tif"
#run algorithm
processing.runalg(algorithm,inputMain + input2D, addInput, dataType, interpolation,
overlapAreas, blendDistance, match, fit, extent, result)
#exit QGIS
QgsApplication.exitQgis()
QApplication.exit()
QGIS version: 2.14.3-Essen
gdal_mergecan take into account NoData values with the-noption for the input and-a_nodatafor the output (official doc). – ArMoraer Sep 06 '16 at 09:12gdal: Mergefrom the QGIS GUI and it didn't have the option for NoData. But I've usedgdal_mergeand it worked perfectly. Now I don't know if I should leave the question here or just delete it. – Ralu Bur Sep 06 '16 at 09:29r.mapcalculatoris still welcome, becausegdal_mergestops my automated script with a "Python.exe has stopped working" error, as seen here, here and here. – Ralu Bur Sep 07 '16 at 12:30