vl_route: Get the Shortest Path Between Two Points

View source: R/vl_route.R

vl_routeR Documentation

Get the Shortest Path Between Two Points

Description

Build and send a Valhalla API query to get the travel geometry between two points.
This function interfaces with the route Valhalla 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.

Usage

vl_route(
  src,
  dst,
  loc,
  costing = "auto",
  costing_options = list(),
  server = getOption("valh.server")
)

Arguments

src

starting point of the route. src can be:

  • a vector of coordinates (longitude and latitude, WGS 84),

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

If relevant, row names are used as identifiers.
If src is a data.frame, a matrix, an sfc object or an sf object then only the first row or element is considered.

dst

destination of the route. dst can be:

  • a vector of coordinates (longitude and latitude, WGS 84),

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

If relevant, row names are used as identifiers.
If dst is a data.frame, a matrix, an sfc object or an sf object then only the first row or element is considered.

loc

starting point, waypoints (optional) and destination of 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 then waypoints are used in the order they are stored in loc and the last row or element is the destination.
If relevant, row names are used as identifiers.

costing

costing model to use.

costing_options

list of options to use with the costing model (see https://valhalla.github.io/valhalla/api/turn-by-turn/api-reference/#costing-options for more details about the options available for each costing model).

server

URL of the Valhalla server.

Value

The output of this function is an sf LINESTRING of the shortest route.
It contains 4 fields:

  • starting point identifier

  • destination identifier

  • travel time in minutes

  • travel distance in kilometers.

Examples

## Not run: 
# Inputs are data frames
apotheke.df <- read.csv(system.file("csv/apotheke.csv", package = "valh"))
src <- apotheke.df[1, c("lon", "lat")]
dst <- apotheke.df[2, c("lon", "lat")]
# Route between the two points, using bicycle costing model
route1 <- vl_route(src = src, dst = dst, costing = "bicycle")

# Inputs are sf points
library(sf)
apotheke.sf <- st_read(system.file("gpkg/apotheke.gpkg", package = "valh"),
  quiet = TRUE
)
srcsf <- apotheke.sf[1, ]
dstsf <- apotheke.sf[2, ]
# Route between the two points, using bicycle costing model and a custom
# costing option
route2 <- vl_route(
  src = srcsf,
  dst = dstsf,
  costing = "bicycle",
  costing_options = list(cycling_speed = 19)
)

## End(Not run)

valh documentation built on April 11, 2025, 6:14 p.m.