只有共享边界才加入多边形

问题描述 投票:0回答:1

我希望你从一组多边形中加入(制作一个多边形),但前提是它们共享一个边界。更具体地说,我想对图像上显示的多边形进行分组(红色圆圈)。

请注意,我已经使用聚合函数根据我拥有的唯一特征(给出下面显示的数据的 Group.1 列)对所有多边形进行了分组。

data_all<-sol_prep 
aggregate(., 
            by = list(.$dscrp_n), 
            function(x) x = floor(mean(x)), 
            join = st_touches) 

这里是其中一组的多边形(所有多边形都显示在图像中):

data<-data.frame(
  stringsAsFactors = FALSE,
           Group.1 = c("Botreaux variante non calcaire loam sableux"),
                                       geometry = c("list(list(c(-72.954502, -72.9547469863982, -72.95470248, -72.95468876, -72.95473877, -72.9547483695981, -72.9547761986361, -72.9547686141566, -72.9547375, -72.9547374779729, -72.95473747, -72.954731472972, -72.9546862, -72.9546719, -72.9546217, -72.9541111, -72.9537796, -72.9534191138071, -72.9536184876125, -72.9536894, -72.9539936, -72.954502, 45.3551106, 45.355239092866, 45.35546251, 45.35556998, 45.35571498, 45.3557402801487, 45.3558137963971, 45.3558191708586, 45.3558412, 45.3558412343503, 45.35584124, 
45.355850598887, 45.3559212, 45.3559707, 45.3559391, 45.3559456, 45.355908, 45.3557070076983, 45.3550789647557, 45.3548556, 45.3548922, 45.3551106)), list(c(-72.95585498, -72.9558549892572, -72.955855, -72.955895, -72.9558991325195, -72.95592001, -72.95605124, -72.9560714217694, -72.9560803, -72.95637, -72.9564575, -72.9566341, -72.9569507, -72.9572217, -72.9571301552681, -72.95702126, -72.9568905833338, -72.9568379, -72.956453, -72.9559522, -72.9555287, -72.9550185833598, -72.9550185, -72.954693, 
-72.954116, -72.9545888, -72.9550388, -72.955062471565, -72.95509001, -72.9557456670355, -72.9557912592063, -72.9557913, -72.9558202611095, -72.95585498, 45.35643124, 45.3564312044354, 45.3564312, 45.3562775, 45.3562731525916, 45.35625124, 45.35590999, 45.3558842884778, 45.355873, 45.3559654, 45.3561548, 45.3563806, 45.356634, 45.356801, 45.3570415117448, 45.3573275, 45.3577420051864, 45.3577584, 45.3579635, 45.3582761, 45.358517, 45.3588990375706, 45.3588991, 45.3591429, 45.3594754, 45.3579813, 
45.3564175, 45.356376444667, 45.35632874, 45.3564491192474, 45.356457492508, 45.3564575, 45.3564455427518, 45.35643124)), list(c(-72.951898010855, -72.9519478, -72.9520076, -72.9527707, -72.9532924, -72.9536606837539, -72.9535226348431, -72.9533967, -72.9531448, -72.9517319, -72.9517002194925, -72.9517082678364, -72.9517108, -72.9517272150412, -72.95182964, -72.951898010855, 45.354487084142, 45.3543493, 45.3543698, 45.3546434, 45.3548079, 45.3548521980459, 45.3552927042705, 45.3556945, 45.355554, 
45.3551603, 45.3551478076863, 45.3551220742613, 45.355114, 45.3550535355782, 45.35467642, 45.354487084142)), list(c(-72.9543244, -72.953895, -72.9536902228012, -72.9534445755317, -72.9530509, -72.9520774, -72.9519059, -72.9522108619773, -72.95232052, -72.95292562, -72.9530258104258, -72.9533351, -72.9537297, -72.9540484, -72.9543244, 45.3571336, 45.358545, 45.3591265127991, 45.3598240114294, 45.3600079, 45.3603542, 45.360533, 45.3595890653697, 45.35924967, 45.35726149, 45.3569459019483, 45.3570037, 
45.3571136, 45.3571511, 45.3571336)), list(c(-72.9515816, -72.9514235, -72.9515028, -72.9514652, -72.9514167, -72.9512285, -72.9509873, -72.9508083, -72.9507681, -72.9510517, -72.9511546, -72.9515872, -72.9523405, -72.9529401, -72.9530057, -72.9524730656377, -72.9518677727211, -72.9517880268497, -72.951764072246, -72.9517552, -72.9516049, -72.9515816, 45.3601539, 45.3593432, 45.3590646, 45.3589925, 45.3587223, 45.3583884, 45.3582523, 45.3582784, 45.3582872, 45.3573966, 45.3570278, 45.3569234, 45.3569, 
45.3569299, 45.3569421, 45.3586415902157, 45.3605727284412, 45.3606558720064, 45.3606540032887, 45.3606533, 45.360379, 45.3601539)), list(c(-72.9494737, -72.9495641, -72.9500557, -72.9498389, -72.9495833, -72.9493425, -72.9493185, -72.949384, -72.9494737, 45.3537547, 45.3536471, 45.3537113, 45.3544144, 45.3542951, 45.354123, 45.3539699, 45.3537902, 45.3537547)), list(c(-72.9476237, -72.947666, -72.9477695, -72.9480365, -72.9483671, -72.9483694, -72.9478321, -72.9475468, -72.9475753, -72.9475533, 
-72.9475693, -72.9476237, 45.3563194, 45.3559056, 45.3557621, 45.3558624, 45.355999, 45.356002, 45.3577621, 45.3576598, 45.357354, 45.3569939, 45.356661, 45.3563194)), list(c(-72.9485813, -72.9486548, -72.948614, -72.9485215, -72.9485313, -72.9486576, -72.948842, -72.9486635, -72.9484489, -72.9483725, -72.9479792, -72.9478854, -72.948192838251, -72.94830612, -72.9483324994349, -72.9484147, -72.9485813, 45.356279, 45.3565943, 45.3568551, 45.3571786, 45.3574846, 45.3576202, 45.3576476, 45.3582266, 
45.3580871, 45.3580687, 45.3578149, 45.3577812, 45.3567772640725, 45.35640736, 45.3563232430847, 45.3560612, 45.356279)))",
                          NA)
   )

