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

Document 'rasters' type as char/list #14

Merged
merged 1 commit into from
Dec 16, 2023
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
18 changes: 13 additions & 5 deletions R/stack_rasters.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#' to create and does not require much space, but does require the input rasters
#' not be moved or altered. Run
#' `sf::gdal_utils("warp", output_filename, "some_path.tif")` to turn the output
#' VRT into a standalone TIF file.
#' VRT into a standalone TIFF file.
#'
#' @param rasters A list of rasters to combine into a single multi-band raster,
#' either as SpatRaster objects from [terra::rast()] or character file paths
#' to files that can be read by [terra::rast()]. Rasters will be "stacked" upon
#' one another, preserving values. They must share CRS.
#' @param rasters A list or character vector of file paths to rasters to combine
#' into a single multi-band raster. Files will be read by [terra::rast()].
#' Rasters will be "stacked" upon one another, preserving values.
#' They must share a CRS.
#' @param output_filename The location to save the final "stacked" raster. Must be
#' a VRT file.
#' @inheritParams rlang::args_dots_empty
Expand Down Expand Up @@ -52,6 +52,14 @@ stack_rasters <- function(rasters,
band_names) {
rlang::check_dots_empty()

tryCatch(
check_type_and_length(rasters = list()),
error = function(e) check_type_and_length(
rasters = character(),
call = rlang::caller_env(4)
)
)

out_dir <- dirname(output_filename)

if (!(reference_raster %in% seq_along(rasters) ||
Expand Down
10 changes: 5 additions & 5 deletions man/stack_rasters.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tests/testthat/_snaps/stack_rasters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# stack_rasters fails when rasters are not character vectors

Code
stack_rasters(r1)
Condition
Error in `stack_rasters()`:
! Some input arguments weren't the right class or length:
* rasters should be a character, but is a SpatRaster.

8 changes: 8 additions & 0 deletions tests/testthat/test-stack_rasters.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,11 @@ test_that("stack_rasters fails when rasters don't share a CRS", {
class = "rsi_multiple_crs"
)
})

test_that("stack_rasters fails when rasters are not character vectors", {
r1 <- terra::rast(matrix(rnorm(100), 10))
expect_snapshot(
stack_rasters(r1),
error = TRUE
)
})
Loading