Skip to contents

Extract the nearest nodes or edges to given spatial features

Usage

nearest_nodes(x, y, focused = TRUE)

nearest_edges(x, y, focused = TRUE)

Arguments

x

An object of class sfnetwork.

y

Spatial features as object of class sf or sfc.

focused

Should only features that are in focus be extracted? Defaults to TRUE. See focus for more information on focused networks.

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)