| spatial_route | R Documentation |
Streams a layer of origins x past a resident spatial_network() and, for
each origin, finds the shortest path to one or more destinations to. Each
origin and destination is snapped to its nearest graph node; the solver
(native-C Dijkstra) returns one row per (origin, destination) pair with the
total cost and, by default, the route geometry. With geometry = FALSE only
the cost is returned, so a destination set per origin yields the
origin-destination cost matrix in long form. The billion-origin stream never
materialises; the graph stays resident.
spatial_route(
x,
network,
to,
to_id = NULL,
geometry = TRUE,
cost_col = "cost",
dest_col = "destination",
geom = "geometry",
coords = NULL,
crs = NA,
out_geom = NULL,
flush_rows = NULL
)
x |
A |
network |
A |
to |
An |
to_id |
Optional column in |
geometry |
If |
cost_col, dest_col |
Names of the output cost and destination-identifier
columns. Defaults |
geom |
Name of the input geometry column holding hex-WKB or WKT strings.
Default |
coords |
Optional length-2 character vector naming the x and y
coordinate columns to assemble point geometry from (e.g. |
crs |
Coordinate reference system of the input geometry, in any form
|
out_geom |
Name of the output geometry column. Defaults to |
flush_rows |
Transformed rows buffered before a spill flush. Larger
values mean fewer, bigger temporary files. |
An unreachable destination returns an infinite cost and an empty geometry
rather than dropping the row, so a cost matrix stays rectangular. Snapping is
to the nearest node, so place origins and destinations on or near the network;
costs are in the units of the network's weight (or CRS length units when the
graph was built from geometry length). The sf package is an optional
dependency (Suggests).
A vectra_node: one row per (origin, destination) carrying x's
attributes, the destination id, the cost, and (when geometry = TRUE) the
route geometry. Backed by temporary .vtr spills removed when the node is
garbage-collected, and carrying the network CRS.
spatial_network() to build the graph, spatial_service_area() for
reachability, collect_sf() to materialize routes as sf.
mk <- function(x1, y1, x2, y2)
sf::st_linestring(rbind(c(x1, y1), c(x2, y2)))
streets <- sf::st_sfc(
mk(0, 0, 1, 0), mk(1, 0, 2, 0), mk(0, 0, 0, 1),
mk(0, 1, 1, 1), mk(1, 0, 1, 1), mk(1, 1, 2, 1), mk(2, 0, 2, 1))
net <- spatial_network(streets)
f <- tempfile(fileext = ".vtr")
write_vtr(data.frame(id = 1:2, x = c(0, 0), y = c(0, 1)), f)
dest <- sf::st_sfc(sf::st_point(c(2, 1)))
tbl(f) |>
spatial_route(net, to = dest, coords = c("x", "y")) |>
collect_sf()
unlink(f)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.