Skip to content

Commit

Permalink
Warn when not renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemahoney218 committed Feb 23, 2024
1 parent 055df47 commit c37f8a4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
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
18 changes: 17 additions & 1 deletion 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,12 +235,26 @@ 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])
if (length(intermediate_vrt) == length(var_names)) {
if (set_names) {
vrt <- c(
vrt[1],
paste0(" <Description>", var_names[[band_no]], "</Description>"),
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"
)
})

0 comments on commit c37f8a4

Please sign in to comment.