osrmRoute: Get the Shortest Path Between Two Points

Description Usage Arguments Value Examples

View source: R/osrmRoute.R

Description

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

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
osrmRoute(
  src,
  dst,
  loc,
  overview = "simplified",
  exclude = NULL,
  returnclass,
  osrm.server = getOption("osrm.server"),
  osrm.profile = getOption("osrm.profile")
)

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.

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).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
## 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.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!

## End(Not run)

osrm documentation built on Sept. 27, 2021, 5:10 p.m.