I'm trying to create a field in a vector layer and then update the values based on the sum of two fields which are already present in the table, using the Python console (QGIS3.4). Also I'm still learning Python... I originally started to develop this under 2.18, but I've recently had to move to QGIS 3 (which is part of the problem)- In 2.18 the first part of the script used to work i.e. creation of a new field in the table.
The original code:
from qgis.core import *
from qgis.utils import *
from qgis.gui import *
from PyQt4.QtCore import QVariant
import processing
import os
layer_P = QgsProject.instance().mapLayersByName("BXO")[0]
SUM = QgsField('t_branch', QVariant.Int)
layer_P.addAttribute (SUM)
idx = layer_P.fieldNameIndex('t_branch')
layer_P.startEditing()
e = QgsExpression ('CB_Num_Ent' + 'CB_Num_Ext')
e.prepare( 't_branch'() )
for f in layer_P.getFeatures():
f[idx] = e.evaluate( f )
layer_P.updateFeature( f )
layer_P.updateField()
layer_P.commitChanges()
I've been looking at the API and seen that fieldNameIndex
has been replaced with fields() .lookupField() or fields() .indexFromName()
and since I've tried a couple of variations - for example:
layer_P = QgsProject.instance().mapLayersByName("BXO")[0]
SUM = QgsField('t_branch', QVariant.Int)
layer_P.addAttribute (SUM)
idx = layer_P.lookupField('t_branch')
layer_P.startEditing()
e = QgsExpression ('CB_Num_Ent' + 'CB_Num_Ext')
e.prepare( layer_P.fields() )
for f in layer_P.getFeatures():
f[idx] = e.evaluate( f )
layer_P.updateFeature( f )
layer_P.updateField()
layer_P.commitChanges()
but for the minute I'm still get the same type of error message
...AttributeError: 'QgsVectorLayer' object has no attribute 'lookupField'
So, firstly I'm trying to find out how to modify the script following the removal of indexFromName. And if that works, hopefully update my new field based on the calculation here :
> e = QgsExpression ('CB_Num_Ent' + 'CB_Num_Ext')
For info the script was adapted from the following : Adding field and calculating expression with PyQGIS