r5r
r5r copied to clipboard
Smooth interpolation
I have gone through the isochrone vignette and I want to transform the resulting grid to a raster object and eventually to contours. The problem is that the resulting grid is far too coarse to be of any use, do you have any suggestions on how to make a finer grid?
Here's a reprex:
`options(java.parameters = "-Xmx2G")
library(r5r) library(sf) library(data.table) library(ggplot2) library(interp) library(dplyr) library(terra) library(stars) library(patchwork)
data_path <- system.file("extdata/poa", package = "r5r")
r5r_core <- setup_r5(data_path, verbose = FALSE)
points <- fread(file.path(data_path, "poa_hexgrid.csv"))
subset point with the geolocation of the central bus station
central_bus_stn <- points[291,]
routing inputs
mode <- c("WALK", "TRANSIT") max_walk_dist <- 1000 # in meters max_trip_duration <- 120 # in minutes departure_datetime <- as.POSIXct("13-05-2019 14:00:00", format = "%d-%m-%Y %H:%M:%S")
time_window <- 120 # in minutes percentiles <- 50
calculate travel time matrix
ttm <- travel_time_matrix(r5r_core, origins = central_bus_stn, destinations = points, mode = mode, departure_datetime = departure_datetime, max_trip_duration = max_trip_duration, time_window = time_window, percentiles = percentiles, verbose = FALSE)
head(ttm)
street_net <- street_network_to_sf(r5r_core)
add coordinates of destinations to travel time matrix
ttm[points, on=c('to_id' ='id'), :=
(lon = i.lon, lat = i.lat)]
interpolate estimates to get spatially smooth result
travel_times.interp <- with(na.omit(ttm), interp::interp(lon, lat, travel_time_p50)) %>% with(cbind(travel_time=as.vector(z), # Column-major order x=rep(x, times=length(y)), y=rep(y, each=length(x)))) %>% as.data.frame() %>% na.omit()
head(travel_times.interp)
transform xyz grid to raster
r <- travel_times.interp[, c('x', 'y', 'travel_time')] |> rast(crs = 'epsg:4326')
find isochrone's bounding box to crop the map below
bb_x <- c(min(travel_times.interp$x), max(travel_times.interp$x)) bb_y <- c(min(travel_times.interp$y), max(travel_times.interp$y))
plot
rasterized <- ggplot() + geom_stars(data = st_as_stars(r)) + geom_sf(data = street_net$edges, color = "gray55", size=0.1, alpha = 0.7) + geom_point(aes(x=lon, y=lat, color='Central bus\nstation'), data=central_bus_stn) + scale_fill_viridis_c(direction = -1, option = 'B') + scale_color_manual(values=c('Central bus\nstation'='black')) + scale_x_continuous(expand=c(0,0)) + scale_y_continuous(expand=c(0,0)) + coord_sf(xlim = bb_x, ylim = bb_y) + labs(fill = "travel time (minutes)", color=NULL, title = 'Rasterized map') + theme_minimal() + theme(axis.title = element_blank())
original <- ggplot(travel_times.interp) + geom_contour_filled(aes(x=x, y=y, z=travel_time), alpha=.8) + geom_sf(data = street_net$edges, color = "gray55", size=0.1, alpha = 0.7) + geom_point(aes(x=lon, y=lat, color='Central bus\nstation'), data=central_bus_stn) + scale_fill_viridis_d(direction = -1, option = 'B') + scale_color_manual(values=c('Central bus\nstation'='black')) + scale_x_continuous(expand=c(0,0)) + scale_y_continuous(expand=c(0,0)) + coord_sf(xlim = bb_x, ylim = bb_y) + labs(fill = "travel time (minutes)", color=NULL, title = 'Original map') + theme_minimal() + theme(axis.title = element_blank())
original +
rasterized +
patchwork::plot_layout(guides = 'collect')
`
Hi @ACalleros. If you would like to generate a travel-time matrix at a finer spatial resolution, there various ways to do that . I would suggest having a look at the sf::st_make_grid
function. Users can set as many origin and destinations points as they see fit for their purposes.
Now, if your question is more targetted towards how to create a raster grid with finer resolution, I wouldn't be able to help but I'm sure there is information on this on StackOverflow.
The problem raised here seems to be "how to make a finer grid?". Not really an r5r issue and there is plenty of tutorials about this on the internet. Closing this issue for now