0

If I execute the following example PyQGIS script, the temporary output layer is called "Remaining fields" in the layers panel which is the standard output layer name of the last executed processing algorithm "native:deletecolumn". But I want the output layer of my script to be named "Example output" as defined in the initAlgorithm. How can I change the name of the output layer? Or is there a better way to pass the output of the algorithm to the script's output "ExampleOutput"?

from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterVectorLayer
from qgis.core import QgsProcessingParameterFeatureSink
import processing

class Mymodel(QgsProcessingAlgorithm):

def initAlgorithm(self, config=None):
    self.addParameter(QgsProcessingParameterVectorLayer('example_input', 'Example input', types=[QgsProcessing.TypeVectorPoint], defaultValue=None))
    self.addParameter(QgsProcessingParameterFeatureSink('ExampleOutput', 'Example output', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, supportsAppend=True, defaultValue=None))

def processAlgorithm(self, parameters, context, model_feedback):
    # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
    # overall progress through the model
    feedback = QgsProcessingMultiStepFeedback(2, model_feedback)
    results = {}
    outputs = {}

    # Add X/Y fields to layer
    alg_params = {
        'CRS': parameters['example_input'],
        'INPUT': parameters['example_input'],
        'PREFIX': '',
        'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
    }
    outputs['AddXyFieldsToLayer'] = processing.run('native:addxyfields', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

    feedback.setCurrentStep(1)
    if feedback.isCanceled():
        return {}

    # Drop field(s)
    alg_params = {
        'COLUMN': ['x'],
        'INPUT': outputs['AddXyFieldsToLayer']['OUTPUT'],
        'OUTPUT': parameters['ExampleOutput']
    }
    outputs['DropFields'] = processing.run('native:deletecolumn', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
    results['ExampleOutput'] = outputs['DropFields']['OUTPUT']
    return results

def name(self):
    return 'mymodel'

def displayName(self):
    return 'mymodel'

def group(self):
    return 'mygroup'

def groupId(self):
    return 'mygroup'

def createInstance(self):
    return Mymodel()

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
zfn
  • 35
  • 3
  • see this answer https://gis.stackexchange.com/a/385009/4449 – Llaves Sep 02 '23 at 16:58
  • @Llaves Thank you for the suggestion, however, I just want to know how to use the standard output name under self.addParameter(QgsProcessingParameterFeatureSink()). I don't know why, but the layer is exported with the output name of the last used algorithm "native:deletecolumn". – zfn Sep 03 '23 at 16:14

1 Answers1

0

I finally found a solution in this issue in the official QGIS repository. Just end the processAlgorithm block with

results['ExampleOutput'] = outputs['DropFields']['OUTPUT']
context.layerToLoadOnCompletionDetails(results['ExampleOutput']).name = "Example output" # Insert your custom layer name here!
return results
zfn
  • 35
  • 3