16

There has been a tons of questions on that but none of them solved my problem.

I have a machine with:

  • Windows 7 x64
  • Python 3.4.3 x64
  • gdal installed: gdalinfo --version --> GDAL 1.11.4, released 2016/01/25; gdal was installed from a wheel GDAL-1.11.4-cp34-none-win_amd64.whl from here

The GDAL is installed into C:\Python34\Lib\site-packages\osgeo. There I have a bunch of .exe files for gdal and ogr and .pyd files.

  • fiona installed: was installed from a wheel Fiona-1.6.3-cp34-none-win_amd64.whl, from the same website.

I am able to run this code and it executes successfully:

import gdal
import ogr
from gdalconst import *

shp = r"C:\Data\GIS\PTS.shp"
driver = ogr.GetDriverByName('ESRI Shapefile')

dataset = driver.Open(shp)    
layer = dataset.GetLayer()
layer.GetFeatureCount()    
schema = layer.schema
fields = [field.GetName() for field in schema]    
feature = layer.GetNextFeature()

I am also able to get OGR formats: ogrinfo --formats prints a bunch of them in the Windows cmd (with no FileGDB there though).

I am able to run this code and it executes successfully:

import fiona
with fiona.drivers():
  with fiona.open(path=r'C:\Data\GIS\TemplateData.gdb', driver='OpenFileGDB') as source:
    print(source.meta)

However, this code won't run:

with fiona.drivers():
    with fiona.open(path=r'C:\Data\GIS\TemplateData.gdb', driver='FileGDB') as source:
        print(source.meta)

Because I don't have Esri File GDB compiled libraries which are required.

I have downloaded and unpacked FileGDB_API_VS2012_1_3.zip from the Esri downloads page. As I understood, there is no need to compile anything as the .dll is already there.

What is the correct procedure to register the dll of the Esri File GDB API to be able to use them in fiona in my environment?


