Extract the nearest nodes or edges to given spatial features
Value
An object of class sf
with each row containing
the nearest node or edge to the corresponding spatial features in y
.
Details
To determine the nearest node or edge to each feature in y
the function st_nearest_feature
is used. When extracting
nearest edges, spatially explicit edges are required, i.e. the edges table
should have a geometry column.
Examples
library(sf, quietly = TRUE)
net = as_sfnetwork(roxel)
pts = st_sample(st_bbox(roxel), 6)
nodes = nearest_nodes(net, pts)
edges = nearest_edges(net, pts)
oldpar = par(no.readonly = TRUE)
par(mar = c(1,1,1,1), mfrow = c(1,2))
plot(net, main = "Nearest nodes")
plot(pts, cex = 2, col = "red", pch = 20, add = TRUE)
plot(st_geometry(nodes), cex = 2, col = "orange", pch = 20, add = TRUE)
plot(net, main = "Nearest edges")
plot(pts, cex = 2, col = "red", pch = 20, add = TRUE)
plot(st_geometry(edges), lwd = 2, col = "orange", pch = 20, add = TRUE)
par(oldpar)