Return common bounding of multiple spatial objects

st_bbox_common(...)

st_bbox_list(l)

Arguments

...

objects that can be coerced to bounding boxes with bb or with st_bbox

l

list of objects that can be coerced to bounding boxes with bb or with st_bbox

Value

object of the class bbox equal to the return of st_bbox but representing the common bounding of several input (listed) objects

Examples

library(sf)
library(stars)
#> Loading required package: abind
library(terra)
#> terra 1.7.78

# get /create spatial objects of different classes and extent:
sf <- st_read(system.file("gpkg/nc.gpkg", package = "sf"), quiet = TRUE)
logo <- rast(system.file("ex/logo.tif", package = "terra"))
rast <- rast(
  val    = as.vector(logo$red),
  nrows  = nrow(logo),
  ncols  = ncol(logo),
  extent = c(-77, -75, 33, 35),
  crs    = st_crs(sf)$wkt
  )
stars <- st_as_stars(rast) %>% st_set_bbox(., st_bbox(.) + rep(2, 4)) %>% st_flip()

# common extent / bbox:
(bbox_common <- st_bbox_common(sf, rast, stars))
#>      xmin      ymin      xmax      ymax 
#> -84.32385  33.00000 -73.00000  37.00000 

# map objects within their common extent:
library(tmap)
#> Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
#> remotes::install_github('r-tmap/tmap')
tm_shape(stars, bbox = bbox_common) +
  tm_raster(title = "stars", style = "cont", palette = "Spectral") +
  tm_shape(rast) +
  tm_raster(title = "SpatRaster", style = "cont", palette = "viridis") +
  tm_shape(sf) + tm_borders(col = "magenta") +
  tm_layout(legend.stack = "horizontal", legend.position = c(0.05, 0.05))


# list of sf objects
l <- lapply(1:nrow(sf), function(x) sf[x, ])

# bbox of all listed sf objects
st_bbox_list(l)
#>      xmin      ymin      xmax      ymax 
#> -84.32385  33.88199 -75.45698  36.58965 

# the bbox of the original geometry set (sf) and the bbox of its listed objects are identical:
all.equal(st_bbox_list(l), st_bbox(sf))
#> [1] TRUE