Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pairwise distance matrix from OSRM? #121

Closed
jnwadiuko opened this issue Jun 2, 2023 · 2 comments
Closed

pairwise distance matrix from OSRM? #121

jnwadiuko opened this issue Jun 2, 2023 · 2 comments

Comments

@jnwadiuko
Copy link

Hopefully this makes sense.

I am trying to build a distance "matrix" from two vectors of points in a dataset using OSRM, using a set up similar to that of mapdist or gmapsdistance.

hs<-hs_sf%>%
  st_as_sf(crs=4326)

traveldistance<-osrmTable(
src=c(hs_sf$geometry.x),
dst = c(hs_sf$geometry.y),
measure = "duration",
osrm.profile = "car"
)

I am trying to find an output of 20 rows with one vector (distance) from an input of 20 rows with two vectors (POINTS from origin and destination). That is to say, ONE distance derived from the two points in each row. Instead, I am getting 20 vectors, which I take to be part of a complete matrix. Is there any way I could use OSRM to get the latter product?

@jnwadiuko jnwadiuko changed the title 1:1 distance matrix from OSRM? pairwise distance matrix from OSRM? Jun 2, 2023
@rCarto
Copy link
Member

rCarto commented Jun 5, 2023

Hello,
Pairwise distances computation is not provided by the OSRM routing software, and it is not provided by osrm package either.
The solution could be to extract only desired values from the full matrix or to use osrmRoute(..., overview = FALSE) on each desired distances (using the apply() family or a for() loop)

@rCarto
Copy link
Member

rCarto commented Jun 5, 2023

Something like that:

library(osrm)
#> Data: (c) OpenStreetMap contributors, ODbL 1.0 - http://www.openstreetmap.org/copyright
#> Routing: OSRM - http://project-osrm.org/
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.2, PROJ 7.2.1; sf_use_s2() is TRUE
ap <- st_read(system.file("gpkg/apotheke.gpkg", 
                          package = "osrm"),
              quiet = TRUE)

# distance between specific pharmacies
res <- data.frame(src = c(1, 2, 4), 
                  dst = c(6, 9, 49), 
                  duration = NA, 
                  distance = NA)


for(i in 1:nrow(res)){
  res[i, c("duration", "distance")] <- 
    osrmRoute(src = ap[res[i, "src"], ], 
              dst = ap[res[i, "dst"], ],
              overview = FALSE)
}
res
#>   src dst duration distance
#> 1   1   6    14.95    11.56
#> 2   2   9    33.15    19.01
#> 3   4  49    22.18    11.96

Created on 2023-06-05 with reprex v2.0.2

@rCarto rCarto closed this as completed Jun 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants