View source: R/st_bbox_common.R
st_bbox_common | R Documentation |
Return common bounding of multiple spatial objects
st_bbox_common(...)
st_bbox_list(l)
... |
objects that can be coerced to bounding boxes with
|
l |
|
object of the class bbox
equal to the return of
st_bbox
but representing the common bounding of several
input (list
ed) objects
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))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.