vl_matrix: Get Travel Time Matrices Between Points

View source: R/vl_matrix.R

vl_matrixR Documentation

Get Travel Time Matrices Between Points

Description

Build and send Valhalla API queries to get travel time matrices between points.
This function interfaces the matrix Valhalla service.
Use src and dst to set different origins and destinations. Use loc to compute travel times or travel distances between all points.

Usage

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

Arguments

src

origin points. src 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.

If relevant, row names are used as identifiers.

dst

destination. dst 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.

If relevant, row names are used as identifiers.

loc

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

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 a list composed of one or two matrices and 2 data.frames

  • durations: a matrix of travel times (in minutes)

  • distances: a matrix of distances (in specified units, default to kilometers)

  • sources: a data.frame of the coordinates of the points actually used as starting points (EPSG:4326 - WGS84)

  • destinations: a data.frame of the coordinates of the points actually used as destinations (EPSG:4326 - WGS84)

Examples

## Not run: 
# Inputs are data frames
apotheke.df <- read.csv(system.file("csv/apotheke.csv", package = "valh"))
# Travel time matrix
distA <- vl_matrix(loc = apotheke.df[1:50, c("lon", "lat")])
# First 5 rows and columns
distA$durations[1:5, 1:5]

# Travel time matrix with different sets of origins and destinations
distA2 <- vl_matrix(
  src = apotheke.df[1:10, c("lon", "lat")],
  dst = apotheke.df[11:20, c("lon", "lat")]
)
# First 5 rows and columns
distA2$durations[1:5, 1:5]

# Inputs are sf points
library(sf)
apotheke.sf <- st_read(system.file("gpkg/apotheke.gpkg", package = "valh"),
  quiet = TRUE
)
distA3 <- vl_matrix(loc = apotheke.sf[1:10, ])
# First 5 rows and columns
distA3$durations[1:5, 1:5]

# Travel time matrix with different sets of origins and destinations
distA4 <- vl_matrix(src = apotheke.sf[1:10, ], dst = apotheke.sf[11:20, ])
# First 5 rows and columns
distA4$durations[1:5, 1:5]

## End(Not run)

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