I exported a model as script using the properly option in QGIS 3.8. The Script works perfectly, but the output name is changed.
Heres a print of the model:
I want that the script ́s output name, from the Processing Toolbox, be the one that I designed in the def initAlgorithm section, and not the given default name.
In the model, the output name comes as "output_name" and in the script, the output name comes as "Reprojected".
Here's the converted Script:
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
from qgis.core import QgsCoordinateReferenceSystem
import processing
class Rename(QgsProcessingAlgorithm):
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterVectorLayer('shapeinput', 'ShapeInput', types=[QgsProcessing.TypeVector], defaultValue=None))
self.addParameter(QgsProcessingParameterFeatureSink('Output_name', 'output_name', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=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(1, model_feedback)
results = {}
outputs = {}
# Reproject Layer
alg_params = {
'INPUT': parameters['shapeinput'],
'TARGET_CRS': QgsCoordinateReferenceSystem('EPSG:4326'),
'OUTPUT': parameters['Output_name']
}
outputs['ReprojectLayer'] = processing.run('native:reprojectlayer', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
results['Output_name'] = outputs['ReprojectLayer']['OUTPUT']
return results
def name(self):
return 'rename'
def displayName(self):
return 'rename'
def group(self):
return 'rename'
def groupId(self):
return ''
def createInstance(self):
return Rename()
-
This was also asked before: How to rename the result of a QGIS Processing AlgorithmJoseph– Joseph2019年09月05日 15:17:16 +00:00Commented Sep 5, 2019 at 15:17
-
I have got exactly the same problem, any answers yet?Rob– Rob2020年04月14日 13:19:36 +00:00Commented Apr 14, 2020 at 13:19
-
Unfortunately not, I recently realized that the shape created has the properties @layer_name. This is the element that needs to be changed, but I haven't found how to change it by python.Herbert Santos– Herbert Santos2020年04月30日 13:38:20 +00:00Commented Apr 30, 2020 at 13:38
3 Answers 3
An alternative solution that worked for me, as parameters['Output_name'] is type QgsProcessingOutputLayerDefinition with the attribute destinationName, add this line before calling the processing algorithm:
parameters['Output_name'].destinationName = 'your_preferred_name'
-
Hello @Andrson, I came across this solution a few years ago, but I forgot to update it here. It's definitely the best wayHerbert Santos– Herbert Santos2024年03月21日 14:52:03 +00:00Commented Mar 21, 2024 at 14:52
If you need change the name of output, you can do it with this:
alg_params = {
'INPUT': parameters['shapeinput'],
'TARGET_CRS': QgsCoordinateReferenceSystem('EPSG:4326'),
'OUTPUT': 'memory:Name_layer'
}
or if you want to save, only do this:
path = '/home/shade/Desktop/name_layer.shp' #or something like that
alg_params = {
'INPUT': parameters['shapeinput'],
'TARGET_CRS': QgsCoordinateReferenceSystem('EPSG:4326'),
'OUTPUT': path
}
For example:
I run the next code:
layer = iface.activeLayer()
alg_params = {
'INPUT': layer,
'TARGET_CRS': QgsCoordinateReferenceSystem('EPSG:4326'),
'OUTPUT': 'memory:Name_layer'
}
result = processing.run('native:reprojectlayer', alg_params)
print ('This is the result: {}'.format(result['OUTPUT']))
print ('This is the name of the layer result: {}'.format(result['OUTPUT'].name()))
And obtained this:
-
The first option generated no result. But processed OKHerbert Santos– Herbert Santos2019年09月05日 14:39:25 +00:00Commented Sep 5, 2019 at 14:39
-
In this case results['Output_name'] saved the qgsvectorlayer, if you need the name, use results['Output_name'].name()Jhon Galindo– Jhon Galindo2019年09月05日 14:46:33 +00:00Commented Sep 5, 2019 at 14:46
-
1@JhonGalindo - I think the OP means that if you run the script from the Processing Toolbox, the output name is given the default name. Your code works from the Python Console, now test if it works by converting it into a script which you run from the toolbox.Joseph– Joseph2019年09月06日 09:54:45 +00:00Commented Sep 6, 2019 at 9:54
-
1Exactly! That is what I mean. Results from the toolbox, not from the console.Herbert Santos– Herbert Santos2019年09月06日 11:58:22 +00:00Commented Sep 6, 2019 at 11:58
-
1thanks @Joseph , I will do it and I will try to show the answer here.Jhon Galindo– Jhon Galindo2019年09月06日 15:52:00 +00:00Commented Sep 6, 2019 at 15:52
I got it!
the last step of the script must be executed inside a variable, here called step1
so it respects the name entered in the output parameter through the last block
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
from qgis.core import QgsFeatureSink
import processing
class Model(QgsProcessingAlgorithm):
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterVectorLayer('vetor', 'vetor', defaultValue=None))
self.addParameter(QgsProcessingParameterFeatureSink('exit', 'exit name', type=QgsProcessing.TypeVectorPolygon, createByDefault=True, defaultValue=None))
def processAlgorithm(self, parameters, context, model_feedback):
feedback = QgsProcessingMultiStepFeedback(1, model_feedback)
results = {}
outputs = {}
step1 = processing.run("native:buffer", {
'DISSOLVE': False,
'DISTANCE': 10,
'END_CAP_STYLE': 0,
'INPUT': parameters['vetor'],
'JOIN_STYLE': 0,
'MITER_LIMIT': 2,
'SEGMENTS': 5,
'OUTPUT': 'memory:'
}, context=context, feedback=feedback)['OUTPUT']
"""here the output name is changed"""
source = step1
(sink, dest_id) = self.parameterAsSink(parameters,'exit',context,source.fields(),source.wkbType(),source.sourceCrs())
features = source.getFeatures()
for current, feature in enumerate(features):
sink.addFeature(feature, QgsFeatureSink.FastInsert)
return results
def name(self):
return 'rename'
def displayName(self):
return 'rename'
def group(self):
return ''
def groupId(self):
return ''
def createInstance(self):
return Model()
-
@Rob here´s the solutionHerbert Santos– Herbert Santos2020年11月17日 19:16:41 +00:00Commented Nov 17, 2020 at 19:16