以下答案基于可以按如下方式安装的R
软件包:sfnetworks
install.packages("remotes")
remotes::install_github("luukvdmeer/sfnetworks")
首先,加载包
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(sfnetworks)
library(tidygraph)
和数据。点对象被转换为sf
格式。
roads <- st_read("C:/Users/Utente/Desktop/Temp/roads_test.shp") %>% st_cast("LINESTRING")
#> Reading layer `roads_test' from data source `C:\Users\Utente\Desktop\Temp\roads_test.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 785 features and 0 fields
#> geometry type: MULTILINESTRING
#> dimension: XY
#> bbox: xmin: 78.12703 ymin: 9.911192 xmax: 78.15389 ymax: 9.943905
#> geographic CRS: WGS 84
points <- tibble::tribble (
~x,~y, ~order,
78.14358, 9.921388,1,
78.14519, 9.921123,2,
78.14889, 9.916954,3,
78.14932, 9.912807,4,
78.14346, 9.913828,5,
78.13490, 9.916551,6,
78.12904, 9.918782,7
)
points <- st_as_sf(points, coords = c("x", "y"), crs = 4326)
绘制网络和点(只是为了更好地理解问题)
par(mar = rep(0, 4))
plot(roads, reset = FALSE)
plot(points, add = TRUE, cex = (1:7)/1.5, col = sf.colors(7), lwd = 4)
将道路转换为 sfnetwork 对象
network <- as_sfnetwork(roads, directed = FALSE)
细分边并选择主要组件。查看https://luukvdmeer.github.io/sfnetworks/articles/preprocess_and_clean.html了解更多详情。
network <- network %>%
convert(to_spatial_subdivision, .clean = TRUE) %>%
convert(to_components, .select = 1, .clean = TRUE) %E>%
mutate(weight = edge_length())
现在我想估计每对连续点之间的最短路径。sfnetwork
不支持多对多路由,所以我们需要定义一个for循环。如果你需要重复这个操作几个点,我认为你应该检查 R 包dodgr
。
routes <- list()
for (i in 1:6) {
path <- st_network_paths(
network,
from = st_geometry(points)[i],
to = st_geometry(points)[i + 1]
)
routes[[i]] <- path
}
提取构成所有最短路径的边的 id
idx <- unlist(pull(do.call("rbind", routes), edge_paths))
因此,如果您想从原始网络中提取边缘
network_shortest_path <- network %E>% slice(idx)
roads_shortest_path <- network_shortest_path %E>% st_as_sf()
绘制网络和点
par(mar = rep(0, 4))
plot(roads, reset = FALSE)
plot(st_geometry(roads_shortest_path), add = TRUE, col = "darkgreen", lwd = 4)
plot(points, add = TRUE, cex = (1:7)/1.5, col = sf.colors(7), lwd = 4)
由reprex 包(v0.3.0)于 2021-03-07 创建