Skip to content

Instantly share code, notes, and snippets.

@etachov
Created October 14, 2018 16:23
Show Gist options
  • Save etachov/a029c849c89ee3d4f7d59dda83f97ed2 to your computer and use it in GitHub Desktop.
Save etachov/a029c849c89ee3d4f7d59dda83f97ed2 to your computer and use it in GitHub Desktop.
Making NYT-style building maps with data from Microsoft
library(tidyverse)
library(sf)
library(tigris)
# start by picking a state from https://github.com/Microsoft/USBuildingFootprints
# WARNING: these files can be pretty big. using arizona for its copious subdivisions and reasoanable 83MB.
url_footprint <- "https://usbuildingdata.blob.core.windows.net/usbuildings-v1-1/Arizona.zip"
download.file(url_footprint, "Arizona.zip")
unzip("Arizona.zip")
# read in building shapes
buildings <- st_read("Arizona.geojson")
# use the tigris package to get zctas boundaries (basically zip codes)
options(tigris_class = "sf", tigris_use_cache = TRUE) # defaults to sp; manually set to sf
zctas_national <- zctas(cb = TRUE)
# get the geometry for the zip codes we want to map
selected_zip <- zctas_national %>%
# input the zipcode(s) you want here. using 85308 for glendale, az
filter(GEOID10 %in% c(85308)) %>%
st_transform(st_crs(buildings))
# clip buildings file to just the selected zipcode
building_select <- st_intersection(selected_zip, buildings)
# plot using using geom_sf
glendale <- ggplot() +
geom_sf(data = building_select, fill = "black", color = NA) +
theme_void() +
# manually dropping the grid to solve a bug in geom_sf
theme(panel.grid.major = element_line(colour = 'transparent'))
ggsave("glendale_az.png", glendale, dpi = 400, width = 8, height = 5)
@azadag
Copy link

azadag commented Oct 28, 2018

Do you have a census API key? tigris may require that...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment