R语言sfnetworks包,st_network_paths地

2021-09-02  本文已影响0人  youmigo

R语言sfnetworks包,st_network_paths地理空间点间路径
寻找最短路径,最短距离

# Mon Jul 26 15:30:06 2021 -

# 字符编码:UTF-8
# R 版本:R x64 4.1 for window 11
# cgh163email@163.com
# 个人笔记不负责任,拎了个梨🍐🍈
#.rs.restartR()
require(sfnetworks)
rm(list = ls());gc()

?st_network_paths()#地理空间点间路径

library(sf, quietly = TRUE)
library(tidygraph, quietly = TRUE)

#创建以边长度作为权重的网络。
#这些权重将自动用于最短路径计算。
net <- as_sfnetwork(roxel, directed = FALSE) #|> plot()
  st_transform(3035) |>
  activate("edges")  |>
  mutate(weight = edge_length())

# 提供节点索引。
paths = st_network_paths(net, from = 495, to = 121)
paths
#> # A tibble: 1 x 2
#>   node_paths edge_paths
#>   <list>     <list>
#> 1 <int [33]> <int [32]>
node_path = paths %>%
  slice(1) %>%
  pull(node_paths) %>%
  unlist()
node_path
#>  [1] 495 485 244 402 166  18  19  96 299 283   9 167 292 524 111 506 512 657 533
#> [20] 424 426 208 164 260 471 143 136 135 140 478  28  29 121
oldpar = par(no.readonly = TRUE)
par(mar = c(1,1,1,1))
plot(net, col = "grey")
plot(slice(activate(net, "nodes"), node_path), col = "red", add = TRUE)
par(oldpar)
dev.copy(png, "2.png");dev.off()
#提供节点作为空间点。
#不等于节点的点将捕捉到最近的节点。
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)

paths = st_network_paths(net, from = p1, to = p2)
paths
#> # A tibble: 1 x 2
#>   node_paths edge_paths
#>   <list>     <list>
#> 1 <int [33]> <int [32]>
node_path <- paths %>%
  slice(1) %>%
  pull(node_paths) %>%
  unlist()
node_path
#>  [1] 495 485 244 402 166  18  19  96 299 283   9 167 292 524 111 506 512 657 533
#> [20] 424 426 208 164 260 471 143 136 135 140 478  28  29 121
oldpar = par(no.readonly = TRUE)
par(mar = c(1,1,1,1))
plot(net, col = "grey")
plot(c(p1, p2), col = "black", pch = 8, add = TRUE)
plot(slice(activate(net, "nodes"), node_path), col = "red", add = TRUE)
par(oldpar)
dev.copy(png, "3.png");dev.off()
# Mon Jul 26 15:45:49 2021 --

image.png
image.png
上一篇下一篇

猜你喜欢

热点阅读