Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only name output bands if it won't error #42

Merged
merged 4 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# rsi (development version)

* `stack_rasters()` will only rename bands if `band_names` is the same length as
the number of bands in the output raster (or missing, or defined by a
function). It will now warn you if these lengths are different. Previously, if
you provided more than the required number of band names, `stack_rasters()`
would silently ignore the extra names, and would error if you provided fewer
names than bands.

# rsi 0.1.2

* `get_stac_data()` no longer includes `mask_band` in its outputs when
Expand Down
8 changes: 7 additions & 1 deletion R/get_stac_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,11 @@ get_stac_data <- function(aoi,
if (is.null(names(asset_names))) names(asset_names) <- asset_names

items_urls <- extract_urls(asset_names, items)
if (!is.null(mask_band)) items_urls[[mask_band]] <- rstac::assets_url(items, mask_band)
drop_mask_band <- FALSE
if (!is.null(mask_band) && !(mask_band %in% names(items_urls))) {
items_urls[[mask_band]] <- rstac::assets_url(items, mask_band)
drop_mask_band <- TRUE
}

download_locations <- data.frame(
matrix(
Expand Down Expand Up @@ -376,6 +380,8 @@ get_stac_data <- function(aoi,
on.exit(file.remove(unlist(download_results[["final_bands"]])), add = TRUE)
}

if (drop_mask_band) items_urls[[mask_band]] <- NULL

mapply(
function(in_bands, vrt) {
stack_rasters(
Expand Down
28 changes: 23 additions & 5 deletions R/stack_rasters.R
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ stack_rasters <- function(rasters,
if (!missing(band_names) && is.function(band_names)) {
var_names <- band_names(var_names)
}
# technically we could have skipped reading the names of each raster here
# and just accepted the band names, but we want to check the CRS anyway
if (!missing(band_names) && is.character(band_names)) {
var_names <- band_names
}
Expand Down Expand Up @@ -233,16 +235,32 @@ stack_rasters <- function(rasters,

band_no <- 1
vrt_bands <- vector("list", length(intermediate_vrt))

set_names <- FALSE
if (length(intermediate_vrt) == length(var_names)) {
set_names <- TRUE
} else if (!missing(band_names)) {
rlang::warn(
c(
"The number of band names provided did not match the number of bands in the output file.",
i = "Ignoring the provided band names and using default values instead."
),
class = "rsi_band_name_length_mismatch"
)
}

for (vrt in intermediate_vrt) {
vrt <- readLines(vrt)
band_def <- grep("VRTRasterBand", vrt)
vrt <- vrt[seq(band_def[[1]], band_def[[2]])]
vrt[1] <- gsub("band=\"1\"", paste0("band=\"", band_no, "\""), vrt[1])
vrt <- c(
vrt[1],
paste0(" <Description>", var_names[[band_no]], "</Description>"),
vrt[2:length(vrt)]
)
if (set_names) {
vrt <- c(
vrt[1],
paste0(" <Description>", var_names[[band_no]], "</Description>"),
vrt[2:length(vrt)]
)
}

vrt_bands[[band_no]] <- vrt

Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/test-stack_rasters.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,23 @@ test_that("type_and_length checks", {
error = TRUE
)
})

test_that("stack_rasters warns when band names aren't the right length", {
expect_warning(
stack_rasters(
list(system.file("rasters/example_sentinel1.tif", package = "rsi")),
tempfile(fileext = ".vrt"),
band_names = "VH"
),
class = "rsi_band_name_length_mismatch"
)

expect_warning(
stack_rasters(
list(system.file("rasters/example_sentinel1.tif", package = "rsi")),
tempfile(fileext = ".vrt"),
band_names = c("VH", "VV", "EXTRA")
),
class = "rsi_band_name_length_mismatch"
)
})
Loading