Wednesday, 18 April 2018

python - How to add support for FileGDB (Esri file gdb API) driver in fiona?


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.



Answer




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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...