st_network_paths 仅生成 node_path 个节点

st_network_paths only generating node_path with single nodes

我正在尝试使用 st_network_paths() 在两个节点之间的最短路径上生成节点列表。但是,我只得到 node_path.

中节点索引的单个值

它适用于玩具数据,但不适用于真实世界的数据。让现实世界、流媒体网络、打球需要发生什么?

线路数据可用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

问题是您正在尝试计算定向网络中两个不同分支之间的路径:

# packages
library(sf) 
library(tidygraph)
library(sfnetworks)

# data
ln <- st_read("C:/Users/andre/Desktop/river.gpkg", quiet = TRUE)
net = as_sfnetwork(ln)

# plot
par(mar = rep(0, 4))
plot(net)
plot(net %N>% slice(2, 50), add = TRUE, cex = 2, col = c("red", "blue"))

reprex package (v2.0.1)

于 2021-12-15 创建

如果所需路径也可以计算为“向后”行进,则可以将 as_sfnetwork 中的参数 directed 设置为 FALSE(即 net = as_sfnetwork(ln, directed = FALSE) 和 运行 与以前相同的代码。您可以识别此类问题,因为 st_network_paths() returns 警告消息如

> paths <- st_network_paths(
+   net, 
+   from = 2, 
+   to = 50
+ )
Warning message:
In shortest_paths(x, from, to, weights = weights, output = "both",  :
  At structural_properties.c:4745 :Couldn't reach some vertices