1

In my plugin I would like to use the gdal tool "cliprasterbymask" and add the new clipped raster to my project.

According to the Documentation the output has the type "raster". So it returns a raster object.

Thus, I have tried the following:

 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})

 self.iface.addRasterLayer(clip)

Unfortunately, I get the following error message which I don't know what it means.

TypeError: QgisInterface.addRasterLayer(): arguments did not match any overloaded call: overload 1: argument 1 has unexpected type 'dict' overload 2: argument 1 has unexpected type 'dict'

How do I get the outputraster from cliprasterbymasklayer and what is actually output?

Apparently, its not just a simple raster layer.

  • Please always provide errors as formatted text rather than pictures. – PolyGeo Dec 2 '19 at 20:07
  • 3
    You have to access the OUTPUT element of the dictionary returned by the processing algorithm. Assuming your outFile is a file path, the following should work: self.iface.addRasterLayer(clip['OUTPUT'], 'Clipped_raster') – Ben W Dec 3 '19 at 3:14
1

The result of processing.run is always a Python dictionnary. From QGIS documentation (end of the paragraph):

The run method returns a dictionary with one or more output names (the ones shown in the algorithm description) as keys and the file paths of those outputs as values

For most processing algorithm, it contains only an 'OUTPUT' key.

When you call

clip = processing.run('gdal:cliprasterbymasklayer', {
                        'INPUT':my_layer,
                        'MASK': my_mask_layer,
                        'OUTPUT':'C:/Temp/my_output.tif'})

You get a dictionnary:

>>> clip
{'OUTPUT': 'C:/Temp/my_output.tif'}

So you in your case, you can either call

self.iface.addRasterLayer(clip['OUTPUT'])

or

self.iface.addRasterLayer(outFile)
0

I'm curious to know what you "wasserstand" and "gebeude" variables are. Apparently one of these is a dictionary object instead of raster or vector as explained in the documentation. The output should be another raster layer that's clipped by the second parameter.

  • hmmm they are regular .shp and Geotiff files – DGIS Dec 2 '19 at 22:08
  • upon further inspection, I noticed you're using curly braces ({...}) which defines a "dict" in python. However, the documentation has these parameters delimited by comma only – amadbear Dec 2 '19 at 23:08

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.