Skip to content

Instantly share code, notes, and snippets.

@dfsnow
Created April 22, 2024 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfsnow/f2a2cee82fb71d1b7e2b5998f9237024 to your computer and use it in GitHub Desktop.
Save dfsnow/f2a2cee82fb71d1b7e2b5998f9237024 to your computer and use it in GitHub Desktop.
Testing out r5r's routing and travel time matrix functions
# Increase Java memory
options(java.parameters = "-Xmx10G")
# Load libraries
library(data.table)
library(dplyr)
library(ggplot2)
library(ggspatial)
library(osmdata)
library(osmextract)
library(r5r)
library(sf)
library(tigris)
# Populate an input/ dir with a .pbf file. For Chicago:
# wget --no-use-server-timestamps -O input/chicago.osm https://overpass-api.de/api/map?bbox=-87.8558,41.6229,-87.5085,42.0488
# osmium cat input/chicago.osm -o input/chicago.osm.pbf
r5r_core <- setup_r5(data_path = "input/")
# Setup some input parameters for a route
mode <- "BICYCLE"
departure_datetime <- Sys.time()
origin <- data.frame(
id = "Trader Joe's",
lat = 41.95085364900005,
lon = -87.67506868730602
)
dest <- data.frame(
id = "Aldi",
lat = 41.96381358872797,
lon = -87.65687040135536
)
# Create a single detailed route and corresponding map
route_single_df <- detailed_itineraries(
r5r_core = r5r_core,
origins = origin,
destinations = dest,
departure_datetime = departure_datetime,
mode = mode,
max_lts = 1,
shortest_path = TRUE,
progress = TRUE
)
ggplot() +
ggspatial::annotation_map_tile(zoomin = -1, type = "cartolight") +
geom_sf(data = route_single_df, color = "blue")
# Load all 2020 Census block centroids and clip them to the Chicago boundary
blocks_gdf <- tigris::blocks(state = "IL", county = "Cook", year = 2020) %>%
st_centroid()
chicago_gdf <- tigris::places(state = "IL", year = 2020) %>%
filter(NAME == "Chicago")
blocks_gdf_clipped <- blocks_gdf %>%
st_intersection(chicago_gdf) %>%
st_transform(4326)
blocks_df <- blocks_gdf_clipped %>%
st_coordinates() %>%
as.data.frame() %>%
setNames(c("lon", "lat")) %>%
mutate(id = blocks_gdf_clipped$GEOID20)
# Route from 2 origins to a sample of Chicago Census block centroids
# Takes ~580s for 20K OD pairs w 60 min cutoff on an M2 Macbook Air
tictoc::tic()
route_multi_df <- detailed_itineraries(
r5r_core = r5r_core,
origins = rbind(origin, dest),
destinations = blocks_df %>% slice_sample(n = 1000),
departure_datetime = departure_datetime,
mode = mode,
max_bike_time = 60,
max_lts = 1,
all_to_all = TRUE,
shortest_path = TRUE,
progress = TRUE
)
tictoc::toc()
ggplot() +
ggspatial::annotation_map_tile(zoom = 14, type = "cartolight") +
geom_sf(data = route_multi_df, aes(color = from_id), alpha = 0.5)
# Route from 2 origins to a sample of Chicago Census block centroids
# Takes ~1.2s for 20K OD pairs w 60 min cutoff on an M2 Macbook Air
tictoc::tic()
travel_time_df <- travel_time_matrix(
r5r_core = r5r_core,
origins = rbind(origin, dest),
destinations = blocks_df,
departure_datetime = departure_datetime,
mode = mode,
max_bike_time = 60,
max_lts = 1,
progress = TRUE
)
tictoc::toc()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment