1

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?

2
  • have you tried anything? the pattern in the second answer seems promising gis.stackexchange.com/a/365097/12552
    – Paul H
    Commented Aug 21, 2020 at 22:13
  • @PaulH, I have tried the suggested answer by martinfleis, but it did not work (tried absolute and relative path), but I am unsure about the syntax used in that answer. gis.stackexchange.com/a/365097/12552 shows how to extract the zip into memory, which is correct. But my question is how to convert the in-memory extracted gpkg file to a gdf GeoDataFrame. So far I've only seen it done with the shapefile package.
    – Heinrich
    Commented Aug 23, 2020 at 20:20

1 Answer 1

1

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

3
  • what is the prefix 'zip:' at the start of the path? I've never seen that. As well as the '!' between the .zip and the .gpkg? Guessing that specified the file in the zip? Also, I've tried that syntax, but with no luck :(
    – Heinrich
    Commented Aug 23, 2020 at 20:16
  • See the detials on zip file syntax in fiona documentation: fiona.readthedocs.io/en/latest/manual.html#virtual-filesystems Commented Aug 23, 2020 at 20:47
  • thanks! sorry about the answer edit, I just wanted it to be as specific as possible for others!
    – Heinrich
    Commented Aug 23, 2020 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.