First time I work with zipfiles in Python :-/
The task at hand is the following (main requirement is not writing anything to disc)
given this url: http://shapefiles.fews.net.s3.amazonaws.com/ALL_HFIC.zip
- get the zipfile
- extract shapefiles from zip arcvhie which contain
Africa
in the file name. - merge all files into one shapefile (read all files into geopandas).
- convert to geoJson.
This is the structure of the code I have so far - but I keep getting an attribute error
AttributeError: 'ZipFile' object has no attribute 'seek'
import io
import zipfile
import pandas as pd
import geopandas as gpd
# util funcs
is_africa = lambda string: "Africa" in string
is_shape = lambda string: string.endswith('shp')
# get_zip() defined in module
filebytes = io.BytesIO(get_zip(url=URL).content)
# get the zipfile object
myzipfile = zipfile.ZipFile(filebytes)
# instantiate empty list where to store the shapefiles of interest.
shapefiles = []
# below code adapted from: https://stackoverflow.com/questions/4917284/
with zipfile.ZipFile(zip_file, 'r') as zf:
for file_name in zf.namelist():
if is_africa(file_name) and is_shape(file_name):
data = zf.read(file_name)
shapefiles.append(data)
# below code adapted from https://stackoverflow.com/questions/48874113/
gdf_africa = gpd.GeoDataFrame(pd.concat([gpd.read_file(i) for i in shapefiles],
ignore_index=True),
crs=gpd.read_file(shapefiles[0]).crs)
gdf_africa.to_file("output.json", driver="GeoJSON")