Skip to content

Instantly share code, notes, and snippets.

@marcosci
Created May 10, 2023 12:36
Show Gist options
  • Save marcosci/2a26e936c007e58493bf5fc8a2c25209 to your computer and use it in GitHub Desktop.
Save marcosci/2a26e936c007e58493bf5fc8a2c25209 to your computer and use it in GitHub Desktop.
street_orientation
# load libraries
library(tidyverse)
library(osmdata)
library(sf)
library(ggtext)
library(exactextractr)
library(fasterize)
library(ggtext)
library(tidygeocoder)
library(glue)
library(purrr)
library(geosphere)
library(classInt)
library(patchwork)
library(glue)
sf::sf_use_s2(TRUE)
#epsg <- 32632
epsg <- 4326
country <- 'germany'
aoi_citys <- c(#'Freiburg im Breisgau, Germany',
'München, Germany',
'Berlin, Germany',
'Erfurt, Germany',
'Dresden, Germany',
'Göttingen, Germany',
'Kiel, Germany',
'Hannover, Germany',
'Essen, Germany',
'Koblenz, Germany',
'Greifswald, Germany',
'Trier, Germany',
'Kaiserslautern, Germany',
'Nürnberg, Germany',
'Köln, Germany',
'Regensburg, Germany',
'Heidelberg, Germany',
'Rothenburg ob der Tauber, Germany',
'Bamberg, Germany',
'Bremen, Germany',
'Frankfurt, Germany',
'Schwerin, Germany',
'Potsdam, Germany',
'Bonn, Germany',
'Bacharach, Germany',
'Tübingen, Germany',
'Marburg, Germany',
'Baden-Baden, Germany',
'Stuttgart, Germany',
'Sankt Goar, Germany',
'Fritzlar, Germany',
'Mittenwald, Germany',
'Goslar, Germany',
'Dortmund, Germany',
'Düsseldorf, Germany',
'Leipzig, Germany',
'Duisburg, Germany',
'Düsseldorf, Germany',
'Dortmund, Germany',
'Kassel, Germany',
'Erfenbach, Germany',
'Hamburg, Germany')
walk(aoi_citys, function(aoi_city){
city <- tidygeocoder::geo(aoi_city) %>%
st_as_sf(coords = c('long', 'lat'), crs = 4326) %>%
st_transform(32632)
# streets data
big_streets <- getbb(aoi_city)%>%
opq(timeout = 100)%>%
add_osm_feature(key = 'highway',
value = c('motorway', 'primary', 'motorway_link', 'primary_link')) %>%
osmdata_sf()
big_streets <- big_streets$osm_lines %>%
st_transform(epsg)
med_streets <- getbb(aoi_city)%>%
opq(timeout = 100)%>%
add_osm_feature(key = 'highway',
value = c('secondary', 'tertiary', 'secondary_link', 'tertiary_link')) %>%
osmdata_sf()
med_streets <- med_streets$osm_lines %>%
st_transform(epsg)
small_streets <- getbb(aoi_city)%>%
opq(timeout = 100)%>%
add_osm_feature(key = 'highway',
value = c('residential', 'living_street',
'unclassified',
'service', 'footway'
)) %>%
osmdata_sf()
small_streets <- small_streets$osm_lines %>%
st_transform(epsg)
crop_buffer <- st_buffer(city, 7500)%>%
st_transform(epsg)
small_streets_crop <- st_crop(small_streets, crop_buffer)
small_streets_crop<- st_intersection(small_streets_crop, crop_buffer)
med_streets_crop <- st_crop(med_streets, crop_buffer)
med_streets_crop <- st_intersection(med_streets_crop, crop_buffer)
big_streets_crop <- st_crop(big_streets, crop_buffer)
big_streets_crop <- st_intersection(big_streets_crop, crop_buffer)
street_plot <- ggplot() +
geom_sf(data = small_streets_crop, color = '#e7bfbf', lwd = .25, alpha = 0.8) +
geom_sf(data = med_streets_crop, color = '#fbabab', lwd = .75, alpha = 0.6) +
geom_sf(data = big_streets_crop, color = '#f74d4d', alpha = 0.4) +
theme_void()
street_bearing_big <- stplanr::line_bearing(big_streets_crop)
street_bearing_med <- stplanr::line_bearing(med_streets_crop)
street_bearing_sma <- stplanr::line_bearing(small_streets_crop)
street_bearing <- tibble(orientation = c(street_bearing_big,
street_bearing_med,
street_bearing_sma))
street_bearing <- street_bearing |>
mutate(course = (orientation + 360) %% 360,
class_b = cut(orientation, seq(-180, 180, 15)),
class_c = cut(course, seq(0, 360, 15))) |>
group_by(class_c) |>
summarise(count = n()) |>
drop_na()
orientation_plot <- street_bearing |>
ggplot(aes(x = class_c, y = count)) +
geom_col(fill = '#fbabab') +
coord_polar(start = 12.45) +
theme_void() +
theme(plot.margin=grid::unit(c(0,0,0,0), 'mm'))
street_plot + orientation_plot +
plot_annotation(title = glue('{aoi_city}')) &
theme(plot.title.position = 'plot',
plot.title = element_markdown(size = 32,
family = 'West',
face = 'bold',
colour = '#4e4545',
hjust = 0.5,
margin = margin(.3,0,.5,-2, 'cm')),
plot.caption = element_markdown(size = 12,
colour = '#4e4545',
hjust = 1))
ggsave(glue('plots/{aoi_city}.png'), width = 12, height = 12, dpi = 600)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment