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

Calculate traveling times from many to one #67

Open
ccamara opened this issue Nov 21, 2020 · 4 comments
Open

Calculate traveling times from many to one #67

ccamara opened this issue Nov 21, 2020 · 4 comments
Assignees
Labels
question Further information is requested

Comments

@ccamara
Copy link

ccamara commented Nov 21, 2020

Provided the following dataframe in which I have several source locations and destination (same destination for all, in this case):

lon_from <- c(2.885882, -2.885882, -2.885521, -2.887446, -2.885882),
lat_from <- c(43.291695, 43.291695, 43.296608, 43.298549, 43.291695),
lon_to <- rep(-2.885882, 5),
lat_to <- rep(43.297457, 5)

df2 <- data.frame(lon_from, lat_from, lon_to, lat_to) 

> df2
   lon_from lat_from    lon_to   lat_to
1  2.885882 43.29169 -2.885882 43.29746
2 -2.885882 43.29169 -2.885882 43.29746
3 -2.885521 43.29661 -2.885882 43.29746
4 -2.887446 43.29855 -2.885882 43.29746
5 -2.885882 43.29169 -2.885882 43.29746
> 

I would like to calculate how long does it take from each source (c(df$lon, df$lat)) to their target destination (c(df$lon_to, df$lat_to)), according to different means of transport (let's say: car and walking).

Apparently, ors_matrix() is what I am looking for!

Unfortunately, I haven't been able to tame it for my case scenario. Currently, I am facing two problems:

  1. I need to feed it with a list, not a dataframe (although documentation specifies that I could use a dataframe, I haven't been able to do it).
  2. Distances are calculated from every observation to every other one. I haven't found the way to calculate the many to one option stated in the description.

So, here I am:close to what I am looking for, but stucked. Do you happen to know how to overcome these two problems? I have read the package's documentation, as well as the API documentation (https://openrouteservice.org/dev/#/api-docs/v2/matrix/{profile}/post) (which provides an extra argument called destination, which does not appear in the function's documentation), but haven't been able to figure it out.

@aoles aoles self-assigned this Nov 21, 2020
@aoles aoles added the question Further information is requested label Nov 21, 2020
@aoles
Copy link
Member

aoles commented Nov 21, 2020

Thanks for reaching out!

Both the source and the destination coordinates need to be provided in one data frame through the locations argument. The mapping is defined by sources and destinations arguments, as illustrated by the following example. Note that the location indices start from 0.

library(openrouteservice)

lon_from <- c(2.885882, -2.885882, -2.885521, -2.887446, -2.885882)
lat_from <- c(43.291695, 43.291695, 43.296608, 43.298549, 43.291695)
lon_to <- -2.885882
lat_to <- 43.297457

coordinates <- data.frame(lon = c(lon_from, lon_to), lat = c(lat_from, lat_to))

profiles <- c("car", "walking")

res <- sapply(profiles, function(profile) {
  ors_matrix(coordinates, sources = 0:4, destinations = 5, profile = ors_profile(profile))$durations
})

res
#>           car   walking
#> [1,] 21038.67 455247.06
#> [2,]   209.23    630.95
#> [3,]   111.75     78.87
#> [4,]   139.00    295.19
#> [5,]   209.23    630.95

Created on 2020-11-21 by the reprex package (v0.3.0)

@ccamara
Copy link
Author

ccamara commented Nov 21, 2020

Thank you for the explanation, @aoles ! It obviously works great.
As far as I see, ors_matrix() makes several assumptions (being that first column in dataframe contains longitude and second one latitude, no matter their names) and I could not find any information about sources and destinations. I will add a PR to improve documentation, if that's ok.

@aoles
Copy link
Member

aoles commented Nov 23, 2020

Thanks for your feedback!

You are right that currently column names do not influence the coordinate order which is fixed as longitude, latitude. I thought this was clear from ?ors_matrix, but if not improvements to documentation are welcome.

Re the sources anddestinations arguments: at the moment these are part of the ellipsis .... They are documented at https://openrouteservice.org/dev/#/api-docs/v2/matrix/{profile}/post referenced from the man page. I was actually thinking of including them as explicit arguments to ors_matrix in order to be able to use 1-based indexing rather that 0-based one, which I believe is more natural to the R users. So stay tuned!

@ccamara
Copy link
Author

ccamara commented Dec 4, 2020

Oh, I didn't see your answer! thank you very much, @aoles !

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

No branches or pull requests

2 participants