I'm working on a plugin for QGIS3, and trying to clip a raster layer using a vector mask layer. I've been able to successfully do this using "Clip raster by mask layer" through the processing toolbox, but when I use the following code in PyQGIS, it does not generate any output. I tried saving the output to 'memory:', to 'TEMPORARY_OUTPUT' and to a specific path in the disk, however, same result for all. Any tips?
from qgis.core import *
import processing
#load layers
project = QgsProject.instance()
mask = project.mapLayersByName('vector')[0]
input = project.mapLayersByName('raster')[0]
parameters = {
'ALPHA_BAND': False,
'CROP_TO_CUTLINE': True,
'DATA_TYPE': 0,
'INPUT': input,
'KEEP_RESOLUTION': True,
'MASK': mask,
'MULTITHREADING': False,
'OUTPUT': 'memory:',
'SOURCE_CRS': dem.crs(),
'TARGET_CRS': dem.crs(),
'NO_DATA': -9999
}
clip_output = processing.run("gdal:cliprasterbymasklayer", parameters)
result = QgsRasterLayer(clip_output['OUTPUT'], "clipped", 'gdal')
project.addMapLayer(result)