Create a spatial network from point geometries
Usage
create_from_spatial_points(
x,
connections = "complete",
directed = TRUE,
edges_as_lines = TRUE,
compute_length = FALSE,
k = 1
)
Arguments
- x
- connections
How to connect the given point geometries to each other? Can be specified either as an adjacency matrix, or as a character describing a specific method to define the connections. See Details.
- directed
Should the constructed network be directed? Defaults to
TRUE
.- edges_as_lines
Should the created edges be spatially explicit, i.e. have
LINESTRING
geometries stored in a geometry list column? Defaults toTRUE
.- compute_length
Should the geographic length of the edges be stored in a column named
length
? Usesst_length
to compute the length of the edge geometries when edges are spatially explicit, andst_distance
to compute the distance between boundary nodes when edges are spatially implicit. Please note that the values in this column are not automatically recognized as edge weights. This needs to be specified explicitly when calling a function that uses edge weights. Defaults toFALSE
.- k
The amount of neighbors to connect to if
connections = 'knn'
. Defaults to1
, meaning that nodes are only connected to their nearest neighbor. Ignored for any other value of theconnected
argument.
Value
An object of class sfnetwork
.
Details
It is assumed that the given points form the nodes in the network.
How those nodes are connected by edges depends on the connections
argument.
The connections can be specified through an adjacency matrix A, which is an
n x n matrix with n being the number of nodes, and element Aij holding a
TRUE
value if there is an edge from node i to node j, and a
FALSE
value otherwise. In the case of undirected networks, the matrix
is not tested for symmetry, and an edge will exist between node i and node j
if either element Aij or element Aji is TRUE
. Non-logical matrices
are first converted into logical matrices using as.logical
,
whenever possible.
The provided adjacency matrix may also be sparse. This can be an object of
one of the sparse matrix classes from the Matrix package, or a
list-formatted sparse matrix. This is a list with one element per node,
holding the integer indices of the nodes it is adjacent to. An example are
sgbp
objects. If the values are not integers, they are
first converted into integers using as.integer
, whenever
possible.
Alternatively, the connections can be specified by providing the name of a specific method that will create the adjacency matrix internally. Valid options are:
complete
: All nodes are directly connected to each other.sequence
: The nodes are sequentially connected to each other, meaning that the first node is connected to the second node, the second node is connected to the third node, et cetera.mst
: The nodes are connected by their spatial minimum spanning tree, i.e. the set of edges with the minimum total edge length required to connect all nodes. The tree is always constructed on an undirected network, regardless of the value of thedirected
. argument. Ifdirected = TRUE
, each edge is duplicated and reversed to ensure full connectivity of the network. Can also be specified asminimum_spanning_tree
.delaunay
: The nodes are connected by their Delaunay triangulation. Requires the spdep package to be installed, and assumes planar coordinates.gabriel
: The nodes are connected as a Gabriel graph. Requires the spdep package to be installed, and assumes planar coordinates.rn
: The nodes are connected as a relative neighborhood graph. Can also be specified asrelative_neighborhood
orrelative_neighbourhood
. Requires the spdep package to be installed, and assumes planar coordinates.knn
: Each node is connected to its k nearest neighbors, withk
being specified through thek
argument. By default,k = 1
, meaning that the nodes are connected as a nearest neighbor graph. Can also be specified asnearest_neighbors
ornearest_neighbours
. Requires the spdep package to be installed.
Examples
library(sf, quietly = TRUE)
oldpar = par(no.readonly = TRUE)
par(mar = c(1,1,1,1))
pts = st_transform(mozart, 3035)
# Using a custom adjacency matrix
adj = matrix(c(rep(TRUE, 17), rep(rep(FALSE, 17), 16)), nrow = 17)
net = as_sfnetwork(pts, connections = adj)
plot(net)
# Using a sparse adjacency matrix from a spatial predicate
dst = units::set_units(300, "m")
adj = st_is_within_distance(pts, dist = dst, remove_self = TRUE)
net = as_sfnetwork(pts, connections = adj, directed = FALSE)
plot(net)
# Using pre-defined methods
cnet = as_sfnetwork(pts, connections = "complete")
snet = as_sfnetwork(pts, connections = "sequence")
mnet = as_sfnetwork(pts, connections = "mst")
dnet = as_sfnetwork(pts, connections = "delaunay")
gnet = as_sfnetwork(pts, connections = "gabriel")
rnet = as_sfnetwork(pts, connections = "rn")
nnet = as_sfnetwork(pts, connections = "knn")
#> Warning: neighbour object has 4 sub-graphs
knet = as_sfnetwork(pts, connections = "knn", k = 2)
#> Warning: neighbour object has 2 sub-graphs
par(mar = c(1,1,1,1), mfrow = c(4,2))
plot(cnet, main = "complete")
plot(snet, main = "sequence")
plot(mnet, main = "minimum spanning tree")
plot(dnet, main = "delaunay triangulation")
plot(gnet, main = "gabriel graph")
plot(rnet, main = "relative neighborhood graph")
plot(nnet, main = "nearest neighbor graph")
plot(knet, main = "k nearest neighbor graph (k = 2)")
par(oldpar)