luukvdmeer / sfnetworks

Tidy Geospatial Networks in R
https://luukvdmeer.github.io/sfnetworks/
Other
347 stars 20 forks source link

st_network_cost does not allow duplicated to node arguments #96

Closed loreabad6 closed 3 years ago

loreabad6 commented 3 years ago

Describe the bug in st_network_cost() one gives to arguments either as node indices or sf POINTS. However igraph::distances() does not allow duplicated to nodes. This is fine when adding node indices manually, but when passing sf POINTS, this is harder to control and find.

Internally we use set_path_endpoints to find the nearest to node indices inside the network. However, the same function is also used in st_network_paths() where this is not an issue.

Reproducible example

library(sf)
library(sfnetworks)
net = as_sfnetwork(roxel, directed = FALSE) %>%
 st_transform(3035)

st_network_cost(net, 1, c(3, 2, 3))
#> Error in (function (graph, v = V(graph), to = V(graph), mode = c("all", : At structural_properties.c:507 : Duplicate vertices in `to', this is not allowed, Invalid value

p1 = st_geometry(net, "nodes")[1]
p2 = st_geometry(net, "nodes")[9]
p3 = st_sfc(p1[[1]] + st_point(c(500, 500)), crs = st_crs(p1))
p4 = st_sfc(p1[[1]] + st_point(c(500.2, 500.2)), crs = st_crs(p1))
p5 = st_sfc(p2[[1]] + st_point(c(-500, -500)), crs = st_crs(p2))
pts1 = c(p1, p5)
pts2 = c(p2, p3, p4)
st_network_cost(net, pts1, pts2)
#> Error in (function (graph, v = V(graph), to = V(graph), mode = c("all", : At structural_properties.c:507 : Duplicate vertices in `to', this is not allowed, Invalid value

Expected behavior st_network_cost() only passes unique values to the to argument, with a warning.

loreabad6 commented 3 years ago

Fixed on develop.

luukvdmeer commented 3 years ago

This was only an issue on develop so can be closed already