0

I'm able to view layers, fields and iterate features but when I try to delete/create a feature or add a new field I run into errors or it does nothing. I'm using the FileGDB driver. If I throw this .gdb into QGIS I can easily delete, or add features (which confirms FileGDB driver works). Is there a way to do it with Python? Ideally I find a way to add/delete fields and layers and add/delete features.

from osgeo import ogr,gdal

test_gdb = r"C:\Users\52\Downloads\geodatabase\GIS\DdaS69f.gdb"
driver = ogr.GetDriverByName("FileGDB")
dataSource = driver.Open(test_gdb, 0)

fldDef = ogr.FieldDefn('DistFld', ogr.OFTReal)
fldDef2 = ogr.FieldDefn('Name', ogr.OFTString)
fldDef2.SetWidth(16) #16 char string width

for x in range(dataSource.GetLayerCount()):
    layer = dataSource.GetLayerByIndex(x)
    layer.CreateField(fldDef)
    layer.CreateField(fldDef2)
    print("Layer Name: {}".format(layer.GetName())) ## Get layer name
    layerDefinition = layer.GetLayerDefn()

    for field in range(layerDefinition.GetFieldCount()):

        print("\nField : {}\n".format(layerDefinition.GetFieldDefn(field).GetName()))
        for feature in layer:
            print(feature.GetField("{}".format(layerDefinition.GetFieldDefn(field).GetName())))
            layer.DeleteFeature(feature.GetFID())

        layer.ResetReading()

Is it possible to edit a .gdb using the FileGDB driver?

  • Did you check the documentation here? it mentions a few known issues, you may want to check if they apply to your case – Sergio Monteleone Apr 19 '18 at 10:06
  • Thanks. It looks like that information is how to use it with the OGR command line, no info on Python bindings. I could just use Python to call the OGR command line but would prefer the bindings. – fpolig01 Apr 19 '18 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.