osrmTrip: Get the Travel Geometry Between Multiple Unordered Points

View source: R/osrmTrip.R

osrmTripR Documentation

Get the Travel Geometry Between Multiple Unordered Points

Description

Build and send an OSRM API query to get the shortest travel geometry between multiple unordered points. This function interfaces the trip OSRM service.
Use this function to resolve the travelling salesman problem.

Usage

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

Arguments

loc

starting point and waypoints to reach along the route. loc can be:

  • a data.frame of longitudes and latitudes (WGS 84),

  • a matrix of longitudes and latitudes (WGS 84),

  • an sfc object of type POINT,

  • an sf object of type POINT.

The first row or element is the starting point.
Row names, if relevant, or element indexes are used as identifiers.

exclude

pass an optional "exclude" request option to the OSRM API.

overview

"full", "simplified". Add geometry either full (detailed) or simplified according to highest zoom level it could be display on.

returnclass

deprecated.

osrm.server

the base URL of the routing server.

osrm.profile

the routing profile to use, e.g. "car", "bike" or "foot".

Details

As stated in the OSRM API, if input coordinates can not be joined by a single trip (e.g. the coordinates are on several disconnected islands) multiple trips for each connected component are returned.

Value

A list of connected components is returned. Each component contains:

trip

An sf LINESTRING. If loc is a data.frame or a matrix the coordinate reference system (CRS) of the route is EPSG:4326 (WGS84). If loc is an sfc or sf object, the route has the same CRS as loc.
Each line of the returned route is a step of the trip. The object has four columns: start (identifier of the starting point), end (identifier of the destination), duration (duration of the step in minutes), distance (length of the step in kilometers).

summary

A list with 2 components: total duration (in minutes) and total distance (in kilometers) of the trip.

Examples

## Not run: 
library(sf)
apotheke.sf <- st_read(system.file("gpkg/apotheke.gpkg", package = "osrm"),
  quiet = TRUE
)
# Get a trip with a set of points (sf POINT)
trips <- osrmTrip(loc = apotheke.sf[1:5, ])
mytrip <- trips[[1]]$trip
# Display the trip
plot(st_geometry(mytrip), col = "black", lwd = 4)
plot(st_geometry(mytrip), col = c("red", "white"), lwd = 1, add = TRUE)
plot(st_geometry(apotheke.sf[1:5, ]),
  pch = 21, bg = "red", cex = 1,
  add = TRUE
)

## End(Not run)

osrm documentation built on May 29, 2024, 9:29 a.m.