Geographic Information Systems Asked by fuzzysolutions on January 19, 2021
I’M writing an own QgsProcessing Algorithm and I want to give the output layer in the registry a custom name instead of the default "Output layer". I want to do that during processAlgorithm()
, because I’d like to name the output after an input layer, adding a fixed name prefix.
I tried the setOutputLayerName()
from the class QgsProcessingContext::LayerDetails
, but it doesn’t change the name of the output. The example below is the "template" that the QGIS processing toolbox suggests for algorithm writing. Note the last third last and second last rows of the code, where I tried out my approach.
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessing,
QgsVectorLayer,
QgsFeatureSink,
QgsProcessingException,
QgsProcessingAlgorithm,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink)
from qgis import processing
class ExampleProcessingAlgorithm(QgsProcessingAlgorithm):
INPUT = 'INPUT'
OUTPUT = 'OUTPUT'
def tr(self, string):
return QCoreApplication.translate('Processing', string)
def createInstance(self):
return ExampleProcessingAlgorithm()
def name(self):
return 'myscript'
def displayName(self):
return self.tr('My Script')
def group(self):
return self.tr('Example scripts')
def groupId(self):
return 'examplescripts'
def shortHelpString(self):
return self.tr("Example algorithm short description")
def initAlgorithm(self, config=None):
self.addParameter(
QgsProcessingParameterFeatureSource(
self.INPUT,
self.tr('Input layer'),
[QgsProcessing.TypeVectorAnyGeometry]
)
)
self.addParameter(
QgsProcessingParameterFeatureSink(
self.OUTPUT,
self.tr('Output layer')
)
)
def processAlgorithm(self, parameters, context, feedback):
source = self.parameterAsSource(
parameters,
self.INPUT,
context
)
if source is None:
raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
(sink, dest_id) = self.parameterAsSink(
parameters,
self.OUTPUT,
context,
source.fields(),
source.wkbType(),
source.sourceCrs()
)
# Send some information to the user
feedback.pushInfo('CRS is {}'.format(source.sourceCrs().authid()))
if sink is None:
raise QgsProcessingException(self.invalidSinkError(parameters, self.OUTPUT))
total = 100.0 / source.featureCount() if source.featureCount() else 0
features = source.getFeatures()
for current, feature in enumerate(features):
# Stop the algorithm if cancel button has been clicked
if feedback.isCanceled():
break
# Add a feature in the sink
sink.addFeature(feature, QgsFeatureSink.FastInsert)
# Update the progress bar
feedback.setProgress(int(current * total))
if False:
buffered_layer = processing.run("native:buffer", {
'INPUT': dest_id,
'DISTANCE': 1.5,
'SEGMENTS': 5,
'END_CAP_STYLE': 0,
'JOIN_STYLE': 0,
'MITER_LIMIT': 2,
'DISSOLVE': False,
'OUTPUT': 'memory:'
}, context=context, feedback=feedback)['OUTPUT']
layer = QgsVectorLayer(baseName="My Output") # Create an empty layer for output naming
context.layerToLoadOnCompletionDetails(dest_id).setOutputLayerName(layer) # Add this name to the output context
return {self.OUTPUT: dest_id}
The easiest way to change the output layer name in your script would be to edit the output parameter and change 'Output layer' to whatever you want e.g.
self.addParameter(
QgsProcessingParameterFeatureSink(
self.OUTPUT,
self.tr('My output')
)
)
Answered by Ben W on January 19, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP