I have many layers but only want to edit several of them in QGIS 3.0
layer1 = QgsVectorLayer('...','layer1','ogr')
layer2 = QgsVectorLayer('...','layer2','ogr')
layer3 = QgsVectorLayer('...','layer3','ogr')
layer4 = QgsVectorLayer('...','layer4','ogr')
layer5 = QgsVectorLayer('...','layer5','ogr')
layer6 = QgsVectorLayer('...','layer6','ogr')
layers_edit = QgsProject.instance().mapLayersByName('layer1' and 'layer2' and 'layer3')
In every layer an area is given ('area_calc')
sa = layers.dataProvider().fieldNameIndex('area_calc')
An error occurs:
Traceback (most recent call last):
File "C:\PROGRA~1\QGIS3~1.0\apps\Python36\lib\code.py", line 91, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
AttributeError: 'list' object has no attribute 'dataProvider'
Making a list of layers and startEditing() also doesn't work.
layers.startEditing()
Traceback (most recent call last):
File "C:\PROGRA~1\QGIS3~1.0\apps\Python36\lib\code.py", line 91, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
AttributeError: 'list' object has no attribute 'startEditing'
The next step I want to do is:
layers.dataProvider().addAttributes([QgsField ('eur1_km2',Variant.Double),QgsField('eur1_pol', QVariant.Double)])
layers.updateFields()
idx=layers.dataProvider().fieldNameIndex('eur1_km2')
idx1=layers.dataProvider().fieldNameIndex('eur1_pol')
for f in layers.getFeatures():
f[idx] = 6
attr_sa = f.attributes()[sa]
attr_idx = f.attributes()[idx]
f[idx1] = (attr_sa/1000000)*attr_idx
layers.updateFeature( f )
layers.commitChanges()
But the same error occurs. Has anyone an idea how to edit these three layers simultanously with python in QGIS 3.0?