Thursday 20 June 2019

How to access feature classes in file geodatabases with Python and GDAL?


I am trying to access a vector dataset in a ESRI File Geodatabase using Python + GDAL. I have successfully compiled GDAL with the file geodatabase API. The FileGDB driver is working correctly since entering


ogrinfo --formats

shows the FileGDB driver and entering


ogrinfo myfilegdb.gdb 


gives me the correct information on the content of the database.


However, I can't find out how to access the content itself in Python. For accessing a shapefile, I would write:


driver = ogr.GetDriverByName('ESRI Shapefile')
ds = driver.Open('shapefile.shp', 0)

When accessing a FileGDB feature class I would assume using the commands:


driver = ogr.GetDriverByName('FileGDB')
ds = driver.Open('myfilegdb.gdb/feature_class', 0)

but this doesn't seem to work since it can not identify/locate the data set. Does anyone know how to call individual feature classes from a ESRI FileGDB.



I'm using Python 2.7, GDAL 1.9.1, filegdb api 1.2 on Ubuntu 12.04 x64. Thanks for any suggestions!



Answer



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
>
>>> ds.GetLayer("buildings")
>

>>> b = ds.GetLayer("buildings")
>>> sr = b.GetSpatialRef()
>>> sr
>
>>> 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.


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...