Zum Hauptinhalt springen

Extract Arcgis Geodatabase format with Python

If you want to know how to extract the individual layers programmatically from a GeoDatabase-ArcGIS archive, then this is for you.

Geo data is often times provided in Geodatabase format. This format provides a container for several layers of data, which then can be rendered using software like ArcGIS or QGIS, which have already plugins to process this file format. But if you work on a lower level, you maybe need to process these files in their format programmatically.

As a bonus: If the input file, like in my case, is zipped, you can use fiona directly to unzip it into memory. For example when you download these files directly from a data portal or repository.

This is the full python script to read zipped geodatabase, assume that only one gdb-file, respective a gdb-directory is present.

import geopandas as gpd
import fiona
from fiona.io import ZipMemoryFile

import sys

with ZipMemoryFile(open( sys.argv[1], 'rb').read()) as zipo:

    myfile = list(filter(lambda x: x.endswith("gdb"), zipo.listdir()))

    if len(myfile) != 1:
        print("Error: Geodatabase archive has", len(myfile), "gdb files. Only need one.")
        exit(1)

    myfile = myfile[0]

    print("Try to get", myfile)

    with zipo.open( myfile ) as src:
        print()
        print("schema:", src.schema)

        for layer in fiona.listlayers(src.path):
            print()
            print(">> Layername:", layer)

            # fp = gpd.read_file(src.path, layer=layer)

This lets you call the python script with a path to the gdb zip file.

Hope this helps someone in the future and saves some time!