繁体   English   中英

从点列表创建路线并在 R 的路段/道路网络上覆盖路线(点列表)

[英]Creating a route from a list of points and Overlaying the route (list of points) on the road segment/ road network in R

我有一个道路网络 shapefile 和点列表。 我必须从点列表中创建一条路线,然后覆盖/空间连接(整合覆盖路段的点的属性)

示例路网形状文件可以在这里找到https://drive.google.com/drive/folders/103Orz6NuiWOaFoEkM18SlzFTjGYi1rju?usp=sharing

以下是带有 lat (x) 和 long (y) 信息的点的代码。 “顺序”列表示路线中目的地的顺序。

points <-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  
)

我想要的 output 是按上述顺序连接所有点的路线层。 而且我还想整合/对路段进行空间连接。

提前致谢

以下答案基于R package 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)

和数据。 点 object 被转换为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)

Plot网络和点(只是为了更好地理解问题)

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 object

network <- as_sfnetwork(roads, directed = FALSE)

细分边缘和 select 主要组件。 查看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 package 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() 

Plot网络和积分

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)

代表 package (v0.3.0) 于 2021 年 3 月 7 日创建

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM