Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save elipousson/62724c3b7055c18002fa0d49cf9f4f13 to your computer and use it in GitHub Desktop.
Save elipousson/62724c3b7055c18002fa0d49cf9f4f13 to your computer and use it in GitHub Desktop.
Script for prepping location zoning maps
# Get zoning metadata from {mapmaryland}
baltimore_zoning_info <- dplyr::filter(
mapmaryland::md_zoning_info,
county == stringr::str_remove(map_params$county_name, ", Maryland$")
)
# Create format function with custom colors ----
zoning_colors <- tibble::tribble(
~label, ~hex,
"EC-1", "006ab6",
"EC-2", "006ab6",
"H", "006ab6",
"CBCA", "4d8844",
"OS", "4d8844",
"R-10", "998439",
"OIC", "ADD8E6",
"OR-1", "ADD8E6",
"C-4", "b12a2f",
"C-5", "b12a2f",
"R-9", "b49d34",
"IMU-1", "c0188c",
"IMU-2", "c0188c",
"I-2", "d589ba",
"C-2", "da2028",
"C-3", "da2028",
"I-1", "e1c3dd",
"R-7", "ebd417",
"R-8", "ebd417",
"C-1", "fbc8b3",
"C-1-E", "fbc8b3",
"C-1-VC", "fbc8b3",
"R-5", "fff100",
"R-6", "fff100",
"R-2", "fff68f",
"R-3", "fff68f",
"R-4", "fff68f",
"R-1", "fffaca",
"R-1-A", "fffaca",
"R-1-B", "fffaca",
"R-1-C", "fffaca",
"R-1-D", "fffaca",
"R-1-E", "fffaca",
"OR-2", "0000FF"
)
zoning_colors$label_fill <- paste0("#", zoning_colors$hex)
zoning_colors$text_color <- gtExtras:::ideal_fgnd_color(zoning_colors$label_fill)
zoning_colors$line_color <- gtExtras:::ideal_fgnd_color(
zoning_colors$label_fill,
light = "gray90",
dark = "gray10"
)
fmt_zoning_data <- function(x,
by = NULL,
na_hex = "ffebcd",
na_text = "gray10") {
x %>%
mutate(
type = stringr::str_extract(label, "[:alpha:]+"),
# FIXME: Working to get color types to the letter and alpha tied to
# the number
type = stringr::str_extract(label, "[:alpha:]+"),
level = stringr::str_extract(label, "[1-9]+"),
) |>
left_join(zoning_colors, by = by) |>
tidyr::replace_na(
list(
"hex" = na_hex,
"line_color" = na_text,
"text_color" = na_text
)
)
}
baltimore_zoning_info <- baltimore_zoning_info |>
rename(
label = zoning_alt
) |>
fmt_zoning_data()
make_location_zoning_map <- function(location,
asp = 8.5 / 11,
default_orientation = "portrait",
dist = 0.1,
unit = "mi",
style_url = "mapbox://styles/elipousson-baltimorecity/clmjue49z046y01ns5tu95ovw") {
location_bbox <- st_bbox_ext(location)
orientation <- sf_bbox_orientation(location_bbox)
asp <- case_when(
sf_bbox_orientation(location_bbox) == default_orientation ~ asp,
sf_bbox_orientation(location_bbox) != "square" ~ 1 / asp,
.default = asp
)
location_area <- sfext::st_bbox_ext(
location,
asp = asp,
dist = dist,
unit = unit
)
# Get zoning data
location_zoning <- get_location_data(
data = mapbaltimore::zoning,
location = location_area
)
# Make grouped geometry by label
location_zoning_label <- get_location_data(
data = location_zoning,
location = location_area,
fn = ~ .x %>%
dplyr::group_by(label) %>%
dplyr::summarize()
)
location_zoning_map_data <- fmt_zoning_data(location_zoning_label)
# zoning_map_area <- sfext::st_bbox_ext(
# location,
# dist = dist,
# unit = unit,
# asp = asp
# )
location_zoning_map <- maplayer::make_mapbox_map(
save = TRUE,
data = location_area,
ggsave_params = list(
name = location$name,
fileext = "pdf",
paper = "letter",
orientation = if_else(orientation == "square",
default_orientation,
orientation)
),
style_url = style_url,
layer = layer_location_data(
# location_zoning_label created by get_mapbaltimore_data.R
data = location_zoning_map_data,
mapping = aes(
fill = .data[["label_fill"]],
color = .data[["line_color"]]
),
linewidth = 0.15,
alpha = 0.35
),
fg_layer = list(
layer_location(data = location),
maplayer::layer_labelled(
data = location_zoning_map_data,
location = location_area,
# location = st_erase(
# zoning_map_area,
# plan_area
# ),
mapping = aes(
fill = .data[["label_fill"]],
color = .data[["text_color"]]
),
label_col = "label",
geom = "sf_label"
)
),
addon = list(
# FIXME: Consider if inconsistent naming between
# layer_location_data_basemap and layer_location_labelled is confusing.
ggplot2::guides(fill = "none", color = "none"),
scale_fill_identity(),
scale_color_identity(),
maplayer:::theme_background(color = NA),
labs(title = location$name)
),
scale = 1,
scaling_factor = "2x",
expand = FALSE,
color = NA
)
}
walk(
planning_districts[["name"]],
\(x) {
make_location_zoning_map(
filter(planning_districts, name == x)
)
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment