st_network_cost  R Documentation 
Wrapper around distances
to calculate costs of
pairwise shortest paths between points in a spatial network. It allows to
provide any set of geospatial point as from
and to
arguments.
If such a geospatial point is not equal to a node in the network, it will
be snapped to its nearest node before calculating costs.
st_network_cost( x, from = igraph::V(x), to = igraph::V(x), weights = NULL, Inf_as_NaN = FALSE, ... )
x 
An object of class 
from 
The (set of) geospatial point(s) from which the shortest paths
will be calculated. Can be an object of class 
to 
The (set of) geospatial point(s) to which the shortest paths will
be calculated. Can be an object of class 
weights 
The edge weights to be used in the shortest path calculation.
Can be a numeric vector giving edge weights, or a column name referring to
an attribute column in the edges table containing those weights. If set to

Inf_as_NaN 
Should the cost values of unconnected nodes be stored as

... 
Arguments passed on to 
Spatial features provided to the from
and/or
to
argument don't necessarily have to be points. Internally, the
nearest node to each feature is found by calling
st_nearest_feature
, so any feature with a geometry type
that is accepted by that function can be provided as from
and/or
to
argument.
When directly providing integer node indices or character node names to the
from
and/or to
argument, keep the following in mind. A node
index should correspond to a rownumber of the nodes table of the network.
A node name should correspond to a value of a column in the nodes table
named name
. This column should contain character values without
duplicates.
For more details on the wrapped function from igraph
see the distances
documentation page.
An n times m numeric matrix where n is the length of the from
argument, and m is the length of unique values in the to
argument.
When the to
argument contains spatial features that have the same
nearest node, these features are considered duplicates.
By default, distances
calculates costs by
by allowing to travel each edge in both directions, hence by assuming an
undirected network. This is the default even when the input network is
directed! For directed networks, the behaviour can be changed by setting
mode = "out"
to consider only outbound edges, or mode = "in"
to consider only inbound edges.
Furthermore, distances
does not allow duplicated
values in the to
argument. This also means that when providing
spatial features, sets of multiple features that happen to have the same
nearest node will be reduced to one by selecting only the first of these
features.
st_network_paths
library(sf, quietly = TRUE) library(tidygraph, quietly = TRUE) # Create a network with edge lenghts as weights. # These weights will be used automatically in shortest paths calculation. net = as_sfnetwork(roxel, directed = FALSE) %>% st_transform(3035) %>% activate("edges") %>% mutate(weight = edge_length()) # Providing node indices. st_network_cost(net, from = c(495, 121), to = c(495, 121)) # Providing nodes as spatial points. # Points that don't equal a node will be snapped to their nearest node. p1 = st_geometry(net, "nodes")[495] + st_sfc(st_point(c(50, 50))) st_crs(p1) = st_crs(net) p2 = st_geometry(net, "nodes")[121] + st_sfc(st_point(c(10, 100))) st_crs(p2) = st_crs(net) st_network_cost(net, from = c(p1, p2), to = c(p1, p2)) # Using another column for weights. net %>% activate("edges") %>% mutate(foo = runif(n(), min = 0, max = 1)) %>% st_network_cost(c(p1, p2), c(p1, p2), weights = "foo") # Not providing any from or to points includes all nodes by default. with_graph(net, graph_order()) # Our network has 701 nodes. cost_matrix = st_network_cost(net) dim(cost_matrix)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.