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.5k29 gold badges115 silver badges350 bronze badges
asked Sep 2, 2023 at 14:37
2
  • see this answer gis.stackexchange.com/a/385009/4449 Commented Sep 2, 2023 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". Commented Sep 3, 2023 at 16:14

1 Answer 1

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
answered Sep 5, 2023 at 8:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.