How to access feature classes in file geodatabases with Python and GDAL?
You're almost there. This is on Windows 7, Python 2.6.5 32bit, and GDAL 1.9.0:
>>> from osgeo import ogr
>>> driver = ogr.GetDriverByName("FileGDB")
>>> ds = driver.Open(r"C:\temp\buildings.gdb", 0)
>>> ds
<osgeo.ogr.DataSource; proxy of <Swig Object of type 'OGRDataSourceShadow *' at 0x02BB7038> >
>>> ds.GetLayer("buildings")
<osgeo.ogr.Layer; proxy of <Swig Object of type 'OGRLayerShadow *' at 0x02BB7050> >
>>> b = ds.GetLayer("buildings")
>>> sr = b.GetSpatialRef()
>>> sr
<osgeo.osr.SpatialReference; proxy of <Swig Object of type 'OSRSpatialReferenceShadow *' at 0x02BB7080> >
>>> sr.ExportToProj4()
'+proj=utm +zone=15 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs '
>>>
Once you open the FGDB, then use GetLayer
to get at your featureclass.
Much simpler and intuitive if you use fiona and geopandas
import fiona
import geopandas as gpd
# Get all the layers from the .gdb file
layers = fiona.listlayers(gdb_file)
for layer in layers:
gdf = gpd.read_file(gdb_file,layer=layer)
# Do stuff with the gdf
Note: fiona uses gdal and geopandas uses fiona
See also Reading the names of geodatabase file layers in Python
I would like to add that "FileGDB" is a propriatary driver that might not be included with you GDAL package http://www.gdal.org/drv_filegdb.html. This results in GetDriverByName
returning None
.
There is also the "OpenFileGDB" driver which is read only and is included by default http://www.gdal.org/drv_openfilegdb.html
>>> from osgeo import ogr
>>> driver = ogr.GetDriverByName("OpenFileGDB")