4

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.

basic example Model using my script

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

6
  • 1
    Why do you need to edit in place and not create a new layer? Other question, why don't you use native algorithm for doing this?
    – etrimaille
    Commented May 31, 2019 at 6:59
  • 1
    Answer to both questions: because I don't know how! I failed to find a native algorithm which I could use in the Modeler. No particular reason why I have edited in place, just that it's the first solution I found, and creating a new layer seems an unnecessary complexity. But if that is the Correct way to do it, I will pursue that path
    – TimD
    Commented May 31, 2019 at 7:35
  • 1
    Can you clarify a little more? You are able to change the name of the input_field, with the new_name. What output are you expecting? The modified layer name is your output, right? Commented Jun 6, 2019 at 14:08
  • 2
    I require an Output which can be piped into the next step in the Model, when using the Graphical Modeler. I have updated the question with some further detail which should clarify further.
    – TimD
    Commented Jun 7, 2019 at 4:02
  • 1
    We've faced the same trouble in the company I work for. Now we are funding a fix or a proper support in the modeler for algorithms that modify the input layer. I'll let you know of any news. Commented Jun 7, 2019 at 4:47

1 Answer 1

0

I think you have done the script properly. That means, there are inputs and there is also an output (that's why you can chain your script in the modeler).

I guess you are only missing one detail. In the last algorithm (Basic statistics for fields), you need to give the output a name when configuring it in the modeler.

enter image description here

Once you do so, you'll see a green box representing the result.

enter image description here

Running the model now allows you to have the results parameter, which is actually the location where you want to store the statistics. You can leave it empty and run the model with your other parameters entered. You'll get something like this:

enter image description here

Which gives you a temporal location for the statistics.

enter image description here


For reference, here are the files I've used:

3
  • Looking at your script, it is practically identical to my script (posted in my question), except your last line is return {self.OUTPUT: source}. I think this may be the key difference. I'm only able to use QGIS 3.4 at the moment, and it doesn't seem to want to import your Model. I will try again when I can use my own machine with QGIS 3.8
    – TimD
    Commented Sep 18, 2019 at 23:26
  • OK have tested again, with strange results - the Rename Field Script only works the first time, for any given column. After that, each time you run it, it appears to run successfully but fails to actually change the column name. In any case, I believe that the return line identified above is the solution to the original query.
    – TimD
    Commented Sep 19, 2019 at 10:10
  • Strange. I used it consecutively several times. However, the files are meant to be a reference for you to adjust your script and make it run as you initially intended. If you do so, let me know if something doesn't work. Commented Sep 19, 2019 at 13:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.