Helper functions to derive the spatial metadata required by STAC items.
extract_bbox(): reads a raster (local or remote) and returns its bounding box in WGS84 (EPSG:4326). This function requires the optional terra package. It is generic: pass either acharacterpath or URL, or adoc_assetfromnew_asset(), in which case the raster is read from the asset's resolvedlocal_path(seeupdate_root()) or itshref.as_geometry(): converts a bounding box into a GeoJSONPolygongeometry. This function has no external dependencies.
If terra is not installed, you can still build items by passing a
bounding box (a numeric vector c(xmin, ymin, xmax, ymax)) directly to
new_item() and, optionally, a geometry created with as_geometry().
Usage
extract_bbox(x)
# S3 method for class 'character'
extract_bbox(x)
# S3 method for class 'doc_asset'
extract_bbox(x)
as_geometry(bbox)Arguments
- x
A
characterpath or URL to a raster file readable by terra, or adoc_assetfromnew_asset(). Remotehttp(s)URLs are accessed through GDAL's/vsicurl/driver.- bbox
A numeric vector of length 4 with the bounding box coordinates in the order
c(xmin, ymin, xmax, ymax).
Value
extract_bbox(): a numeric vectorc(xmin, ymin, xmax, ymax), orNULLif the raster could not be read.as_geometry(): alistrepresenting a GeoJSONPolygongeometry, orNULLifbboxisNULL.
Examples
# as_geometry() works without any optional dependency
bbox <- c(-50, -10, -49, -9)
as_geometry(bbox)
#> <GeoJSON Geometry: Polygon>
#> bbox: -50, -10, -49, -9
# extract_bbox() requires terra
if (requireNamespace("terra", quietly = TRUE)) {
f <- system.file("extdata/s2/S2_MSI_20LMR_B04_2022-07-16.tif",
package = "rstatic")
if (nzchar(f)) {
# from a path or URL
extract_bbox(f)
# or directly from an asset
extract_bbox(new_asset(f, title = "B04"))
}
}
#> xmin ymin xmax ymax
#> -63.636579 -8.630257 -63.418217 -8.412882
