Skip to content

Commit

Permalink
Only name output bands if it won't error (#42)
Browse files Browse the repository at this point in the history
* Only name output bands if it won't error

* Don't pass mask band name to stack_rasters

* Change item_urls to items_urls

* Warn when not renaming
  • Loading branch information
mikemahoney218 authored Feb 23, 2024
1 parent 60a04a7 commit d08d73f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
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"
)
})

0 comments on commit d08d73f

Please sign in to comment.