有没有办法在 R 中合并连续的多线串 sf 对象?

Is there a way to merge contiguous multilinestrings sf object in R?

我希望有人能帮助我。比如说,我有七个线段,前四个和后三个是连续的。没有可用于对它们进行分组的属性。有没有办法将前四个线段以及最后三个线段合并为一个特征?

提前致谢,注意安全!

library(sf)

s1 <- st_multilinestring(list(rbind(c(0,3), c(0,4))))
s2 <- st_multilinestring(list(rbind(c(0,4), c(1,5))))
s3 <- st_multilinestring(list(rbind(c(1,5), c(2,5))))
s4 <- st_multilinestring(list(rbind(c(2,5), c(2.5,5))))
s5 <- st_multilinestring(list(rbind(c(2.7,5), c(4,5))))
s6 <- st_multilinestring(list(rbind(c(4,5), c(4.5,4))))
s7 <- st_multilinestring(list(rbind(c(4.5,4), c(5,4))))

sf_ml <- st_sf(section = 1 ,geometry=st_sfc(list(s1,s2,s3,s4,s5,s6,s7)))
plot(sf_ml)

plot output

我希望得到这样的东西:

Simple feature collection with 2 features and 1 field
geometry type:  MULTILINESTRING
dimension:      XY
bbox:           xmin: 0 ymin: 3 xmax: 5 ymax: 5
CRS:            NA
# A tibble: 2 × 2
  section                                           geometry
*   <dbl>                                  <MULTILINESTRING>
1       1 ((0 3, 0 4), (0 4, 1 5), (1 5, 2 5), (2 5, 2.5 5))
2       1         ((2.7 5, 4 5), (4 5, 4.5 4), (4.5 4, 5 4))

我想你可以使用下面的方法。这个想法是使用一个 “几何二元谓词”来计算连接和连续的组 行,提取它们的 ID,然后在按 身份证件。 首先,加载包:

library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:igraph':
#> 
#>     as_data_frame, groups, union
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

创建数据

s1 <- st_multilinestring(list(rbind(c(0,3), c(0,4))))
s2 <- st_multilinestring(list(rbind(c(0,4), c(1,5))))
s3 <- st_multilinestring(list(rbind(c(1,5), c(2,5))))
s4 <- st_multilinestring(list(rbind(c(2,5), c(2.5,5))))
s5 <- st_multilinestring(list(rbind(c(2.7,5), c(4,5))))
s6 <- st_multilinestring(list(rbind(c(4,5), c(4.5,4))))
s7 <- st_multilinestring(list(rbind(c(4.5,4), c(5,4))))
sf_ml <- st_sf(section = 1 ,geometry=st_sfc(list(s1,s2,s3,s4,s5,s6,s7)))

运行 st_touches 查找共享边界点的那些线的 ID。

(my_idx_touches <- st_touches(sf_ml))
#> Sparse geometry binary predicate list of length 7, where the predicate
#> was `touches'
#>  1: 2
#>  2: 1, 3
#>  3: 2, 4
#>  4: 3
#>  5: 6
#>  6: 5, 7
#>  7: 6

我们可以看到第一个和第二个几何共享一个边界点,所以 在。然后,我们将这个对象转换成图形对象。

(my_igraph <- graph_from_adj_list(my_idx_touches))
#> IGRAPH fb199d0 D--- 7 10 -- 
#> + edges from fb199d0:
#>  [1] 1->2 2->1 2->3 3->2 3->4 4->3 5->6 6->5 6->7 7->6

同样,我们可以看到第一个多线串连接到第二个 多线串等等。最后,我们可以使用函数输出返回的名为 membership 的对象来提取连接线组 components().

(my_components <- components(my_igraph)$membership)
#> [1] 1 1 1 1 2 2 2

前四个几何图形属于一组,其他三个几何图形属于一组 另一组。最后,我们可以使用合并同一组中的行 总结()

sf_ml2 <- sf_ml %>% 
  group_by(section = as.character({{my_components}})) %>% 
  summarise()

检查打印件

sf_ml2
#> Simple feature collection with 2 features and 1 field
#> Geometry type: MULTILINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: 0 ymin: 3 xmax: 5 ymax: 5
#> CRS:           NA
#> # A tibble: 2 x 2
#>   section                                           geometry
#>   <chr>                                    <MULTILINESTRING>
#> 1 1       ((0 3, 0 4), (0 4, 1 5), (1 5, 2 5), (2 5, 2.5 5))
#> 2 2               ((2.7 5, 4 5), (4 5, 4.5 4), (4.5 4, 5 4))

和情节

plot(sf_ml2, lwd = 2)

reprex package (v2.0.1)

于 2021-09-14 创建