Last active
December 17, 2020 18:09
-
-
Save ckrapu/1807577fcb79f0522c87552bfb0ebf76 to your computer and use it in GitHub Desktop.
Use OSM API to download buildings and turn them into GeoPandas geodataframes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import geopandas as gpd | |
import numpy as np | |
import pandas as pd | |
import requests | |
save_directory = '../data/' | |
osm_api_url_base = 'http://overpass.openstreetmap.ru/cgi/xapi_meta?' | |
regions = { | |
'washingtondc_test' : (-77.0,38.99,-76.991,39.00), | |
'moscow_test' : (37.40,55.90,37.405,55.905) | |
} | |
gdfs = {} | |
tag_query = 'way[building=*]' | |
for name, bbox in regions.items(): | |
osm_url = osm_api_url_base + tag_query + '[bbox={0},{1},{2},{3}]'.format(*bbox) | |
retrieved = requests.get(osm_url) | |
as_text = retrieved.text | |
osm_filename = 'osm_{0}.osm'.format(name) | |
with open(save_directory + osm_filename,'w') as f: | |
f.write(as_text) | |
gpkg_filename = 'osm_{0}.gpkg'.format(name) | |
! cd $save_directory; ogr2ogr $gpkg_filename $osm_filename | |
gdfs[name] = gpd.read_file(save_directory + gpkg_filename, layer='multipolygons') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist is for use in a Jupyter notebook and uses the OpenStreetMap database and one of its API mirrors to download all records corresponding to buildings within a given bounding box. It then uses ogr2ogr to convert them from .osm to .gpkg files and load them in GeoPandas.