3

In a vector layer table there are two fields F1 and F2 with float type. I want to multiply these fields and insert results to new field F3. In below code new field created but without any value. What is problem?

layer = iface.activelayer()
F1 = self.ui.cBox_Width.currentText()
F2 = self.ui.cBox_Length.currentText()

new_Field = "F3"
provider = layer.dataProvider()
caps = provider.capabilities()

idx = provider.fieldNameIndex(new_Field)
try:
    if idx == -1:
        if caps & QgsVectorDataProvider.AddAttributes:
            res = provider.addAttributes([QgsField(new_Field, QVariant.Double)])
except:
    return False

layer.startEditing()
bufferfield = layer.fieldNameIndex('buffer_km')
formula = QgsExpression("\"F1\" * \"F2\"")
formula.prepare(layer.pendingFields())
for f in layer.getFeatures():
    f[idx] = formula.evaluate(f)
    layer.updateFeature(f)

layer.updateFields()
layer.commitChanges()
0

1 Answer 1

1

Try moving:

layer.updateFields()

before you start editing to ensure all newly created fields are recognised. You also need to replace:

f[idx] = formula.evaluate(f) 

with something like:

new_Field_idx = layer.fieldNameIndex(new_Field)
f[new_Field_idx] = formula.evaluate(f) 

as the old index would be -1 if it wasn't already created. And if you're calling your fields in your expression, you could use:

formula = QgsExpression(str(F1) + '*' + str(F2))

So you could try using something like the following:

layer = iface.activeLayer()
F1 = self.ui.cBox_Width.currentText()
F2 = self.ui.cBox_Length.currentText()

new_Field = "F3"
provider = layer.dataProvider()

idx = layer.fieldNameIndex(new_Field)
if idx == -1:
    layer.dataProvider().addAttributes([QgsField(new_Field, QVariant.Double)])

layer.updateFields()
layer.startEditing()
#bufferfield = layer.fieldNameIndex('buffer_km')
new_Field_idx = layer.fieldNameIndex(new_Field)
formula = QgsExpression(str(F1) + '*' + str(F2))
formula.prepare(layer.pendingFields())
for f in layer.getFeatures():
    f[new_Field_idx] = formula.evaluate(f)
    layer.updateFeature(f)

layer.commitChanges()
7
  • Hi @Joseph, Unfortunately the result field is empty.
    – HMadadi
    Oct 27, 2017 at 11:38
  • @nickan - Apologies, edited the post :)
    – Joseph
    Oct 27, 2017 at 11:53
  • Sorry I can't get any result.
    – HMadadi
    Oct 27, 2017 at 12:20
  • @nickan - Does it work if you replace F1 and F2 with the actual field names in the line formula = QgsExpression(str(F1) + '*' + str(F2))? (i.e. formula = QgsExpression('fieldName1 * fieldName2))
    – Joseph
    Oct 27, 2017 at 12:32
  • I tested your advice and replace F1 and F2 with field names and but it didn't work :(
    – HMadadi
    Oct 27, 2017 at 13:13

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.