Skip to content

Commit

Permalink
Merge branch 'martin' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Jung authored Dec 5, 2024
2 parents bec158d + b7c0e4e commit 50672cc
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Imports:
terra
Suggests:
gdalUtilities,
stars,
testthat (>= 3.0.0)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

export(misc_sanitizeNames)
export(sp_resampleRas)
export(sp_replaceGriddedNA)
49 changes: 49 additions & 0 deletions R/spl_replaceGriddedNA.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#' Replace NA values in gridded layers with a fixed value.
#'
#' @description
#' This function replaces all NA values in a spatial gridded layer with
#' a fixed value such as for example \code{0}. Accepted input layers are for [`SpatRaster`] from
#' the \code{"terra"} R-package and [`stars`] from the \code{"stars"} R-package.
#'
#' @details
#' Required inputs are a single \code{"obj"} gridded data object and a numeric value.
#' In addition an optional mask layer can be provided that to use a mask. In this case
#' all no-data values a replaced with the value in this mask.
#'
#' @param obj A [`SpatRaster`], [`SpatRasterDataset`] or [`stars`] object.
#' @param value A fixed numeric value of which all \code{NA} values are to be replaced with (Default: \code{0}).
#' @param mask An optional [`SpatRaster`] object used instead of the value.
#' @param verbose Be chatty about what is processed (Default: \code{FALSE}).
#'
#' @examples
#' # Example
#' s <- rast(system.file("ex/logo.tif", package="terra"))
#' s[sample(1:terra::ncell(s), 100)] <- NA
#' sfill <- sp_replaceGriddedNA(s, value = 100)
#' plot(sfill)
#'
#' @returns A object of the same type as the input but with no-data values replaced with \code{'value'}.
#' @author Martin Jung
#' @keywords internal, utils
#' @export
sp_replaceGriddedNA <- function(obj, value = 0, mask, verbose = FALSE){
assertthat::assert_that(
inherits(obj, "stars") || inherits(obj, "SpatRaster"),
is.numeric(value) || length(value)==1,
missing(mask) || (inherits(mask, "stars") || inherits(mask, "SpatRaster")),
is.logical(verbose),
msg = "Input data or parameters wrong set?"
)

# Process
if(missing(mask)){
if(verbose) message("Replacing no data values with value: ", value)
# Set all NA to the value
obj[is.na(obj)] <- value
} else {
if(verbose) message("Masking obj with mask values.")
# Mask the layer by template
obj[is.na(obj)] <- mask[is.na(obj)]
}
return(obj)
}
41 changes: 41 additions & 0 deletions man/sp_replaceGriddedNA.Rd

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

27 changes: 27 additions & 0 deletions tests/testthat/test-spl_tests.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
test_that("Spatial object modifications", {

suppressWarnings(requireNamespace("terra", quietly = TRUE))

# Dummy layers
r1 <- terra::rast(nrows = 10, ncols = 10, res = 0.05, xmin = -1.5, xmax = 1.5, ymin = -1.5, ymax = 1.5, vals = rnorm(3600,mean = .5,sd = .1))
r2 <- terra::rast(nrows = 10, ncols = 10, res = 0.05, xmin = -1.5, xmax = 1.5, ymin = -1.5, ymax = 1.5, vals = rnorm(3600,mean = .5,sd = .5))
expect_s4_class(r1, "SpatRaster")
expect_s4_class(r2, "SpatRaster")

# --- #
# NA replacements #
r1[sample(1:terra::ncell(r1),100)] <- NA
expect_no_error(
r1_filled <- sp_replaceGriddedNA(r1, value = 0)
)
expect_s4_class(r1_filled, "SpatRaster")
expect_equal(terra::global(r1_filled,"min")[,1], 0) # Should be 0
# Use layer 2 for masking instead
expect_no_error(
r1_filled2 <- sp_replaceGriddedNA(r1,mask = r2)
)
expect_s4_class(r1_filled2, "SpatRaster")
# --- #


})

0 comments on commit 50672cc

Please sign in to comment.