Created
November 8, 2022 19:57
-
-
Save walkerke/362c255e9f21273e1d92423b578a2775 to your computer and use it in GitHub Desktop.
This file contains hidden or 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(mapboxapi) | |
library(tidyverse) | |
library(sf) | |
library(patchwork) | |
city_halls <- c( | |
"New York" = "City Hall Park, New York, NY 10007", | |
"Los Angeles" = "200 N Spring St, Los Angeles, CA 90012", | |
"Chicago" = "121 N La Salle St, Chicago, IL 60602", | |
"Houston" = "901 Bagby St, Houston, TX 77002", | |
"Phoenix" = "200 W Washington St, Phoenix, AZ 85003", | |
"Philadelphia" = "1400 John F Kennedy Blvd, Philadelphia, PA 19107", | |
"San Antonio" = "100 Military Plaza #4, San Antonio, TX 78205", | |
"San Diego" = "Civic Ctr Plz, San Diego, CA 92101", | |
"Dallas" = "1500 Marilla St, Dallas, TX 75201", | |
"San Jose" = "200 E Santa Clara St, San Jose, CA 95113" | |
) | |
# Build out the mapping workflow | |
plots <- imap(city_halls, ~{ | |
print(glue::glue("Working on {.y}...")) | |
# Geocode the hall | |
hall_sf <- mb_geocode(.x, output = "sf") | |
# 500m buffer for the hall - | |
# projected coordinates get smoother results, so | |
# use crsuggest for this | |
my_crs <- crsuggest::suggest_top_crs(hall_sf) | |
buf <- hall_sf %>% | |
st_transform(my_crs) %>% | |
st_buffer(500, nQuadSegs = 1000) %>% | |
st_transform(4326) | |
# Query the tiles | |
vector_extract <- get_vector_tiles( | |
tileset_id = "mapbox.mapbox-streets-v8", | |
location = buf, | |
zoom = 15 | |
) | |
# Intersect the buildings with the buffer | |
buildings_extract <- vector_extract$building %>% | |
st_intersection(buf) | |
# Plot the result | |
ggplot() + | |
geom_sf(data = buf, color = "black", fill = "white") + | |
geom_sf(data = buildings_extract, fill = "navy", color = "navy") + | |
theme_void() + | |
labs(title = .y) + | |
theme(plot.title = element_text(hjust = 0.5)) | |
}) | |
wrap_plots(plots, ncol = 5) + | |
plot_annotation(caption = "Building footprints within approximately 500m of respective city halls.\nGraphic by @kyle_e_walker; data © Mapbox, OpenStreetMap contributors") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment