R语言sfnetworks包,st_network_cost()#计算空间网络的成本矩阵
# Mon Jul 26 16:59:02 2021 -
# 字符编码:UTF-8
# R 版本:R x64 4.1 for window 11
# cgh163email@163.com
# 个人笔记不负责任,拎了个梨🍐🍈
#.rs.restartR()
require(sfnetworks)
rm(list = ls());gc()
?st_network_cost()#计算空间网络的成本矩阵
library(sf, quietly = TRUE)
library(tidygraph, quietly = TRUE)
#创建以边长度作为权重的网络。
#这些权重将自动用于最短路径计算。
net = as_sfnetwork(roxel, directed = FALSE) %>%
st_transform(3035) %>%
activate("edges") %>%
mutate(weight = edge_length())
#提供节点索引。
st_network_cost(net, from = c(495, 121), to = c(495, 121))
#> [,1] [,2]
#> [1,] 0.000 2094.555
#> [2,] 2094.555 0.000
# Providing nodes as spatial points.
# Points that don't equal a node will be snapped to their nearest node.
p1 = st_geometry(net, "nodes")[495] + st_sfc(st_point(c(50, -50)))
st_crs(p1) = st_crs(net)
p2 = st_geometry(net, "nodes")[121] + st_sfc(st_point(c(-10, 100)))
st_crs(p2) = st_crs(net)
st_network_cost(net, from = c(p1, p2), to = c(p1, p2))
#> [,1] [,2]
#> [1,] 0.000 2094.555
#> [2,] 2094.555 0.000
# Using another column for weights.
net %>%
activate("edges") %>%
mutate(foo = runif(n(), min = 0, max = 1)) %>%
st_network_cost(c(p1, p2), c(p1, p2), weights = "foo")
#> [,1] [,2]
#> [1,] 0.00000 10.29983
#> [2,] 10.29983 0.00000
# Not providing any from or to points includes all nodes by default.
with_graph(net, graph_order()) # Our network has 701 nodes.
cost_matrix = st_network_cost(net)
dim(cost_matrix)
#> [1] 701 701
网友评论