从 R sf 中的多边形中去除孔洞 [英] Removing holes from polygons in R sf

查看:61
本文介绍了从 R sf 中的多边形中去除孔洞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

库(sf)外 = 矩阵(c(0,0,10,0,10,10,0,10,0,0),ncol=2,byrow=TRUE)孔 1 = 矩阵(c(1,1,1,2,2,2,2,1,1,1),ncol=2,byrow=TRUE)孔 2 = 矩阵(c(5,5,5,6,6,6,6,5,5,5),ncol=2,byrow=TRUE)pts = 列表(外层,孔 1,孔 2)(pl1 = st_polygon(pts))# 多边形 ((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")

解决方案

以下 (v0.2.1) 于 2018 年 10 月 5 日创建

Is there a way to remove holes from a polygon in R with the package sf? I would be interested in solutions that include other packages, too. Here's an example of a polygon with two holes.

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))    

Here's the figure:

plot(pl1, col="red")

解决方案

Following https://github.com/r-spatial/sf/issues/609#issuecomment-357426716, this could work:

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)

Created on 2018-10-05 by the reprex package (v0.2.1)

这篇关于从 R sf 中的多边形中去除孔洞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