I'm trying to generate a list of nodes on the shortest path between two nodes using st_network_paths()
. However, I only get a single value for the node index in node_path
.
It works with toy data but not real world data. What needs to happen to make the real world, stream network, play ball?
Line data available here
library(sfnetworks)
library(sf)
library(tidygraph)
library(tidyverse)
ln <- st_read("river.gpkg")
net = as_sfnetwork(ln)
paths <- st_network_paths(net,
from = 2,
to = 50)
# plot one path to check
node_path <- paths %>%
slice(1) %>%
pull(node_paths) %>%
unlist()
node_path
plot(net, col = "grey")
plot(slice(activate(net, "nodes"), 2),
col = "blue", add = TRUE)
plot(slice(activate(net, "nodes"), 50),
col = "red", add = TRUE)
plot(slice(activate(net, "nodes"), node_path),
col = "green", add = TRUE) # just recreates the node_path
The problem is that you are trying to compute a path between two different branches in a directed network:
Created on 2021-12-15 by the reprex package (v2.0.1)
If the desired paths can also be calculated travelling "backwards", you can set the argument
directed
inas_sfnetwork
toFALSE
(i.e.net = as_sfnetwork(ln, directed = FALSE)
and run the same code as before. You can recognise this type of problems sincest_network_paths()
returns a warning message like