4

I have the same Question as asked in this post (How to connect input parameter to the field calculator algorithm within the graphical modeler?) and the answer from Joseph already helped a lot:

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

from qgis.core import QgsExpression

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

num = str(Number)
idx = layer.fieldNameIndex(Fields)
e = QgsExpression(num + '* 5')    # Change value to suit your needs
e.prepare(layer.pendingFields())

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

layer.commitChanges()

Anyway, I'm looking for a way to multiply the number that the user enters in the interface with the value of the chosen field. In the script provided in the answer, the field value is ignored and the user value gets multiplied with the number defined in the script (default 5).

I have to admit that I'm not that much of a python programmer, so any suggestion would be very helpful.

10
  • Thanks @Miron, the code you linked to contained a mistake in which all features recorded the same output value (i.e. user input num multipled by 5). I've edited that post =)
    – Joseph
    Feb 29, 2016 at 12:45
  • Huh, I just realised with the edited post in the question you linked to, you could just change ##Number=number 0 to ##Number=Field Layer and the rest of the script can stay exactly the same.
    – Joseph
    Feb 29, 2016 at 13:14
  • Thanks for the quick response! I would like to stick with the case where the user can choose a number to multiply it with the field values. But if I try the modified code from the original post with ##Number=number 0 , the output gives NULL as filed values.
    – Miron
    Feb 29, 2016 at 13:21
  • 1
    Sorry, you are absolutely right, I created a new feature and an integer column and the script works like a charm. I have no idea why it would not work on my original feature witch is as well integer....anyway, thanks a lot for your help, really appreciated!!! cheers, Miron
    – Miron
    Feb 29, 2016 at 14:03
  • 1
    Just for the record: I figured the reason the script does not work was the the field name contained a "-" in the name (risk-class). If I change the name into "risk_class", the script works fine ;) very strange indeed
    – Miron
    Feb 29, 2016 at 14:11

1 Answer 1

3

You could try using the following script which multiples one field by the other. The output is saved in whatever field you selected for Fields:

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

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()

Your Answer

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

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