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.

Alex Tereshenkov
  • 29,912
  • 4
  • 54
  • 119
  • 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 '16 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. – Alex Tereshenkov May 12 '16 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. – Alex Tereshenkov May 12 '16 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 '16 at 05:38
  • @Luke, perfect, got it working. I thought you had a typo as it sounded logic to put the dll into the plugins. Out of curiosity, is ogr_FileGDB.dll used for OpenFileGDB driver or for something else? – Alex Tereshenkov May 13 '16 at 05:52
  • 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 '16 at 06:03
  • @AlexTereshenkov The FileGDB.dll you downloaded from Esri is the FileGDB API. The source for this is not open/available. When you call an OGR function, the driver (gdalplugins/ogr_FileGDB.dll) translates that to the Esri API (FileGDB.dll). The GDAL_DRIVER_PATH env var controls where gdal/ogr looks for driver plugins. The API dll needs to be where Windows will look for it when the driver tries to load it. i.e either be in the same directory as the main gdal11.dll or in one of the directories in your PATH env. var. – user2856 May 13 '16 at 06:04
  • Thanks a ton for the help and clarifications, now it's not as complicated as it was :) – Alex Tereshenkov May 13 '16 at 06:21
  • You need an ESRI account to download FileGDB_API_VS2010_1_4.zip from ESRI website. Instead, you can download it directly from their github repo. – François Leblanc Mar 16 '18 at 15:40
  • I got this error when trying to import fiona : ERROR 1: Can't load requested DLL: c:\Users\aboufira\AppData\Local\Continuum\miniconda3\envs\11203028_PHP\Lib\site-packages\osgeo\gdalplugins\ogr_FileGDB.dll 126: The specified module could not be found. – user32882 Jul 05 '19 at 14:24

4 Answers4

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.

user2856
  • 65,736
  • 6
  • 115
  • 196
  • I'd to copy bin64\FileGDBAPI.dll from FileGDB_API_1_5_1-VS2010.zip located at https://github.com/Esri/file-geodatabase-api/tree/master/FileGDB_API_1.5.1 at step 1 to make it work (in Python 3.5 64bit) – Mattijn Jan 30 '18 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 05 '19 at 12:25
  • @user32882 These instructions are not for conda installed gdal... – user2856 Jul 05 '19 at 13:34
  • How can I set it up for conda? Is this possible? – user32882 Jul 05 '19 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. – NW_Photo_Laureate Feb 01 '23 at 01: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.

sgillies
  • 9,056
  • 1
  • 33
  • 41
  • 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? – Alex Tereshenkov May 12 '16 at 15:34
  • 1
    The GDAL_DRIVER_PATH variable is the key. Read https://trac.osgeo.org/gdal/wiki/ConfigOptions#GDAL_DRIVER_PATH and if that doesn't set you on the right path, see the thread at http://lists.osgeo.org/pipermail/gdal-dev/2014-May/039066.html. – sgillies May 12 '16 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. – Alex Tereshenkov May 13 '16 at 05:56
  • This is absolutely the correct answer. No need to change the installation around, just set the right paths! – Benjamin Mar 14 '19 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.

15Step
  • 2,430
  • 1
  • 13
  • 28
Rudy Stricklan
  • 618
  • 5
  • 12
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']
alexyshr
  • 71
  • 5