gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:06:51 25 4
gpt4 key购买 nike

有没有办法使用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")

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

关于从R sf中的多边形中移除孔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52654701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com