I have written a simple Processing script to rename a given field. It works successfully, changing the input layer's field name. However, I have not been able to produce an Output. This is a problem because I would like to use this script as an interim step in a Graphical Modeler tool, which requires an Output to pass on to the next step.
def processAlgorithm(self, parameters, context, feedback):
source = self.parameterAsVectorLayer(
parameters,
self.INPUT,
context
)
input_field = self.parameterAsString(
parameters,
self.INPUT_FIELD,
context)
new_name = self.parameterAsString(
parameters,
self.NEW_FIELD_NAME,
context)
if source is None:
raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
# Rename the field
source.startEditing()
source.renameAttribute(source.fields().indexFromName(input_field), new_name)
source.commitChanges()
return {'MODIFIED_LAYER': source}
The last line does appear to return a Layer reference:
Execution completed in 0.07 seconds
Results:
{'MODIFIED_LAYER': <qgis._core.QgsVectorLayer object at 0x0000022D82850E58>}
However, when I use the script in a Model, it allows me to pipe the output into another algorithm, but at runtime, it does not work.
Input Parameters: # for 'Basic statistics for fields'
{ FIELD_NAME: 'placetype', INPUT_LAYER: None }
I am not sure what approach I should use to produce output which can then be used in the Modeler