Calculate the travel time on foot between two coordinates with R and OpenStreetMap (OSM)

142 views Asked by At

I'm trying to calculate the time on foot by two coordinates with OpenStreetMap.

I'm using this dataset: https://github.com/JayScribes/Geocode-Distance/blob/main/NYC%20Bike%20Share%20Data.csv

And this is my code:

library(readr)
NYC_Bike_Share_Data <- read_csv("NYC Bike Share Data.csv")
View(NYC_Bike_Share_Data)

library(osrm)
osrm_conn <- osrm::osrmTable(profile = "foot-walking")
 
origen<-c(NYC_Bike_Share_Data$ss_lat,NYC_Bike_Share_Data$sslong)
destino<-c(NYC_Bike_Share_Data$es_lat,NYC_Bike_Share_Data$es_long)


duracion <- osrmTable(osrm_conn, loc = c(origen, destino))$durations[1]

Output:

Error: "loc" should be a data.frame or a matrix of coordinates, an sfc POINT object or an sf POINT object.
1

There are 1 answers

2
Dave2e On

Your definitions are incorrect, you are making vectors and not data frames/matrixes using the c() function.
Also, it is convention to list longitude as the first column followed by latitude.

Note: orsm limits calls to less than 1000 points, so you will need to subdivide your dataset.

The following should work for you:

NYC_Bike_Share_Data <- read.csv("NYC Bike Share Data.csv")

library(osrm)

origen<-NYC_Bike_Share_Data[ ,c("ss_long", "ss_lat")]
destino<-NYC_Bike_Share_Data[ ,c("es_long", "es_lat")]

duracion <- osrmTable(src = origen, dst =destino,  osrm_profile = "foot", measure = "duration")

This is resulting is a matrix instead of a single output value for each row. Also the matrix is asymmetric thus going from A to B is different than going from B to A. I am not sure if this is expected. You will need to extract the diagonal from this matrix for your final answer.

diag(duracion$durations)