R:删除边界框的重叠区域并创建多边形

R: Remove overlapping area of bounding boxes and create a polygon

我想创建一个不包含 2 个选定区域的多边形边界框:

我有 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  = 

我可以用他的函数将它们转换成点矩阵:

library(sf)
get_sc_bbox<- function(bb){
  return(st_coordinates(st_as_sfc(st_bbox(bb))))
}

我如何从 bb1 中删除 bb2 的区域并创建一个新的边界框(多边形)来排除那些区域bb2重叠 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)

reprex package (v0.3.0)

于 2021-03-30 创建