I am trying to use osrmTable
to calculate driving distances, but I am encountering incorrect results under certain circumstances. It took me a lot of trial and error to (I think) identify when the error happens, but I think I have figured it out (though not the solution) - see below. First, say we have the following data and run osrmTable
(using the demo server):
library(osrm)
library(sf)
a <- data.frame(
lon=c(-98,-92),
lat=c(38,38))
b <- data.frame(
lon=c(-68,-109),
lat=c(38,34))
a <- st_as_sf(a, coords = c("lon", "lat"), crs = "WGS84")
b <- st_as_sf(b, coords = c("lon", "lat"), crs = "WGS84")
osrmTable(src = a, dst = b, measure=c("duration", "distance"))
This produces:
$durations
1 2
1 0 0
2 0 0
$distances
1 2
1 0 0
2 0 0
$sources
lon lat
1 -9.497727 38.78069
2 -9.497727 38.78069
$destinations
lon lat
1 -9.497727 38.78069
2 -9.497727 38.78069
This is nonsensical - the durations and distances are all 0, and the sources and destinations are nowhere remotely close to the actual ones.
But if I then make only one very small change - instead of the lat
being both 38, I change the second to be 38.01 - I get correct (and dramatically different) results.
c <- data.frame(
lon=c(-98,-92),
lat=c(38,38.01))
d <- data.frame(
lon=c(-68,-109),
lat=c(38,34))
c <- st_as_sf(c, coords = c("lon", "lat"), crs = "WGS84")
d <- st_as_sf(d, coords = c("lon", "lat"), crs = "WGS84")
osrmTable(src = c, dst = d, measure=c("duration", "distance"))
Results:
$durations
1 2
1 2078.6 971.8
2 1732.5 1319.3
$distances
1 2
1 2769379 1236686
2 2250736 1829854
$sources
lon lat
1 -98.00000 37.99923
2 -92.00317 38.01000
$destinations
lon lat
1 -69.97288 41.24970
2 -108.99029 33.99603
These results make sense. Again, the only thing that changed was that I made the latitudes slightly different in the a
data frame.
What is causing this odd behavior, and is there a way to achieve correct results besides changing the coordinates to be slightly different?