Skip to content

Instantly share code, notes, and snippets.

@kylehounslow
Created October 30, 2017 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylehounslow/9348e2992c0d0019210c4142d20f3dee to your computer and use it in GitHub Desktop.
Save kylehounslow/9348e2992c0d0019210c4142d20f3dee to your computer and use it in GitHub Desktop.
Download OSM data and load into geopandas GeoDataFrame
import os
import wget
import zipfile
import geopandas as gpd
dl_link = 'https://s3.amazonaws.com/metro-extracts.mapzen.com/san-francisco_california.imposm-shapefiles.zip'
shapefile_dir = './shapefiles'
shapefile_name ='san-francisco_california_osm_buildings.shp'
shapefile = os.path.join(shapefile_dir, shapefile_name)
def download_unzip_file(url, out_dir):
print('Downloading file from {}...'.format(url))
wget.download(url)
_, zipfile_name = os.path.split(url)
if not os.path.exists(out_dir):
os.mkdir(out_dir)
print('extracting {} to {}...'.format(zipfile_name, out_dir))
with zipfile.ZipFile(zipfile_name,"r") as zip_ref:
zip_ref.extractall(out_dir)
print('Done.')
os.remove(zipfile_name)
# check if file already on disk
if not os.path.exists(shapefile):
download_unzip_file(url=dl_link, out_dir=shapefile_dir)
# load dataframe from shapefile
df = gpd.GeoDataFrame.from_file(shapefile)
# we can now work with GeoDataFrame just like a pandas dataframe
# e.g. list all unique building types:
print(df['type'].unique())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment