I am trying to make a script, to run at the toolbox, that receives a raster file and has an identical output to it, following the idea from here.

I couldn't find a complete script that works unchanged so I can understand how to manipulate rasters with PyQGIS 3. The most complete I found was the answer from root676 here, but I couldn't execute the script.

So I would like guidance on how to set up a simple script for input and output of raster files, without using native QGIS or GDAL tools, to not pre-define the output file name.


When I run the script mentioned, I get:

The following layers were not correctly generated.

  • C:/Users/cs279069/AppData/Local/Temp/processing_26353d6a350741f99b38886692ba0306/35605f7cacd647be810cbfc73ee98186/OUTPUT_RASTER_A.tif
  • C:/Users/cs279069/AppData/Local/Temp/processing_26353d6a350741f99b38886692ba0306/d89846f8fbb8400da1e7891ff94270f0/OUTPUT_RASTER_B.tif
You can check the 'Log Messages Panel' in QGIS main window to find more information about the execution of the algorithm.

The Log Messages Panel is empty. Probably because there is no calculation on it.

Clarifying the question:

The input raster can be any raster of any size, because I'll use this in several different scripts, just to rename the output file. When I create some script, I use the native tools of QGIS, and that generaty an output with the tool predefined name. Like "Reprojected", "Clipped (mask)".

So basically I need something like this: OUTPUT_RASTER = INPUT_RASTER * 1

So the output will be identical from the input and will have the display name that was defined in the def initAlgorithm section.

More info:

I´m trying to run this code.

def initAlgorithm(self, config=None):
        self.tr("Input Raster"), None, False))

        self.tr("Output Raster"),
        None, False))

def processAlgorithm(self, parameters, context, feedback):
    input_raster = self.parameterAsRasterLayer(parameters, self.INPUT_RASTER, context)
    output_raster = self.parameterAsOutputLayer(parameters, self.OUTPUT_RASTER, context)

    output_raster = input_raster

    results = {}
    results[self.OUTPUT_RASTER] = output_raster
    return results

That´s the error given:

The following layers were not correctly generated.

  • C:/Users/cs279069/AppData/Local/Temp/processing_779dd8aa0c284112b878829e6b56c348/4bacded9efb04c6c8c9502e6572f5c1d/OUTPUT_RASTER.tif
You can check the 'Log Messages Panel' in QGIS main window to find more information about the execution of the algorithm.

  • Did you receive errors when you tried to run the script you linked to? What do you want your 'simple script' to do? Just make a copy of the input raster?
    – Joseph
    Commented Oct 10, 2019 at 9:35
  • @Joseph Question edited to bring more details, thanks for the attention. Commented Oct 10, 2019 at 10:30
  • For any question that seeks code please include a code snippet that illustrates what you have tried and where you are stuck as formatted text within your question.
    – PolyGeo
    Commented Oct 11, 2019 at 7:27

1 Answer 1


I found a solution that doesn't seem right, but it works. enter image description here

I created a model that has only one gdal algorithm. I created a second model that has only the first model. So I converted the second model to script, and got the result below, where the output file respects the name assigned in the first model, in this case "raster out".

    class Raster(QgsProcessingAlgorithm):

    def initAlgorithm(self, config=None):
        self.addParameter(QgsProcessingParameterRasterLayer('raster', 'raster', defaultValue=None))
        self.addParameter(QgsProcessingParameterRasterDestination('Out2', 'out2', createByDefault=True, defaultValue=None))

    def processAlgorithm(self, parameters, context, model_feedback):
        feedback = QgsProcessingMultiStepFeedback(1, model_feedback)
        results = {}
        outputs = {}

        # raster inout
        alg_params = {
            'rasterin': parameters['raster'],
            'gdal:cliprasterbyextent_1:raster out': parameters['Out2']
        outputs['RasterInout'] = processing.run('model:raster inout', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
        results['Out2'] = outputs['RasterInout']['gdal:cliprasterbyextent_1:raster out']
        return results

It is necessary that the first model exists in QGIS, while the second does not need to be saved, only the generated script.

To keep the output raster equal to the input raster, in the clip raster by extension, the extension comes from the input raster itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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