4

I'm trying to iterate through all the features of a layer to change the value of some of its attributes. I'm using the following code:

from qgis.core import *
import os

working_folder = r'C:\myFolder'
fc_Opt_Prospect = os.path.join(working_folder, 'Equipm.gdb|layername=Prosp')

layer = QgsVectorLayer(fc_Opt_Prospect, 'Optimity_Prospect', 'ogr')

layer.startEditing()

for f in layer.getFeatures():
    layer.changeAttributeValue(f.id(), 4, 3)
    layer.changeAttributeValue(f.id(), 5, 6)

layer.commitChanges()
QgsProject.instance().addMapLayer(layer)

When I check the attribute table of the layer I can see that no value has been changed. No error is thrown by the script.

3 Answers 3

3

QGIS can't edit Esri file geodatabases, even at version 3.8:

enter image description here

1
  • 2
    3.22 I believe editing was introduced to FGDB. Whether the above script will work though, not 100%.
    – nr_aus
    Commented Aug 31, 2023 at 13:55
2

As was mentioned in @nr_aus's comment it is possible to edit the Esri File Geodatabase since QGIS 3.22, see the changelog for more details.

I test it with the following data file: https://gisdata-piercecowa.opendata.arcgis.com/datasets/1ed55090838643a99b07fdffdb0e5ac9_0

And similar code as above will work:

from os.path import join
from qgis.core import QgsVectorLayer

working_folder = "C:/Users/taras/Downloads/Libraries_9209718161064393263/"
libs = join(working_folder, 'f49bdce3-128c-4082-9beb-71ab798f7410.gdb|layername=Libraries')

layer = QgsVectorLayer(libs, 'Libraries', 'ogr')

if layer.isValid():
    with edit(layer):
        print(layer.isEditable()) # True

Tested on QGIS 3.34.6-Prizren on my Windows 11.

0

I have realised that "layer.startEditing()" is returning false. For some reason the layer is not editable. I guess that's the reason why values are not changed at all with the script above. Now remains the question of why the layer is not editable.

Your Answer

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

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