osrm (version 3.5.1)

osrmRoute: Get the Shortest Path Between Two Points

Description

Build and send an OSRM API query to get the travel geometry between two points. This function interfaces the route OSRM service.

Usage

osrmRoute(
  src,
  dst,
  loc,
  overview = "simplified",
  exclude = NULL,
  returnclass,
  osrm.server = getOption("osrm.server"),
  osrm.profile = getOption("osrm.profile")
)

Value

If returnclass is not set, a data frame is returned. It contains the longitudes and latitudes of the travel path between the two points.

If returnclass is set to "sf", an sf LINESTRING is returned.

The sf LINESTRING contains 4 fields: identifiers of origine and destination, travel time in minutes and travel distance in kilometers.

If overview is FALSE, a named numeric vector is returned. It contains travel time (in minutes) and travel distance (in kilometers).

Arguments

src

a vector of identifier, longitude and latitude (WGS84), a vector of longitude and latitude (WGS84) or an sf object of the origine point.

dst

a vector of identifier, longitude and latitude (WGS84), a vector of longitude and latitude (WGS84) or an sf object of the destination point.

loc

a data.frame of identifier, longitude and latitude (WGS84) or an sf object of via points. The first row is the origine, the last row is the destination.

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.

returnclass

if returnclass="sf" an sf LINESTRING is returned. If returnclass is not set a data.frame of coordinates is returned.

osrm.server

the base URL of the routing server. getOption("osrm.server") by default.

osrm.profile

the routing profile to use, e.g. "car", "bike" or "foot" (when using the routing.openstreetmap.de test server). getOption("osrm.profile") by default.

Examples

Run this code
if (FALSE) {
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.df[16, ], 
                    returnclass="sf")
# Display paths
plot(st_geometry(route1))
plot(st_geometry(apotheke.sf[c(1,16),]), col = "red", pch = 20, add = TRUE)

# Return only duration and distance
route3 <- osrmRoute(src = apotheke.sf[1, ], dst = apotheke.df[16, ], 
                    overview = FALSE)
route3

# Using only coordinates
route4 <-  osrmRoute(src = c(13.412, 52.502), 
                     dst = c(13.454, 52.592),
                     returnclass = "sf")
plot(st_geometry(route4))

# Using via points
pts <- structure(
 list(x = c(13.32500, 13.30688, 13.30519, 13.31025, 
            13.4721, 13.56651, 13.55303, 13.37263, 13.50919, 13.5682), 
      y = c(52.40566, 52.44491, 52.52084, 52.59318, 52.61063, 52.55317, 
            52.50186, 52.49468, 52.46441, 52.39669)), 
 class = "data.frame", row.names = c(NA, -10L))
route5 <- osrmRoute(loc = pts, returnclass = "sf")
plot(st_geometry(route5), col = "red", lwd = 2)
points(pts, pch = 20, cex = 2)

# Using a different routing server
u <- "https://routing.openstreetmap.de/routed-foot/"
route5 <- osrmRoute(apotheke.sf[1, ], apotheke.df[16, ], returnclass="sf", 
                    osrm.server = u)

# 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.df[16, ], returnclass="sf", 
                    osrm.profile = "bike")
route7 <- osrmRoute(apotheke.sf[1, ], apotheke.df[16, ], returnclass="sf", 
                    osrm.profile = "car")
plot(st_geometry(route5), col = "green")
plot(st_geometry(route6), add = TRUE) # note the cycle route has fewer turns
plot(st_geometry(route7), col = "red", add = TRUE) # car route, indirect = good!
}

Run the code above in your browser using DataCamp Workspace