【问题标题】:st_network_paths only generating node_path with single nodesst_network_paths 仅生成带有单个节点的 node_path
【发布时间】:2022-01-18 16:46:13
【问题描述】:

我正在尝试使用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

【问题讨论】:

    标签: r shortest-path sfnetwork


    【解决方案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 package (v2.0.1) 于 2021 年 12 月 15 日创建

    如果所需路径也可以“向后”计算,您可以将as_sfnetwork中的参数directed设置为FALSE(即net = 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
    

    【讨论】:

      猜你喜欢
      • 2015-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-19
      相关资源
      最近更新 更多