nearest | R Documentation |
Extract the nearest nodes or edges to given spatial features
nearest_nodes(x, y, focused = TRUE)
nearest_edges(x, y, focused = TRUE)
x |
An object of class |
y |
Spatial features as object of class |
focused |
Should only features that are in focus be extracted? Defaults
to |
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.
An object of class sf
with each row containing
the nearest node or edge to the corresponding spatial features in y
.
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.