Builds the dense matrix of travel distances or times between all units, depot included, in one of three ways:
Arguments
- coords
Data frame or matrix with two coordinate columns:
lat,lon(degrees) for"haversine"and"osrm", orx,y(planar, any unit) for"euclidean". Row names, or a columnunit, name the units. Aselect_units()sample is accepted and only its sampled units are used, so thatframe |> select_units(n) |> travel_matrix(bases = towns)builds the matrix of the field work.- method
"haversine","euclidean"or"osrm".- matrix
Optional square numeric matrix of travel costs already computed; when given,
methodis ignored except for the coordinate columns it implies.- unit
Label of the travel unit (
"km","min", ...), kept for reports; set automatically unless a matrix is supplied.- detour
Multiplier applied to straight-line distances (
>= 1). Ignored, with a warning, for"osrm"and supplied matrices.- measure
For
"osrm":"duration"(minutes) or"distance"(kilometres on the network).- server, profile
For
"osrm": the base URL of the routing server and the profile ("car","bike","foot"); default to theosrmpackage options.- block
For
"osrm": number of origins and of destinations per request (block^2cells each).- bases
Optional data frame of depots (a
unitcolumn and the same coordinate columns) put in front ofcoords.- speed
Optional speed in coordinate units per hour (kilometres per hour with
"haversine"): the distances are turned into minutes andunitbecomes"min". Ignored for"osrm"durations.
Value
A square numeric matrix of class fieldopt_matrix with unit names
as dimnames and attributes coords, method, unit, detour and, for
"osrm", snap (the distance in kilometres from each unit to the point
of the network it was snapped to: large values flag units far from any
road).
Details
from coordinates, as straight-line distances: great-circle (
"haversine", kilometres) withlat/lon, or Euclidean with planarx/y. Adetourfactor (the ratio of road to straight-line distance, typically 1.2 to 1.4 on rural road networks) turns them into a planning estimate of road distances when no network is available;from a road network, with
method = "osrm": durations (minutes) or distances (kilometres) from an OSRM server through theosrmpackage, requested in blocks so that the server's table limit is respected (the public demo server allows 10 000 cells per request, and is meant for small tests only: run your own server for a frame);from a matrix computed elsewhere (
matrix), with the coordinates used only for names and plots.
Road matrices need not be symmetric (one-way streets, different durations by direction); the routing solver prices every leg in the direction travelled.
Examples
segments <- data.frame(
unit = c("depot", "s1", "s2", "s3"),
lat = c(-8.05, -8.10, -8.00, -8.12),
lon = c(-34.90, -34.95, -34.85, -34.80)
)
travel_matrix(segments)
#> Travel matrix: 4 units, haversine (km); mean off-diagonal 12.65.
travel_matrix(segments, detour = 1.3)
#> Travel matrix: 4 units, haversine x 1.3 (km); mean off-diagonal 16.44.
if (FALSE) { # \dontrun{
# road durations from the OSRM demo server (small frames only)
travel_matrix(segments, method = "osrm", measure = "duration")
} # }