TransWikia.com

Calculate traveling time from several points to a same target, for different means of transport

Geographic Information Systems Asked by ccamara on April 18, 2021

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).

I know openroutservice has an R package which has a function, ors_matrix(), which according to official documentation, generates:

One to many, many to many or many to one: ors_matrix() allows you to
obtain aggregated time and distance information between a set of
locations (origins and destinations). Unlike ors_directions() it does
not return detailed route information. But you may still specify the
transportation mode and compute routes which adhere to certain
restrictions, such as avoiding specific road types or object
characteristics.

That seems to be exactly what I am looking for!

If I follow the description, I get pretty interesting results:

library(openrouteservice)
# openrouteservice::ors_api_key("<my_api_key>")

from <- list(c(-2.885882, 43.291695), c(-2.885882, 43.296608))
to <- c(-2.88276, 43.29196)

res <- ors_matrix(from)

> res$durations
       [,1]   [,2]
[1,]   0.00 142.12
[2,] 138.86   0.00

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 or any other way to achieve what I am looking for?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP