Author: Geoff Boeing
Use OSMnx to download OpenStreetMap building footprints and visualize them as figure-ground diagrams.
import osmnx as ox
from IPython.display import Image
%matplotlib inline
ox.__version__
# configure the inline image display
img_folder = "images"
extension = "png"
size = 240
# specify that we're retrieving building features from OSM
tags = {"building": True}
gdf = ox.features_from_place("Piedmont, California, USA", tags)
gdf_proj = ox.project_gdf(gdf)
fp = f"./{img_folder}/piedmont_bldgs.{extension}"
fig, ax = ox.plot_footprints(gdf_proj, filepath=fp, dpi=400, save=True, show=False, close=True)
Image(fp, height=size, width=size)
# save as a GeoPackage
gdf_save = gdf.applymap(lambda x: str(x) if isinstance(x, list) else x)
gdf_save.drop(labels="nodes", axis=1).to_file("./data/piedmont_bldgs.gpkg", driver="GPKG")
# calculate the area in projected units (meters) of each building footprint, then display first five
areas = gdf_proj.area
areas.head()
# total area (sq m) covered by building footprints
sum(areas)
# get the total area within Piedmont's admin boundary in sq meters
place = ox.geocode_to_gdf("Piedmont, California, USA")
place_proj = ox.project_gdf(place)
place_proj.area.iloc[0]
# what proportion of piedmont is covered by building footprints?
sum(areas) / place_proj.area.iloc[0]
point = (48.873446, 2.294255)
dist = 612
gdf = ox.features_from_point(point, tags, dist=dist)
gdf_proj = ox.project_gdf(gdf)
bbox = ox.utils_geo.bbox_from_point(point=point, dist=dist, project_utm=True)
fp = f"./{img_folder}/paris_bldgs.{extension}"
fig, ax = ox.plot_footprints(
gdf_proj,
bbox=bbox,
color="w",
filepath=fp,
dpi=90,
save=True,
show=False,
close=True,
)
Image(fp, height=size, width=size)
Plot and save to disk as .png
# helper funcion to get one-square-mile street networks, building footprints, and plot them
def make_plot(
place,
point,
network_type="drive",
dpi=40,
dist=805,
default_width=4,
street_widths=None,
):
fp = f"./{img_folder}/{place}.{extension}"
gdf = ox.features_from_point(point, tags, dist=dist)
fig, ax = ox.plot_figure_ground(
point=point,
dist=dist,
network_type=network_type,
default_width=default_width,
street_widths=street_widths,
save=False,
show=False,
close=True,
)
fig, ax = ox.plot_footprints(
gdf, ax=ax, filepath=fp, dpi=dpi, save=True, show=False, close=True
)
place = "portland_buildings"
point = (45.517309, -122.682138)
make_plot(place, point)
place = "richmond_district_buildings"
point = (37.781999, -122.472501)
make_plot(place, point)
place = "port_au_prince_buildings"
point = (18.522240, -72.347607)
make_plot(place, point, network_type="all", default_width=1, street_widths={"secondary": 3})
place = "monrovia_liberia_buildings"
point = (6.340236, -10.747255)
make_plot(place, point, network_type="all", default_width=2, street_widths={"primary": 6})