[英]Group polygons that are within a certain threshold distance of each other
I am trying to group polygons that are within a certain distance of each other.我正在尝试对彼此相距一定距离内的多边形进行分组。
For example, polygon 1 is within 200,000m of polygon 2 and polygon 2 is within 200,000m of polygon 3 so I want to group all three together.例如,多边形 1 在多边形 2 的 200,000m 范围内,多边形 2 在多边形 3 的 200,000m 范围内,所以我想将这三个组合在一起。 Polygon 4 and 5 would be grouped together and then polygon 6 would be in a group alone.多边形 4 和 5 将组合在一起,然后多边形 6 将单独在一个组中。
Data:数据:
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)
And then I turn it into a shapefile using this code:然后我使用以下代码将其转换为 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")
You can use:您可以使用:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.