Skip to content

Commit

Permalink
another test
Browse files Browse the repository at this point in the history
  • Loading branch information
wcornwell committed Nov 20, 2024
1 parent ee6700b commit 88adbb1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
10 changes: 8 additions & 2 deletions R/create_masked_raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#' @param nir_threshold_df optional - a two columned df with 'site' col (site values must match first string of input file name) and threshold values for each file
#' @param red_band_index layer number for red band
#' @param nir_band_index layer number for nir band
#' @param make_plot Logical that determines whether a plot is produced for checking the output, default is FALSE
#' @param return_raster Logical that determines whether raster is returned
#' @examples
#' input <- system.file("extdata/multiband_image", package = "saltbush")
#' output_dir <- tempdir()
Expand All @@ -30,7 +32,8 @@
create_masked_raster <- function(input, output_dir,
ndvi_threshold = NULL, nir_threshold = NULL,
ndvi_threshold_df = NULL, nir_threshold_df = NULL,
red_band_index = 3, nir_band_index = 5) {
red_band_index = 3, nir_band_index = 5,
make_plot = FALSE, return_raster = FALSE) {

if (dir.exists(input)) {
# list all ENVI or TIF files in the directory
Expand Down Expand Up @@ -98,7 +101,10 @@ create_masked_raster <- function(input, output_dir,

print(paste("Masked raster saved to:", masked_filename))

terra::plot(raster_data_masked)
if (make_plot) terra::plot(raster_data_masked)

if (return_raster) return(raster_data_masked)
else return(NULL)
}
}

8 changes: 7 additions & 1 deletion man/create_masked_raster.Rd

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

13 changes: 0 additions & 13 deletions tests/testthat/test-multiband_create.R

This file was deleted.

27 changes: 27 additions & 0 deletions tests/testthat/test-multiband_mask_create.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

output_dir <- tempdir()

test_that('create_multiband_image works', {
input_dir <- system.file("extdata/create_multiband_image", package = "saltbush")
create_multiband_image(input_dir,
c('blue', 'green', 'red', 'red_edge', 'nir'),
output_dir)
folder <- "extdata/create_multiband_image"
output_filename <- file.path(output_dir, paste0(basename(folder), "_multiband_image"))
file_that_should_exist <- paste0(output_filename, '.tif')
expect_true(file.exists(file_that_should_exist))
})

test_that('create_multiband_image works', {
input <- system.file("extdata/multiband_image", package = "saltbush")
a <- create_masked_raster(
input,
output_dir,
ndvi_threshold = 0.02,
nir_threshold = 0.04,
red_band_index = 3,
nir_band_index = 5,
return_raster = TRUE
)
expect_type(a, "S4")
})

0 comments on commit 88adbb1

Please sign in to comment.