R/st_bbox_common.R
st_bbox_common.RdReturn common bounding of multiple spatial objects or aligned bounding of single spatial object
st_bbox_common(...)
st_bbox_list(l)
st_bbox_aligned(obj, alignment)objects that can be coerced to bounding boxes with
bb or with st_bbox
list of objects that can be coerced to bounding boxes with
bb or with st_bbox
any object that can be coerced to a bounding box with
st_bbox
single positive numeric value, corresponding to the units of
the CRS, which the input object has.
object of the class bbox equal to the return of
st_bbox representing in the case of
st_bbox_common() or st_bbox_list() the common bounding
of several (listed) input objects
st_bbox_aligned() the aligned bounding of a single input object.
Returned values xmin, ymin, xmax and ymax match
\(k\) \(\cdot\) alignment-argument, where \(k\) is an integer.
The input object is positioned within these four coordinates.
library(sf)
library(stars)
#> Loading required package: abind
library(terra)
#> terra 1.8.93
# 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)
tm_shape(stars, bbox = bbox_common) +
tm_raster(
col.scale = tm_scale_continuous(values = "brewer.spectral"),
col.legend = tm_legend(
title = "stars",
frame = FALSE,
reverse = TRUE,
height = 10,
position = tm_pos_in("left", "bottom")
)) +
tm_shape(rast) +
tm_raster(
col.scale = tm_scale_continuous(values = "viridis"),
col.legend = tm_legend(
title = "SpatRaster",
frame = FALSE,
reverse = TRUE,
height = 10,
position = tm_pos_in("left", "bottom")
)) +
tm_shape(sf) + tm_borders(col = "magenta") +
tm_layout(legend.stack = "horizontal")
# 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
# get pretty / aligned bbox of an object
(bb_aligned <- st_bbox_aligned(sf, alignment = 2.5))
#> xmin ymin xmax ymax
#> -85.0 32.5 -75.0 37.5
# dividing the aligned bbox by the alignment value results in integer values
bb_aligned / 2.5
#> xmin ymin xmax ymax
#> -34 13 -30 15
# plot used object within extent of aligned bbox
plot(st_geometry(sf), extent = bb_aligned, col = "gray")
# add to plot grid matching the alignment (= cellsize)
grid_aligned <- st_make_grid(bb_aligned, cellsize = 2.5)
plot(grid_aligned, border = "red", lwd = 2, add = TRUE)