QGIS – How to Edit File Geodatabase Using FileGDB Driver in Python

arcgis-desktopgdalogrpythonqgis

I'm trying to use the GDAL/OGR Python bindings to edit a file geodatabase in Python. I followed this good guide which demonstrates how to install QGIS and a special "FileGDB" driver that allows you to edit file geodatabases. This works in QGIS – I can drag in a file geodatabase, specify that I want to open it with the FileGDB driver and then I can edit it.

Now, I want to do this programmatically outside of the QGIS program. I first tried listing all of the drivers in the Python console within QGIS with the following

from osgeo import ogr
import sys

test_gdb = r"C:\Users\5976632\Downloads\geodatabase\GIS\DDS69ff.gdb"
driver = ogr.GetDriverByName("FileGDB")
cnt = ogr.GetDriverCount()
formatsList = [] 

for i in range(cnt):
    driver = ogr.GetDriver(i)
    driverName = driver.GetName()
    if not driverName in formatsList:
        formatsList.append(driverName)

formatsList.sort() 

for i in formatsList:
    print (i)

The results are – (Note that FileGDB driver is listed)

ARCGEN
AVCBin
AVCE00
AeronavFAA
AmigoCloud
BNA
CAD
CSV
CSW
Carto
Cloudant
CouchDB
DB2ODBC
DGN
DXF
EDIGEO
ESRI Shapefile
ElasticSearch
FileGDB
GFT
GML
GMLAS
GPKG
GPSBabel
GPSTrackMaker
GPX
GeoJSON
GeoRSS
Geoconcept
Geomedia
HTF
HTTP
Idrisi
Interlis 1
Interlis 2
JML
JP2OpenJPEG
KML
LIBKML
MSSQLSpatial
MapInfo File
Memory
MySQL
NAS
ODBC
ODS
OGR_GMT
OGR_OGDI
OGR_PDS
OGR_SDTS
OGR_VRT
OSM
OpenAir
OpenFileGDB
PCIDSK
PDF
PGDUMP
PGeo
PLSCENES
PostgreSQL
REC
S57
SEGUKOOA
SEGY
SQLite
SUA
SVG
SXF
Selafin
TIGER
UK .NTF
VDV
VFK
WAsP
WFS
Walk
XLS
XLSX
XPlane
netCDF

Then I wanted to figure out where the version of Python QGIS is using is located so I could use it externally. I did a

print(sys.executable)

and got

C:\OSGeo4W64\bin\qgis-bin.exe

which is not where Python is installed, it's pointing to the qgis.exe. I found what I believe is the Python.exe in the following location

"C:\OSGeo4W64\apps\Python36\python.exe"

I pointed my IDE to use that version of Python and when I list all of the drivers, FileGDB is missing…My question is why when I try to list all of the Drivers outside of QGIS in Python, is FileGDB not listed? Is QGIS setting some special environment when it opens?

If there is an easier way to do this please let me know. I just want to edit a file geodatabase using Python.

Best Answer

I needed to set GDAL_DRIVER_PATH environment variable to point to gdalplugins folder which in my case was located C:\OSGeo4W64\bin\gdalplugins

See this post How to add support for FileGDB (Esri file gdb API) driver in fiona?