1

At the end of my processing model, it runs an "Add geometry attributes" operation. I want the resulting layer to be named "Overlap" instead of the default "Added geom info." How should I insert the renaming process in the same script?

This is the start of the model

def processAlgorithm(self, parameters, context, feedback):
        feedback = QgsProcessingMultiStepFeedback(4, feedback)
        results = {}
        outputs = {}

This is the end of the model

alg_params = {
            'CALC_METHOD': 0,
            'INPUT': outputs['DissolveOverlap']['OUTPUT'],
            'OUTPUT': parameters['MeasuredOverlap']
        }
        results['MeasuredOverlap'] = processing.run('qgis:exportaddgeometrycolumns', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
        return results
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
BallpenMan
  • 1,217
  • 8
  • 19

2 Answers2

1

@Llaves provided the solution. Anyway, add this class first

class Renamer (QgsProcessingLayerPostProcessorInterface):
    def __init__(self, layer_name):
        self.name = layer_name
        super().__init__()
def postProcessLayer(self, layer, context, feedback):
    layer.setName(self.name)

Then add this before you return 'results'

global renamer
renamer = Renamer('test')
context.layerToLoadOnCompletionDetails(results['MeasuredOverlap']['OUTPUT']).setPostProcessor(renamer)
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
BallpenMan
  • 1,217
  • 8
  • 19
0

Simplest and shortest solution is by lejedi76 in an answer here:

layer_id = results['MeasuredOverlap']['OUTPUT']
layer_details = context.layerToLoadOnCompletionDetails(layer_id)
layer_details.name = "My name is Overlap"

To be used just before returning results.

Andre Geo
  • 538
  • 2
  • 11