| osrmRoute | R Documentation |
Build and send an OSRM API query to get the travel geometry
between two points. This function interfaces with the route OSRM
service.
Use src and dst to get the shortest direct route between
two points.
Use loc to get the shortest route between two points using
ordered waypoints.
osrmRoute(
src,
dst,
loc,
overview = "simplified",
exclude,
osrm.server = getOption("osrm.server"),
osrm.profile = getOption("osrm.profile"),
snapping_distance = FALSE
)
src |
starting point of the route.
If relevant, row names are used as identifiers. |
dst |
destination of the route.
If relevant, row names are used as identifiers. |
loc |
starting point, waypoints (optional) and destination of the
route.
The first row or element is the starting point then waypoints are used in
the order they are stored in |
overview |
"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. |
exclude |
pass an optional "exclude" request option to the OSRM API. |
osrm.server |
the base URL of the routing server. |
osrm.profile |
the routing profile to use, e.g. "car", "bike" or "foot". |
snapping_distance |
if TRUE, a list is returned: the first element is the route, the second is a table of snapping distances. |
If snapping_distance = FALSE (default):
If overview is not FALSE, an sf LINESTRING of the shortest
route is returned. It contains 4 fields: src, dst, duration (in minutes)
and distance (in kilometers).
If overview = FALSE, a named numeric vector is returned
(duration in minutes and distance in kilometers).
If snapping_distance = TRUE, a list is returned:
route: the route as described above.
snapping: an sf POINT of the snapped waypoints. It contains
the id and the snapping_distance (in kilometers).
If src (or loc) is a vector, a data.frame or a matrix, the coordinate
reference system (CRS) of the route is EPSG:4326 (WGS84).
If src (or loc) is an sfc or sf object, the route has the same CRS
as src (or loc).
## Not run:
library(sf)
apotheke.df <- read.csv(system.file("csv/apotheke.csv", package = "osrm"))
apotheke.sf <- st_read(system.file("gpkg/apotheke.gpkg", package = "osrm"),
quiet = TRUE
)
# Travel path between points
route1 <- osrmRoute(src = apotheke.sf[1, ], dst = apotheke.sf[16, ])
# Display paths
plot(st_geometry(route1))
plot(st_geometry(apotheke.sf[c(1, 16), ]), col = "red", pch = 20, add = TRUE)
# Return the route and snapping distances
route2 <- osrmRoute(
src = apotheke.sf[1, ], dst = apotheke.sf[16, ],
snapping_distance = TRUE
)
route2$route
route2$snapping
# Return only duration and distance
route3 <- osrmRoute(
src = apotheke.df[1, c("lon", "lat")],
dst = apotheke.df[16, c("lon", "lat")],
overview = FALSE
)
route3
# Using only coordinates
route4 <- osrmRoute(
src = c(13.412, 52.502),
dst = c(13.454, 52.592)
)
plot(st_geometry(route4))
# Using via points
route5 <- osrmRoute(loc = apotheke.sf[c(1, 2, 4, 3), ])
plot(st_geometry(route5), col = "red", lwd = 2)
plot(st_geometry(apotheke.sf[c(1, 2, 4, 3), ]), add = TRUE)
# Using a different routing server
u <- "https://routing.openstreetmap.de/routed-foot/"
route5 <- osrmRoute(apotheke.sf[1, ], apotheke.sf[16, ], osrm.server = u)
route5
# Using an open routing service with support for multiple modes
# see https://github.com/riatelab/osrm/issues/67
u <- "https://routing.openstreetmap.de/"
options(osrm.server = u)
route6 <- osrmRoute(apotheke.sf[1, ], apotheke.sf[16, ],
osrm.profile = "bike"
)
route7 <- osrmRoute(apotheke.sf[1, ], apotheke.sf[16, ],
osrm.profile = "car"
)
plot(st_geometry(route7), col = "green") # car
plot(st_geometry(route6), add = TRUE) # bike
plot(st_geometry(route5), col = "red", add = TRUE) # foot
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.