1

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?

0

If you want to edit specific layers, you can insert their names into a list and iterate through these using a for loop. You can then add the rest of your script inside the loop to affect each layer in turn:

layer_names = ['layer1',  'layer2', 'layer3']

for layers in QgsProject.instance().mapLayers().values():
    if layers.name() in layer_names:
        sa = layers.fields().lookupField('area_calc')
        # do more stuff
  • Thanks for your advice. I can now add attributes to the table, but unfortunately I can't fill the table with values. It should look like 'layers.fields().lookupField('name') = 6'. Have you any idea how to add values within the table? (By the way the value is in every table the same). – Helga F. Jun 21 '18 at 12:26
  • You can't fill the tables using the code you wrote from your previous question? Could you please edit your question to include your new script so that we can see how far it works? :) – Joseph Jun 22 '18 at 9:33
0

I add the changes to my script. Here is the code:

layer_edit = ['layer1', 'layer2','layer3']
price = 6.0
for layers in QgsProject.instance().mapLayers().values():
    if layers.name() in layer_edit:
        layers.dataProvider().addAttributes([QgsField('eur1_km2',QVariant.Double)])
        layers.dataProvider().addAttributes([QgsField('eur1_pol',QVariant.Double)])
        layers.updateFields()
        idx = layers.dataProvider().fieldNameIndex('eur1_km2')

        layers.startEditing()
        for f in layers.getFeatures():
                f[idx] = price
                layers.updateFeature(f)
                idx = layers.dataProvider().fieldNameIndex('eur1_km2')
                idx1 = layers.dataProvider().fieldNameIndex('eur1_pol')
                sa = layers.dataProvider().fieldNameIndex('area_calc')
                attr_idx = f.attributes()[idx]
                attr_sa = f.attributes()[sa]
                f[idx1]=(attr_sa/1000000*attr_idx)
                layers.updateFeature(f)

        layers.commitChanges()

    else: print('NONE')

Now it works.

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.