将彼此之间在特定阈值距离内的多边形分组

Group polygons that are within a certain threshold distance of each other

我正在尝试对彼此之间一定距离内的多边形进行分组。

例如,多边形 1 与多边形 2 的距离在 200,000 米以内,多边形 2 与多边形 3 的距离在 200,000 米以内,因此我想将所有三个组合在一起。多边形 4 和 5 将组合在一起,然后多边形 6 将单独在一个组中。

数据:

food <-structure(list(shape = c(17.1, 17.1, 17.1, 17.1, 18.1, 18.1, 
18.1, 18.1, 19.1, 19.1, 19.1, 19.1, 20.1, 20.1, 20.1, 20.1, 21.1, 
21.1, 21.1, 21.1, 24.1, 24.1, 24.1, 24.1), longitude = c(28, 
38, 38, 28, 38, 48, 48, 38, 38, 48, 48, 38, 58, 68, 68, 58, 58, 
68, 68, 58, 95, 100, 100, 95), latitude = c(-4, -4, -7, -7, -8, 
-8, -11, -11, -12, -12, -15, -15, -15, -15, -18, -18, -18.5, 
-18.5, -22, -22, -12, -12, -19, -19)), class = "data.frame", row.names = c(NA, 
-24L)

然后我使用以下代码将其转换为 shapefile:

library(sf)
library(sfheaders)

food <- lapply(split(food, food$shape), function(x) { coords <- as.matrix(cbind(x$longitude, 
x$latitude)); list(rbind(coords, coords[1,]))}) 

names(food)<- NULL

Coord_Ref <- st_crs(4326)

food <-  st_sfc(st_multipolygon(x=food), crs = Coord_Ref)

st_is_valid(food)

food <-  st_cast(food, "POLYGON")

您可以使用:

st_is_within_distance(st_centroid(food), dist = 2000000)
#> Sparse geometry binary predicate list of length 6, where the predicate was 
#> `is_within_distance '
#>  1: 1, 2, 3
#>  2: 1, 2, 3
#>  3: 1, 2, 3
#>  4: 4, 5
#>  5: 4, 5
#>  6: 6