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?

Heinrich
  • 340
  • 1
  • 4
  • 12
  • have you tried anything? the pattern in the second answer seems promising https://gis.stackexchange.com/a/365097/12552 – Paul H Aug 21 '20 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 Aug 23 '20 at 20:20

1 Answers1

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

Heinrich
  • 340
  • 1
  • 4
  • 12
martinfleis
  • 7,124
  • 2
  • 22
  • 30
  • 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 Aug 23 '20 at 20:16
  • See the detials on zip file syntax in fiona documentation: https://fiona.readthedocs.io/en/latest/manual.html#virtual-filesystems – martinfleis Aug 23 '20 at 20:47
  • thanks! sorry about the answer edit, I just wanted it to be as specific as possible for others! – Heinrich Aug 23 '20 at 20:52