[R-sig-Geo] help needed regarding package OSRM

Timothée Giraud timothee.giraud at cnrs.fr
Wed Nov 8 14:13:38 CET 2017


Hi,

osrmRoute() requests a route between 2 points, it can return a 
SpatialLinesDataFrame of the route with travel time (minutes) and travel 
distance (kilometers) in its data slot.

osrmTable() requests a travel time matrix between a set of points, it 
can return a matrix in minutes between sources and destinations.

The OSRM server cannot return a travel distance matrix (in km).

Thus, the only solution I see for now is to use a loop (or apply or 
whatever) with osrmRoute() :

devtools::install_github("rCarto/osrm")

library(osrm)
data("com")
mat <- data.frame(i = character(), j = character (),
                   time = numeric(), distance = numeric(),
                   stringsAsFactors = F)
k = 1
for (i in 1:2){
   for (j in 6:8){
     rout <- osrmRoute(src = com[i, c("comm_id", "lon","lat")],
                       dst = com[j, c("comm_id", "lon","lat")], sp = T)
     mat[k, ] <- as.vector(rout at data[1,])
     k <- k + 1
     Sys.sleep(0.5)
   }
}
mat

Note that I use the github version of the package. This version is 
adapted to last changes made on the demo server.

Note also that I use a Sys.sleep(0.5)instruction in the loop to be 
gentle with the public OSRM demo server.

If you use your own OSRM server, the CRAN version will work and the 
Sys.sleep() instruction is useless.


Timothée Giraud



More information about the R-sig-Geo mailing list