0

As written in the title I want to calculate the distance from all gas stations in my hometown to the two motorway accesses around here using the package osrm.

stations_ms contains Latitude & Longitude for the gas stations and highway_ms.df contains the lat & long for the motorway accesses.

Calculating the distance for just one row of my dataset is no problem, but I am not able to create a loop/function, which does it for every row.

Here is my code:

route4 <-  osrmRoute(src = c(stations_ms$longitude[1], stations_ms$latitude[1]), 
                    dst = highway_ms.df[1,],
                    overview = "FALSE")

for (i in 1:nrow(stations_ms)) {
 route[i] <- osrmRoute(src = c(stations_ms$longitude[i], stations_ms$latitude[i]),
                       dst = highway_ms.df[1,],
                       overwiew = "FALSE")
}
```

Maybe someone can help me :)
Gonny
  • 37
  • 5
  • You have `route[i]` to store distance and duration result. What is `route`? If you make `route` a list before the loop, such as `route <- list()`, you can store all of the results in that list: `route[[i]] <- osrmRoute(...)` ... also, you have "overwiew" instead of "overview"... and instead of a list, you could use a function from the `apply` family as an alternative...let me know if this is helpful... – Ben Dec 22 '20 at 15:01
  • If i understood correctly, with overview = "FALSE" I get the duration (min) and distance (km). The problem with a list is that it somehow only safes the duration. – Gonny Dec 22 '20 at 16:20
  • Please see answer below and let me know if helpful. This should give you both duration and distance. – Ben Dec 22 '20 at 16:47

1 Answers1

1

Here is a workable example that might be helpful.

The overview in osrmRoute has the following options:

"full", "simplified" or FALSE. Use "full" to return the detailed geometry, use "simplified" to return a simplified geometry, use FALSE to return only time and distance.

If you only want time and distance, using FALSE should work fine. My comment was in regard to spelling (had a "w" instead of a "v").

I made up some example data:

my_points <- data.frame(
  id = 1:3,
  longitude = c(13.4, 13.5, 13.3),
  latitude = c(52.4, 52.5, 52.3)
)

And wanted to find distances to a pharmacy in Berlin (using apotheke.df that comes with the osrm package). You could do:

library(osrm)

route <- list()
for (i in 1:nrow(my_points)) {
  route[[i]] <- osrmRoute(src = c(my_points$longitude[i], my_points$latitude[i]),
                          dst = apotheke.df[1,],
                          overview = FALSE)
}

This starts with an empty list called route. Then, we fill in each list element with both time and duration. The end result is the following list:

R> route

[[1]]
duration distance 
   20.56    11.77 

[[2]]
duration distance 
   17.38     7.63 

[[3]]
duration distance 
   33.12    27.45

Which can be converted to a matrix or data frame (in this case, I made a matrix):

R> do.call(rbind, route)

     duration distance
[1,]    20.56    11.77
[2,]    17.38     7.63
[3,]    33.12    27.45
Ben
  • 28,684
  • 5
  • 23
  • 45
  • yes, now it works ... Thank You! It seems as if i had some brackets missing, therefore i only got the duration into the list. – Gonny Dec 22 '20 at 17:23
  • Is it possible that the osrm package calculates the route wrong? for my second motorway access (7.55944 / 51.98906) the kilometers are way too high. For example the first gas station (51.90243 / 7.638070) according to osrm is 42 kilometers away, but in reality (google maps) it is only 18 km. – Gonny Dec 22 '20 at 17:35
  • Hmmm...not sure...I just tried this: `osrmRoute(src = c(7.55944, 51.98906), dst = c(7.638070, 51.90243), overview = "FALSE")` and it gave a distance of 17.86 km. Is it something you can reproduce with providing the raw coordinates in `osrmRoute`? – Ben Dec 22 '20 at 17:58
  • `route_test <- osrmRoute(src = c(7.638070, 51.90243), dst = c(7.55944, 51.98906), overview = FALSE)` the result is `> route_test duration distance 28.67 42.37` – Gonny Dec 22 '20 at 18:06
  • It appears that if you reverse `src` and `dst` you get remarkably different distances - at least for "car". If you include `osrm.profile = "bike"` or `osrm.profile = "foot"` it's the same. – Ben Dec 22 '20 at 19:07