python-3.xshapefilegeopandasfionageopackage

Read a gpkg file from memory/zipfile


I know that it is possible to read a shapefile from a zipfile by extracting it in memory and then reading it: https://gis.stackexchange.com/questions/250092/using-pyshp-to-read-a-file-like-object-from-a-zipped-archive

Fiona also has ways to read a shapefile from memory: https://pypi.org/project/Fiona/1.5.0/

However, I haven't been able to find a way to read in a .gpkg (geopackage) in the same way.

How do I extract a geopackage from a zipfile and then into a geopandas geodataframe?


Solution

  • You can read it directly by specifying the path to gpkg within zip.

        df = gpd.read_file('zip:///path/to/file.zip!data.gpkg')
    

    for relative path:

        df = gpd.read_file('zip://../path/to/file.zip!data.gpkg')
    

    (in the case of needing to go back a directory and then into 'path/to/' etc