Skip to content

Instantly share code, notes, and snippets.

@long39ng
Created September 30, 2021 20:28
Show Gist options
  • Save long39ng/ee1e2c9fc544870378cfe3e67a1eee65 to your computer and use it in GitHub Desktop.
Save long39ng/ee1e2c9fc544870378cfe3e67a1eee65 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(osmdata)
library(sf)
# Define functions --------------------------------------------------------
query_streets <- function(place) {
query <- getbb(place) |>
opq()
highways <- query |>
add_osm_feature("highway", c("motorway", "trunk", "primary", "secondary", "tertiary")) |>
osmdata_sf() |>
pluck("osm_lines")
streets <- query |>
add_osm_feature("highway", c("unclassified", "residential", "living_street")) |>
osmdata_sf() |>
pluck("osm_lines")
coords <- tidygeocoder::geo(place, return_addresses = FALSE, min_time = 0) |>
mutate(
across(everything(), round, 2),
lat = if_else(lat > 0, paste0(abs(lat), "°N"), paste0(abs(lat), "°S")),
long = if_else(long > 0, paste0(abs(long), "°E"), paste0(abs(long), "°W"))
) |>
t() |>
paste0(collapse = " · ")
list(place = place, highways = highways, streets = streets, coords = coords)
}
create_map <- function(street_list, title = TRUE, caption = TRUE, scale = 1,
width = 8.3, height = 11.7) {
colours <- list(
bg = "#0b0e14",
fg = "#95e6cb",
fg2 = colorspace::darken("#95e6cb", .2)
)
bbox <- map2_dbl(
st_bbox(street_list$highways),
st_bbox(street_list$streets),
max
)
xrange <- quantile(
c(bbox[["xmin"]], bbox[["xmax"]]),
probs = seq(0, 1, .05)
)[c(2, 20)]
yrange <- c(bbox[["ymin"]], bbox[["ymax"]])
p <- ggplot() +
geom_sf(data = street_list$streets, size = .3 * scale, colour = colours$fg2) +
geom_sf(data = street_list$highways, size = .45 * scale, colour = colours$fg) +
coord_sf(xlim = xrange, ylim = c(NA, yrange[[2]])) +
theme_void() +
theme(
plot.background = element_rect(fill = colours$bg, colour = NA),
plot.margin = margin(60, 60, 70, 60)
)
if (title) {
p <- p +
annotate("text",
x = mean(xrange),
y = yrange[[1]] - (yrange[[2]] - yrange[[1]]) / 7,
label = paste(
paste0(rep("―", 2), collapse = ""),
" ", toupper(street_list$place), " ",
paste0(rep("―", 2), collapse = "")
),
family = "Metropolis", fontface = "bold", size = 10 * scale, colour = colours$fg
)
}
if (caption) {
p <- p +
annotate("text",
x = mean(xrange),
y = yrange[[1]] - (yrange[[2]] - yrange[[1]]) / 4.5,
label = street_list$coords,
family = "Inter", size = 9 * scale, colour = colours$fg2
)
}
cairo_pdf(
paste0("city-maps/", str_replace_all(tolower(street_list$place), " |,", "-"), ".pdf"),
width = width,
height = height,
bg = colours$bg,
antialias = "subpixel"
)
print(p)
dev.off()
}
# Use defined functions to create maps ------------------------------------
streets <- query_streets("Bielefeld")
# saveRDS(streets, here::here("city-maps/bi_streets.RDS"))
# streets <- readRDS(here::here("city-maps/bi_streets.RDS"))
create_map(streets)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment