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
-
1Why 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– etrimaille2019年05月31日 06:59:30 +00:00Commented May 31, 2019 at 6:59
-
1Answer 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 pathTimD– TimD2019年05月31日 07:35:29 +00:00Commented May 31, 2019 at 7:35
-
1Can 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?Erich Purpur– Erich Purpur2019年06月06日 14:08:49 +00:00Commented Jun 6, 2019 at 14:08
-
2I 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– TimD2019年06月07日 04:02:30 +00:00Commented Jun 7, 2019 at 4:02
-
1We'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.Germán Carrillo– Germán Carrillo2019年06月07日 04:47:42 +00:00Commented Jun 7, 2019 at 4:47
1 Answer 1
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.
Once you do so, you'll see a green box representing the result.
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:
Which gives you a temporal location for the statistics.
For reference, here are the files I've used:
-
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.8TimD– TimD2019年09月18日 23:26:24 +00:00Commented 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– TimD2019年09月19日 10:10:16 +00:00Commented 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.Germán Carrillo– Germán Carrillo2019年09月19日 13:48:18 +00:00Commented Sep 19, 2019 at 13:48
Explore related questions
See similar questions with these tags.