Created
October 14, 2018 16:23
-
-
Save etachov/a029c849c89ee3d4f7d59dda83f97ed2 to your computer and use it in GitHub Desktop.
Making NYT-style building maps with data from Microsoft
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
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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you have a census API key? tigris may require that...