st_bbox_common: Return common bounding of multiple spatial objects

View source: R/st_bbox_common.R

st_bbox_commonR Documentation

Return common bounding of multiple spatial objects

Description

Return common bounding of multiple spatial objects

Usage

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)
library(terra)

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

# map objects within their common extent:
library(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)

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


a-benini/sfhelpers documentation built on Aug. 28, 2024, 3:30 a.m.