0

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

它适用于玩具数据,但不适用于现实世界的数据。需要做什么才能让现实世界、流网络、打球?

此处提供线路数据

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
4

1 回答 1

1

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

# 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 包于 2021-12-15 创建(v2.0.1)

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

> 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
于 2021-12-15T14:05:06.323 回答