UPDATE: (based on Luke's answer)

I have downloaded the File Geodatabase API 1.4 version for Windows (Visual Studio 2010) from the Esri downloads page. I copied the FileGDB_API_VS2010_1_4\bin64\FileGDBAPI.dll to the C:\Python34\Lib\site-packages\osgeo. Now I have two files in here, ogr_FileGDB.dll and FileGDBAPI.dll.

I have created a Windows variable GDAL_DRIVER_PATH : C:\Python34\Lib\site-packages\osgeo\gdalplugins. In the PATH variable, I don't have anything Python specific except the C:\Python34\Lib\site-packages\osgeo.

Now when running the ogrinfo --formats I get -> "FileGDB" (read/write) and am able to use the Python code for working with the FileGDB driver.

10
  • The 1.4 File Geodatabase API has been released for a lonnng time now. There really is no reason to use 1.3.
    – Vince
    May 12, 2016 at 11:12
  • @Luke, thanks, but I need write access to the gdb. Afaik, you can only read with OpenFileGDB... Any specific links or doc page how to achieve that with the compilation and how to point out to the dll (via Windows variables etc)? Would be very helpful. May 12, 2016 at 12:16
  • @Vince, I don't mind using 1.4 or any other version that would allow me editing the file gdb :) I lack the docs page / spec with clear instructions how to be able to do that. May 12, 2016 at 12:16
  • @AlexTereshenkov As you're using 64bit python, copy the Esri bin64\FileGDBAPI.dll to the [python install dir]\Lib\site-packages\osgeo dir as I specified in my answer, not the gdalplugins dir you mention in your edit. Only driver plugins go in that directory, not 3rd party dlls.
    – user2856
    May 13, 2016 at 5:38
  • 1
    @AlexTereshenkov, ogr_FileGDB.dll is the FileGDB driver (not OpenFileGDB which is built into gdal/ogr). This dll is compiled from the GDAL/OGR driver code. It needs to be in the directory specified by the GDAL_DRIVER_PATH env var.
    – user2856
    May 13, 2016 at 6:03

4 Answers 4

11

The Gohlke GDAL/OGR wheel includes the FileGDB driver compiled as a plugin.

To get the FileGDB driver working:

  1. Copy the Esri bin64\FileGDB.dll to [python install/virtualenv dir]\Lib\site-packages\osgeo (use bin\FileGDB.dll if using 32bit python). Do not copy the FileGDB.dll to the gdalplugins directory.
  2. Set GDAL_DRIVER_PATH environment variable, either:
    • manually; or
    • edit [python install/virtualenv dir]\Lib\site-packages\osgeo\__init__.py and uncomment line 10.
      # uncomment the next line to enable plugins
      os.environ['GDAL_DRIVER_PATH'] = os.path.join(os.path.dirname(__file__), 'gdalplugins')

Opening a GDB with the FileGDB driver should now work.

>>> import fiona
>>> with fiona.drivers():
...     with fiona.open(path=r'C:\Temp\Default.gdb', driver='FileGDB') as source:
...         print(source.meta)
...
{'crs': {'init': u'epsg:4326'}, 'driver': 'FileGDB', 'crs_wkt': u'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01
74532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]', 'schema': {'geometry': 'MultiPolygon', 'properties': OrderedDict([(u'SHAPE_Length', 'float'), (u'SHAPE_Area', 'float')])}}
>>>

Note:

Using Python 2.7 I could only get FileGDB plugin to work with the FileGDB API v1.3 (MSVC 2008). as v.1.4 segfaults python. I assume this is because python and the GDAL and Fiona libraries provided by Gohlke are compiled with MSVC 2008 and v. 1.4 is compiled with MSVC 2010 (and later).

The FileGDB API v1.4 works fine with Python 3.4 and the GDAL and Fiona libraries provided by Gohlke which are compiled with MSVC 2010.

5
  • I'd to copy bin64\FileGDBAPI.dll from FileGDB_API_1_5_1-VS2010.zip located at github.com/Esri/file-geodatabase-api/tree/master/… at step 1 to make it work (in Python 3.5 64bit)
    – Mattijn
    Jan 30, 2018 at 16:28
  • My gdal was automatically installed by geopandas using the miniconda command line conda install geopandas. In __init__.py line 10 is already uncommented, so I don't think it is the line you are referring to. Also there is no gdalplugins file
    – user32882
    Jul 5, 2019 at 12:25
  • @user32882 These instructions are not for conda installed gdal...
    – user2856
    Jul 5, 2019 at 13:34
  • How can I set it up for conda? Is this possible?
    – user32882
    Jul 5, 2019 at 14:07
  • @user32882 if still interested, a newer release of GeoPandas (I use 0.9.0) is now able to read/write the OpenFileGDB driver (ESRI File Geodatabase). Geopandas 0.9.0 uses Fiona 1.8.19 FYI. I successfully merged two feature classes and saved it out to an ESRI File Geodabase. The file appears good. Python line for reference: gdf_joined.to_file('path/to/gdb', layer = 'feature_class', driver = 'OpenFileGDB'). Note that the OpenFileGDB driver does NOT write fields with python datetime.date dtypes, whereas gdf_joined.to_file('path/to/shp') can write shapefiles with datetime.date dtypes. Feb 1 at 1:06
4

The key information is here:

I am also able to get OGR formats: ogrinfo --formats prints a bunch of them in the Windows cmd (with no FileGDB there though).

This tells me that your GDAL_DRIVER_PATH environment variable is not set. See the instructions at https://trac.osgeo.org/gdal/wiki/FileGDB#Testingthedriver. Once "FileGDB" shows in ogrinfo --formats (or fio env --formats), you'll be good to go.

4
  • thanks. Surprised you have time to answer questions on GIS.SE - you are probably super busy with fiona, shapely and other stuff. Very cool things you wrote, kudos. I am aware that I don't have the FileGDB driver hence asking how to do that. I've seen the page you refer to, it says one needs to Compile GDAL with the FGDB section commented . I don't have the GDAL source; I've just installed it from the wheel, so I guess those steps are not applicable in my case. Where should I go from here? May 12, 2016 at 15:34
  • 1
    The GDAL_DRIVER_PATH variable is the key. Read trac.osgeo.org/gdal/wiki/ConfigOptions#GDAL_DRIVER_PATH and if that doesn't set you on the right path, see the thread at lists.osgeo.org/pipermail/gdal-dev/2014-May/039066.html.
    – sgillies
    May 12, 2016 at 19:17
  • A bit misleading steps as I didn't need to compile anything - the wheels I've installed already had everything I needed, it was just about pasting the files in the right folder. But thanks a ton for the useful info anyways, good to know for some other use cases. May 13, 2016 at 5:56
  • This is absolutely the correct answer. No need to change the installation around, just set the right paths!
    – Benjamin
    Mar 14, 2019 at 18:40
0

Copying files FileGDBAPI.dll into folder C:\Program Files\QGIS 2.18\bin and ogr_FileGDB.dll into folder C:\Program Files\QGIS 2.18\bin\gdalplugins does the trick for me. Here's a document that explains their use.

1
  • 1
    It appears all of the links are dead.
    – Aaron
    Nov 10, 2020 at 21:31
0

I just needed the right version of the FileGBD library. It depends on the way your GDAL and FIONA installers (*.wls in my case) were compiled (Visual Studio Version).

Besides copying FileGDBAPI.dll into the folder osgeo (as indicated in the previous post), I had to copy the file ogr_FileGDB.dll to the folder gdalplugins.

The __init__.py file located in (site-packages\osgeo\) is looking for GDAL system variables (GDAL_DATA, PROJ_LIB, GDAL_DRIVER_PATH). If the variables are NOT found in the system, it creates the variables at running time. So, if you have the variable GDAL_DRIVER_PATH already configured, python will use it to process the code. If you have the variables in the system, you may need to perform some changes in the variables or in the location of the files (ogr_FileGDB.dll, FileGDBAPI.dll)

The process described below is for a system without mentioned environment variables, or if configured, pointing to desired [python install/virtualenv dir]

Systems and versions information:

Procedure:

  1. Download the File Geodatabase API 1.5.2 version for Windows (Visual Studio 2019) from here. Copy the FileGDB_API_VS2019.zip\bin64\FileGDBAPI.dll to the [python install/virtualenv dir]\Lib\site-packages\osgeo]
  2. Copy the file ogr_FileGDB.dll:
  • From the folder [python install/virtualenv dir]\Lib\site-packages\osgeo\gdalplugins\disabled]
  • To the folder [python install/virtualenv dir]\Lib\site-packages\osgeo\gdalplugins]

