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