This Question is a follow up Question to this one I recently asked.
In my plugin I have a function that clips rasters by mask and I would like that the function returns the raster layer object. And then in another function I can get the Object. That's the code so far:
def clipRaster(self):
wasserstand = self.getRasterLayer()
outFile = os.path.join(QgsProject.instance().homePath(), "rtemp.tiff")
gebeude = self.getVectorLayer()
clip = processing.run('gdal:cliprasterbymasklayer',{'INPUT': wasserstand,'MASK': gebeude,
'NODATA': -1, 'ALPHA_BAND': False, 'CROP_TO_CUTLINE': True, 'KEEP_RESOLUTION': True,
'OPTIONS': "", 'DATA_TYPE': 5, 'OUTPUT': outFile})
rlayer = QgsRasterLayer(clip['OUTPUT'])
#rlayer = QgsRasterLayer(path_to_tif, "SRTM layer name")
return rlayer
def getclip(self):
clip = self.clipRaster()
self.iface.addRasterLayer(clip)
#iface.addRasterLayer("/path/to/raster/file.tif", "layer name you like")
Unfortunately, the error message I get is the following:
TypeError QgisInterface.addRasterLayer() arguments did not match any overloaded call overload 1 argument 1 has unexpected type 'QgsRasterLayer' overload 2 argument 1 has unexpected type 'QgsRasterLayer'
Why isn't it working? "outFile" defines the path and name of the raster, the processing algorithm creates a raster layer and puts it into the "outFile" directory. To create a new raster the function QgsRasterLayer() needs a path and layer name which is both in outFile, thus the raster is now saved in the rlayer variable. Finally, iface.addRasterLayer(clip) adds this object. It makes sense doesn't it? :).
Can someone explain my mistake?