Note: The command ogrinfo.exe --formats will list the FileGDB driver even if a bad library version is installed. The real test is using the driver with OGR or FIONA, for instance:

ogr

from osgeo import ogr
driver_w = ogr.GetDriverByName("FileGDB")
ds_w = driver_w.Open(".\data\data.gdb", 0)
fc_counties_layer_w = ds_w.GetLayer("counties")

if fc_counties_layer_w is None:
    print("Could not open counties in .\\data\\data.gdb")

sr_w = fc_counties_layer_w.GetSpatialRef()
attributes_fc_counties_layer_w = fc_counties_layer_w.GetLayerDefn()

for i in range(attributes_fc_counties_layer_w.GetFieldCount()):
    print(attributes_fc_counties_layer_w.GetFieldDefn(i).GetName())

fiona

import fiona
with fiona.drivers():
    with fiona.open(".\data\data.gdb", driver='FileGDB') as source:
        print(source.meta)

Note 2: With Python, you can check the variables with the code below, but note that the result could be the value of the variables for the current Python environment or the value of the variables for the system. Check which Python environment are you using to run the next code.

from sys import version_info
import os
os.environ['GDAL_DATA']
os.environ['PROJ_LIB']
os.environ['GDAL_DRIVER_PATH']

Your Answer

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

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