本文介绍了R:删除边界框的重叠区域并创建一个多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个不包含2个选定区域的多边形边界框:
I would like to create a polygon bounding box that does not include 2 selected areas:
我有2个边界框:
bb1 <- c(xmin = 11.23602, ymin = 47.80478, xmax = 11.88867, ymax = 48.24261)
bb2 <- c(xmin = 11.46067, ymin = 48.05556 , xmax = 11.69851 ,ymax =
我可以使用他的函数将它们转换为点矩阵:
I can convert those to a point matrix with his function:
library(sf)
get_sc_bbox<- function(bb){
return(st_coordinates(st_as_sfc(st_bbox(bb))))
}
如何从 bb1 中删除 bb2 的区域,并创建一个新的边界框(多边形),将 bb2 的那些区域排除在外overlayp bb1 吗?
How could I remove areas of bb2 from bb1 and create a new bounding box (a polygon) that would exclude those areas where bb2 overlapp bb1 ?
推荐答案
这应该做到:
suppressMessages(library(sf))
suppressMessages(library(ggsflabel))
# use st_bbox() to make it an actual bounding box,
## then st_as_sfc to make a it a polygon
bb1 <- c(xmin = 11.23602, ymin = 47.80478, xmax = 11.88867, ymax = 48.24261) %>%
st_bbox() %>%
st_as_sfc()
bb2 <- c(xmin = 11.46067, ymin = 48.05556 , xmax = 11.69851 ,ymax = 48.21477) %>%
st_bbox() %>%
st_as_sfc()
bb3 <- c(xmin = 12.08761161, ymin = 47.82664071, xmax = 12.17363439, ymax = 47.88453729) %>%
st_bbox() %>%
st_as_sfc()
#polygon covering all three rectangles
bb_all <- st_union(bb1, bb2) %>%
st_union(bb3) %>%
st_bbox() %>%
st_as_sfc()
# remove bb3 and bb2 from the full bounding box
polygon_with_holes <- st_difference(bb_all, bb3) %>%
st_difference(bb2)
ggplot(polygon_with_holes) +
geom_sf(fill = 'green', alpha = .3)
这篇关于R:删除边界框的重叠区域并创建一个多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!