I am new to writing Python tools in QGIS so maybe I am missing something here. I am trying to create a simple processing tool that adds two raster layers together with cell statistics. Here is my working code...
from qgis.core import (QgsProcessingAlgorithm,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterRasterDestination)
from qgis import processing
class Crosstab(QgsProcessingAlgorithm):
INPUT_RASTER1 = 'INPUT_RASTER1'
INPUT_RASTER2 = 'INPUT_RASTER2'
OUTPUT_RASTER = 'OUTPUT_RASTER'
def __init__(self):
super().__init__()
def name(self):
return "simple_crosstab"
def displayName(self):
return "Simple Crosstab Tool"
def createInstance(self):
return type(self)()
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterRasterLayer(
self.INPUT_RASTER1, "Map A"))
self.addParameter(QgsProcessingParameterRasterLayer(
self.INPUT_RASTER2, "Map B"))
self.addParameter(QgsProcessingParameterRasterDestination(
self.OUTPUT_RASTER, "Crosstab Raster"))
def processAlgorithm(self, parameters, context, feedback):
# Get the input parameter value
input_raster1 = self.parameterAsRasterLayer(parameters, self.INPUT_RASTER1, context)
input_raster2 = self.parameterAsRasterLayer(parameters, self.INPUT_RASTER2, context)
# Check if the input rasters are valid
if not input_raster1 or not input_raster2:
raise QgsProcessingException('Two input rasters are required for the operation')
# run cell statistics to calculate the sum
alg_params = {
'GRIDS': [input_raster1, input_raster2],
'STATISTICS': [0], # 0 for Sum
'RESULT': parameters[self.OUTPUT_RASTER]
}
processing.run('native:cellstatistics', alg_params, context=context, feedback=feedback)
# Return the result (if applicable)
return {}
This is what the tool looks like in QGIS...
When I go to run this tool, I receive the following error for both of the input raster layers:
Incorrect parameter value for INPUT.
Clearly, this is some sort of issue with how the input parameters are defined, but I thought I was using the right function QgsProcessingParameterRasterLayer
to read the raster layers into the tool.
What am I doing wrong here?
-
Not sure if this is the answer, but it might help get you there: lyr = QgsRasterLayer(lyr_path, lyr_name); lyr_out = QgsProject.instance().addMapLayer(lyr)David Galt– David Galt2023年07月31日 22:26:18 +00:00Commented Jul 31, 2023 at 22:26
1 Answer 1
It's not how you call the parameters, it's how you call the function that is the problem here. Like how you specifically define that the parameters in your algorithm are called i.e. INPUT_RASTER1
, so do all the other algorithms. But you're using different names, and so the algorithm simply cannot find the input.
The correct alg_params
for this specific function is this:
alg_params = {
'INPUT': [input_raster1, input_raster2],
'STATISTICS': 0, # 0 for Sum # <-- It's not a list! Just an int value!
'OUTPUT': parameters[self.OUTPUT_RASTER]#,
# 'REFERENCE_LAYER': ???? # <-- You need to add this parameter too!
}
For future reference - The easiest way to find out what the parameters are is to check in QGIS:
- Open the tool there, enter some parameter values (don't need to be sensible, just valid), let the algorithm actually run to double-check that you got everything.
- Either hover over each input field separately and see what the Python Identifier is, or
- Go on "Advanced" (bottom left), and then "Copy Python command".
- Paste it into your algorithm. Of course, this will contain fixed values - replace them with your variables (
input_raster1
, etc.).
You don't need to have all the parameters in your dictionary, but you do need all the mandatory ones, and of course any you wish to change. For example, when copying the Python command for cell statistics there will also be 'OUTPUT_NODATA_VALUES'
. This parameter has a default value and can just be skipped in Python, or you can include it with its fixed value after all - doesn't make a difference in this case. 'REFERENCE_LAYER'
, on the other hand, has no default and so must be included in the dictionary.
Explore related questions
See similar questions with these tags.