Skip to content

Instantly share code, notes, and snippets.

@linwoodc3
Last active September 23, 2017 00:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linwoodc3/72b2f24b6d2ff6ffde1597f1ca2dea3f to your computer and use it in GitHub Desktop.
Save linwoodc3/72b2f24b6d2ff6ffde1597f1ca2dea3f to your computer and use it in GitHub Desktop.
Using Python 2 (doesn't work for 3), this function reads zipped shapefiles from the web or disk and convert it into a Python geodataframe (pandas dataframe with spatial features).
import geopandas as gpd
import numpy as np
import requests
import gdal
import fiona
import uuid
import re
# informaed by: https://gis.stackexchange.com/questions/225586/reading-raw-data-into-geopandas/225627
def shapefilereader(target):
"""Function to convert zipped shapefiles from the web or on disk into geopandas dataframes
Parameters
----------
target : str
string representing path to file on disk or url to download the zipped shapefile.
Returns
-------
Geopandas dataframe
Pandas dataframe with geospatial features and operations.
"""
# Detect whether we are using a web-based shapefile or local disk
r = re.compile('^(http|https)://',re.I)
if r.search(target):
download = True
request = requests.get(target)
target = '/vsimem/{}.zip'.format(uuid.uuid4().hex) #gdal/ogr requires a .zip extension
gdal.FileFromMemBuffer(target,bytes(request.content))
else:
download = False
with fiona.Collection(target,vsi='zip') as f:
return gpd.GeoDataFrame.from_features(f,crs=f.crs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment