4

I am building a model in the graphical modeler of QGIS 2.8.1 Wien and I would like to give the option to the user to enter a number in the model with which a selected field will be multiplied, but I cannot figure out how to connect the parameter for the user input to the field calculator algorithm.

Any ideas on this?

  • As I can see there this question already exists here but for older version: gis.stackexchange.com/questions/72569/… – Eleftheria Oct 2 '15 at 7:32
  • 1
    I also have trouble getting it to work through the modeler. Have you considered converting your model to a Python script? You would still get the same interface when running the script as you would with the modeler but it allows you to edit code such as including user input. Hopefully others can advise on how to get this to work through the modeler :) – Joseph Oct 2 '15 at 12:43
  • Thank you for your answer. I have considered converting the model toy python script but Toolbox does not give me the option by rigth clicking on the model to save it as .py – Eleftheria Oct 2 '15 at 19:34
  • 1
    An early version had that option. You would have to downgrade the Processing plugin to version to 2.2.0-2. Download and extract the folder to (if you're on Windows) C:\Users\You\.qgis2\python\plugins. Note that there might be algorithms not be available in the old version compared to the newer ones. I suggest you back up your C:\Users\You\.qgis2\processing folder as this contains models/scripts in case something happens. – Joseph Oct 5 '15 at 9:43
5

Instead of using the Field Calculator tool, a possible workaround is to create a custom script which allows the user to choose an attribute field and update it with a value they enter.

To do this, go to Processing Toolbox > Scripts > Tools > Create new script and copy the following:

##Update field by number=name
##Layer=vector
##Fields=Field Layer
##Number=number 0

from qgis.core import QgsExpression

layer = processing.getObject(Layer)  
layer.startEditing()

idx = layer.fieldNameIndex(Fields)
n = str(Number)
i = str(Fields)
e = QgsExpression(n + '*' + i)
e.prepare(layer.pendingFields())

for f in layer.getFeatures():
    f[idx] = e.evaluate(f)
    layer.updateFeature(f)

layer.commitChanges()

Save the script into C:/Users/You/.qgis2/processing/scripts. You will need 3 parameters:

  • Layer - Vector layer
  • Field - Table field
  • Number - Number

Then add the script from the list of algorithms to your model. Use the Parent algorithms option if you want this script to run after a specific tool (i.e. at the end of the model).:

Model


Did a simple test where I want to update the id field by entering 5 in the Number parameter. Here is the attribute table initially:

Original attributes

Setting and running the model:

Setting model

Result:

Result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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