1

In my PyQGIS standalone script I want to use the SAGA Tool "Clip raster by mask layer". I have tried to following.

def clipRaster(self):
    raster = self.getRasterLayer()
    vector = self.getVectorLayer()
    outDir = os.path.join(QgsProject().instance().homePath(), "clip.tiff")
    param = {"input": raster,
             "polygons": vector,
             "output": outDir,
             }
    clip = processing.run('saga:cliprasterwithpolygon', param)
    rlayer = QgsRasterLayer(clip['output'], 'clip')
    QgsProject().instance().addMapLayer(rlayer)

Unfortunately, I get the following error message:

_core.QgsProcessingException: Unable to execute algorithm Could not load source layer for INPUT: no value specified for parameter Traceback (most recent call last): File "C:/Users/Jan/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\gebeude_betroffenheit\Gebeude_Betroffenheit.py", line 292, in run self.clipRaster() File "C:/Users/Jan/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\gebeude_betroffenheit\Gebeude_Betroffenheit.py", line 237, in clipRaster clip = processing.run('saga:cliprasterwithpolygon', param) File "C:/PROGRA~1/QGIS3~1.4/apps/qgis-ltr/./python/plugins\processing\tools\general.py", line 105, in run return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context) File "C:/PROGRA~1/QGIS3~1.4/apps/qgis-ltr/./python/plugins\processing\core\Processing.py", line 139, in runAlgorithm raise QgsProcessingException(msg) _core.QgsProcessingException: Unable to execute algorithm Could not load source layer for INPUT: no value specified for Parameter

Hmm it indicates that there is something wrong with the raster layer but the same layer works perfectly fine for another function. Thus, I believe there is something wrong with the "param" variable...

Does anyone see the problem in my code?

I'm using QGIS 3.4

1

Ok I found the problem. In my outDir, I ordered to save the new raster as .tiff. However SAGA tools save all rasters in .sdat format.

After I changed the name from "clip.tiff" to "clip.sdat" the error message disappeared.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.