Skip to contents

Extract the indices of nearest nodes or edges to given spatial features

Usage

nearest_node_ids(x, y, focused = TRUE)

nearest_edge_ids(x, y, focused = TRUE)

Arguments

x

An object of class sfnetwork.

y

Spatial features as object of class sf or sfc.

focused

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

Value

An integer vector with each element containing the index of 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)

nearest_node_ids(net, pts)
#> [1] 761 256   8 353 985 730
nearest_edge_ids(net, pts)
#> [1]  618  144    4  210 1192  939