【问题标题】:sf and networks in R : create an edgelist from distinct linestrings and points sf objectsR中的sf和网络:从不同的线串和点sf对象创建一个边缘列表
【发布时间】:2022-01-11 02:17:57
【问题描述】:

我有一个节点(站点)的 sf 对象和另一个线串(路线)。


# toy example 

## nodes
p1 = st_point(c(7, 51))
p2 = st_point(c(7, 52))
p3 = st_point(c(7, 53))
p4 = st_point(c(8, 52))
nodes = st_as_sf(st_sfc(p1, p2, p3,p4, crs = 4326))



## routes

e1 = st_cast(st_union(p1,p3), "LINESTRING")
e2 = st_cast(st_union(p1,p4), "LINESTRING")
e3 = st_cast(st_union(p3,p4), "LINESTRING")
lines = st_as_sf(st_sfc(e1, e2, e3, crs = 4326))


如何获得直接连接两个节点的路径段的边缘列表?

#Desired output

from | to
p1   | p2
p2   | p3
p1   | p4
p3   | p4

这里 p1 和 p3 之间有一条线,但 p2 在中间,所以 p1-p2 和 p2-p3 之间有 2 条边

我知道sfnetwork包可以用线串或点来构建网络,但是如何用线和点的交点来构建空间网络呢?

【问题讨论】:

  • 正如您所提到的,必须定义边缘。您可以查看lwgeom::st_split 以获得点之间的单独线段,这可能更容易使用。

标签: r networking routes sf points


【解决方案1】:

不确定您在寻找什么,但我会再试一次,作为您评论的后续!因此,请在下面找到使用sfsfnetworksdplyr 库的完全编辑的reprex。

Reprex

  • 第 1 步:使用您的点/节点 p1、p3 和 p4 创建一个网络
library(sf)
library(sfnetworks)
library(dplyr)

# toy example 
p1 = st_point(c(7, 51))
p3 = st_point(c(7, 53))
p4 = st_point(c(8, 52))
nodes = st_as_sf(st_sfc(p1, p3, p4, crs = 4326))
nodes$names <- c("p1", "p3", "p4") # add the name of nodes


## routes
e1 = st_cast(st_union(p1,p3), "LINESTRING")
e2 = st_cast(st_union(p1,p4), "LINESTRING")
e3 = st_cast(st_union(p3,p4), "LINESTRING")
lines = st_as_sf(st_sfc(e1, e2, e3, crs = 4326))
lines$from  <-  c("p1", "p1", "p3")
lines$to  <-  c("p3", "p4", "p4")


# Create the network
network <- sfnetwork(nodes, lines, node_key = "names")
#> Checking if spatial network structure is valid...
#> Spatial network structure is valid


# What the network looks like:
network
#> # A sfnetwork with 3 nodes and 3 edges
#> #
#> # CRS:  EPSG:4326 
#> #
#> # A directed acyclic simple graph with 1 component with spatially explicit edges
#> #
#> # Node Data:     3 x 2 (active)
#> # Geometry type: POINT
#> # Dimension:     XY
#> # Bounding box:  xmin: 7 ymin: 51 xmax: 8 ymax: 53
#>             x names
#>   <POINT [°]> <chr>
#> 1      (7 51) p1   
#> 2      (7 53) p3   
#> 3      (8 52) p4   
#> #
#> # Edge Data:     3 x 3
#> # Geometry type: LINESTRING
#> # Dimension:     XY
#> # Bounding box:  xmin: 7 ymin: 51 xmax: 8 ymax: 53
#>    from    to                x
#>   <int> <int> <LINESTRING [°]>
#> 1     1     2     (7 51, 7 53)
#> 2     1     3     (7 51, 8 52)
#> 3     2     3     (7 53, 8 52)
  • 第 2 步:将补充节点(即 p2)添加到“网络”对象
# Create a 'sf' object for point 2 'p2'
p2 = st_point(c(7, 52))
p2_sf <- st_as_sf(st_sfc(p2, crs = 4326))
p2_sf$names <- "p2"

# Add 'p2' to the network and get the 'new_network' object
new_network <- st_network_blend(network, p2_sf)


# Cleaning the 'new_network' object
new_network %>% 
  activate("nodes") %>% 
  mutate(names = coalesce(names.x, names.y)) %>% 
  arrange(., names) %>%
  select(names,x) -> new_network
  • 第 3 步:创建具有所需结果的 sf 对象
new_network %>% 
  activate("edges") %>% 
  st_as_sf() %>% 
  mutate(from = paste0("p", from), to = paste0("p", to))

#> Simple feature collection with 4 features and 2 fields
#> Geometry type: LINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: 7 ymin: 51 xmax: 8 ymax: 53
#> Geodetic CRS:  WGS 84
#> # A tibble: 4 x 3
#>   from  to                   x
#> * <chr> <chr> <LINESTRING [°]>
#> 1 p1    p2        (7 51, 7 52)
#> 2 p2    p3        (7 52, 7 53)
#> 3 p1    p4        (7 51, 8 52)
#> 4 p3    p4        (7 53, 8 52)

reprex package (v2.0.1) 于 2021-12-05 创建

【讨论】:

  • 谢谢!但我想自动创建点之间的线段边缘列表。我见过 sfnetworks,但它只需要线或点,我都有
  • 嗨@pgourdon,我再试一次!希望这个完全编辑的帖子将接近您正在寻找的内容。干杯。
  • 非常感谢。这就是诀窍!
  • 嗨@pgourdon。感谢您的反馈,很高兴我能帮助您。关于您的最后一条评论, sfnetworks 库不提供在两条边/线之间的交叉点添加新节点的可能性。不过有几种解决方案。这里有两个:(i) 如果您的 PC 上安装了 GRASS,您可以使用 v.clean 工具使用 break 参数。通过首先将 R 和 GRASS 与 rgrass7 库链接,可以直接在 R 中实现此解决方案;
  • (ii) 如果 L 表示相交线的集合,则可以运行以下代码 result &lt;- st_collection_extract(st_intersection(L), "POINT")。然后,您可以通过运行result["x"] 访问该组相交点的坐标列表,然后将这些点插入到网络中,正如我在上面的回复中指出的那样。希望这可以帮助。如果您需要进一步的帮助,我认为用最小的可重复示例提出一个新问题将是最有效的。我祝你工作顺利。干杯。
猜你喜欢
  • 1970-01-01
  • 2020-06-21
  • 1970-01-01
  • 2018-12-21
  • 1970-01-01
  • 2021-11-12
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
相关资源
最近更新 更多