我尝试了一个 group_by 函数跟随一个总结但是那没有用因为我不想把所有的多边形连接在一起。

r spatial sf
1个回答
0
投票

您可以将重叠的多边形与

st_union()
合并,尽管这确实会形成多多边形,但您可以将其转换回多边形。但是,您的数据有一个问题:您的大部分多边形并不完全重叠 - 您可以看到绘图中的间隙 - 因此您需要使用
st_buffer()
向它们添加一点,以便它们重叠。

首先,让数据成为一个干净的sf对象:

library(dplyr)
library(ggplot2)
library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.4.2, PROJ 8.2.1; sf_use_s2() is TRUE

# string to objects
data$geometry <- lapply(parse(text = data$geometry), eval)
# objects to lists of matrices to multipolygons
data$geometry <- lapply(
    data$geometry, 
    function(x) {
        if (isTRUE(is.na(x))) NA else {
            lapply(x, function(y) {
                list(matrix(y[[1]], ncol = 2))
            }) |> 
                st_multipolygon()
        }
    }
)

data <- st_as_sf(data)
# drop row with empty geometry from NA
data <- data[!st_is_empty(data$geometry), ]

现在使用

st_union()
拆分、缓冲、分组和汇总,然后拆分回多边形并绘制:

# split multipolygon into rows of polygons
data <- st_cast(data, "POLYGON")
#> Warning in st_cast.sf(data, "POLYGON"): repeating attributes for all
#> sub-geometries for which they may not be constant

# set crs so we can buffer with units
st_crs(data) <- "EPSG:4326"

merged <- data |> 
    st_buffer(dist = units::set_units(3, 'm')) |> 
    group_by(Group.1) |>
    summarise(geometry = st_union(geometry)) |>
    st_cast("POLYGON")
#> Warning in st_cast.sf(summarise(group_by(st_buffer(data, dist =
#> units::set_units(3, : repeating attributes for all sub-geometries for which they
#> may not be constant

merged
#> Simple feature collection with 5 features and 1 field
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -72.95728 ymin: 45.35362 xmax: -72.9475 ymax: 45.3607
#> Geodetic CRS:  WGS 84
#> # A tibble: 5 × 2
#>   Group.1                                                               geometry
#>   <chr>                                                            <POLYGON [°]>
#> 1 Botreaux variante non calcaire loam sableux ((-72.95396 45.35486, -72.9539 45…
#> 2 Botreaux variante non calcaire loam sableux ((-72.95603 45.35586, -72.95599 4…
#> 3 Botreaux variante non calcaire loam sableux ((-72.94801 45.35789, -72.94802 4…
#> 4 Botreaux variante non calcaire loam sableux ((-72.95164 45.36057, -72.95166 4…
#> 5 Botreaux variante non calcaire loam sableux ((-72.94959 45.35362, -72.94958 4…

ggplot(merged) + geom_sf()

unioned plot

请注意,由于缓冲区的原因,这些边界与您的边界不同——它们大了 3m——但它们可能足够相似,具体取决于用例。

© www.soinside.com 2019 - 2024. All rights reserved.