Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

detailed_itineraires for multiple origins #224

Closed
franciscopasqual opened this issue Feb 4, 2022 · 9 comments
Closed

detailed_itineraires for multiple origins #224

franciscopasqual opened this issue Feb 4, 2022 · 9 comments

Comments

@franciscopasqual
Copy link

The detailed_itineraires function works only for one single origin at a time, and I need to obtain results (actually just the distances) for a large number of origins. I wasn't successful on automating the function using "for", because at every new loop the output overwrites the last one... Any suggestions on how can I change the "det" for every new loop?

for (i in 1:10) {

det <- detailed_itineraries(r5r_core = r5r_core,
origins = origins[i,],
destinations = destinations,
mode = mode,
departure_datetime = departure_datetime,
max_walk_dist = max_walk_dist,
max_trip_duration = max_trip_duration,
shortest_path = FALSE)
}

@xtimbeau
Copy link

xtimbeau commented Feb 4, 2022 via email

@mvpsaraiva
Copy link
Collaborator

Thanks @xtimbeau for helping out. I definitely suggest using solution 2... do a cross product of origins and destinations, and let r5r run it all in parallel. You can use the function @rafapereirabr suggested in issue #61 to do that.

@franciscopasqual
Copy link
Author

Thanks for the suggestions! I managed to create the cross product of all pairs but still don't know how to use that to run detailed_itineraires function, since it asks for different origins and destinations files.

det <- detailed_itineraries(r5r_core = r5r_core,
origins = df # not sure what to put in here
destinations = df # same
mode = mode,
departure_datetime = departure_datetime,
max_walk_dist = max_walk_dist,
max_trip_duration = max_trip_duration,
shortest_path = FALSE)

@rafapereirabr
Copy link
Member

please follow the example below:

library(r5r)
library(data.table)
library(dplyr)

# build transport network
data_path <- system.file("extdata/poa", package = "r5r")
r5r_core <- setup_r5(data_path = data_path, temp_dir = TRUE)


# Read points
origins <- read.csv(file.path(data_path, "poa_points_of_interest.csv"))[1:4,]
destinations <- read.csv(file.path(data_path, "poa_points_of_interest.csv"))[3:10,]

# get all possible combinations between origins and destinations
# function
get_all_od_combinations <- function(origins, destinations){
  
  # all possible id combinations
  base <- expand.grid(origins$id, destinations$id)
  
  # rename df
  setDT(base)
  setnames(base, 'Var1', 'idorig')
  setnames(base, 'Var2', 'iddest')
  
  # bring spatial coordinates from origin and destination
  base[origins, on=c('idorig'='id'), c('lon_orig', 'lat_orig') := list(i.lon, i.lat)]
  base[destinations, on=c('iddest'='id'), c('lon_dest', 'lat_dest') := list(i.lon, i.lat)]
  
  return(base)
}

df <- get_all_od_combinations(origins, destinations)

# select/rename columns for r5r input
all_orig <- dplyr::select(df, c('id'=idorig, 'lon'=lon_orig,'lat'=lat_orig))
all_dest <- dplyr::select(df, c('id'=iddest, 'lon'=lon_dest,'lat'=lat_dest))


# run r5r
departure_datetime <- as.POSIXct("13-05-2019 14:00:00", format = "%d-%m-%Y %H:%M:%S")
dit <- detailed_itineraries(r5r_core,
                            origins = all_orig,
                            destinations = all_dest,
                            mode = c("WALK", "TRANSIT"),
                            departure_datetime = departure_datetime,
                            max_walk_dist = 1000,
                            max_trip_duration = 30)

@franciscopasqual
Copy link
Author

This worked out perfectly! I didn't realize that the function works for multiple origins at a time if you organize them that way.

Thanks a lot @rafapereirabr!

@Torskelgen
Copy link

Is there any way to make your approach work with different departure times for each OD pair, @rafapereirabr, or do I have to do a loop?

@mvpsaraiva
Copy link
Collaborator

Is there any way to make your approach work with different departure times for each OD pair, @rafapereirabr, or do I have to do a loop?

Hi @Torskelgen. Currently, it's not possible. This is a good idea, and it's relatively simple to implement. We'll add this feature in the next release of the package.

@Torskelgen
Copy link

That would be really neat. Thank you for your really great work with this package!

@rafapereirabr
Copy link
Member

The dev version of the detailed_itineraries() function now has a new parameter all_to_all, which allows users to set whether they want to query routes between all origins to all destinations (all_to_all = TRUE) or to query. By default (FALSE), the functions queries routes between the the 1st row of origins to the 1st row of destinations, then the 2nd row of origins to the 2nd row of destinations, and so on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants