1

For my QGIS plugin, I'm trying to use the GRASS Tool r.neighbor. I wrote the following code which should run the tool and return the result as a raster object.

    def focalStatistics(self):
    raster = self.merge()
    outDir = os.path.join(QgsProject().instance().homePath(), "Extrapolation.tiff")
    param = {"input": raster,
             "selection": None,
             "output": outDir,
             "method": 'maximum',
             "size": 3,
             "title": 'extrapoliert',
             "weight": None,
             "gauss": None,
             "quantile": None
             }
    focalstat = processing.run('grass7:r.neighbors', param)
    rlayer = QgsRasterLayer(focalstat['output'])
    QgsProject().instance().addMapLayer(rlayer)
    return rlayer

The needed parameters I looked up from here

However, I get the following error message:

Cannot run algorithm wrong parameters for method

I don't see the problem, the parametrs should be correct. Can someone explain the error to me

2

First of all, I don't know if it's a typing error, but your indentation is wrong. I think your problem comes from the definition of the method.

The following code works, I have:

  • removed self everywhere
  • indicated the path of my raster directly
  • in the parameters I replaced the maximum by the corresponding index
  • replaced processing.run by processing.runAndLoadResults: the result is automatically added to the project
def focalStatistics():
    raster = "C:/Users/v.bre/Desktop/test/grass_raster/MNT_SICOVAL_20m_cc43.tif"
    outDir = "C:/Users/v.bre/Desktop/test/grass_raster/MNT_SICOVAL_20m_cc43_test.tif"
    param = {"input": raster,
             "selection": None,
             "output": outDir,
             "method": 4,
             "size": 3,
             "title": 'extrapoliert',
             "weight": None,
             "gauss": None,
             "quantile": None
             }
    focalstat = processing.runAndLoadResults('grass7:r.neighbors', param)

focalStatistics()

1
  • in the original code of my editor, the indentation was correct. What solved the problem finally was to remove the 'maximum' with the indexnumber, which is weird because according to the documentation "method" expects a string and not integer. But ok it works now. Thank you – DGIS Dec 16 '19 at 13:54

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.