从 R sf 中的多边形中删除孔

Removing holes from polygons in R sf

有没有一种方法可以使用包 sf 从 R 中的多边形中删除孔洞?我也会对包含其他软件包的解决方案感兴趣。 这是一个带有两个孔的多边形示例。

library(sf)
outer = matrix(c(0,0,10,0,10,10,0,10,0,0),ncol=2, byrow=TRUE)
hole1 = matrix(c(1,1,1,2,2,2,2,1,1,1),ncol=2, byrow=TRUE)
hole2 = matrix(c(5,5,5,6,6,6,6,5,5,5),ncol=2, byrow=TRUE)
pts = list(outer, hole1, hole2)
(pl1 = st_polygon(pts))
# POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0), (1 1, 1 2, 2 2, 2 1, 1 1),(5 5, 5 6, 6 6, 6 5, 5 5))    

这是数字:

plot(pl1, col="red")

已关注 https://github.com/r-spatial/sf/issues/609#issuecomment-357426716, 这可能有效:

library(sf)
outer = matrix(c(0,0,10,0,10,10,0,10,0,0),ncol=2, byrow=TRUE)
hole1 = matrix(c(1,1,1,2,2,2,2,1,1,1),ncol=2, byrow=TRUE)
hole2 = matrix(c(5,5,5,6,6,6,6,5,5,5),ncol=2, byrow=TRUE)
pts = list(outer, hole1, hole2)
pl1 = st_geometry(st_polygon(pts))

plot(pl1)

pl2 <- st_multipolygon(lapply(pl1, function(x) x[1]))
plot(pl2)

reprex package (v0.2.1)

于 2018-10-05 创建

nngeo 在@lbusett 回答了这个问题后引入了一个函数来执行此操作(并在描述中引用了他,干得好)。

所以你可以使用:

nngeo::st_remove_holes(your_sf_object)

https://rdrr.io/cran/nngeo/man/st_remove_holes.html

sfheaders::sf_remove_holes()也可以这样做

sfheaders::sf_remove_holes(pl1)
POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))