-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
|
||
export(misc_sanitizeNames) | ||
export(sp_resampleRas) | ||
export(sp_replaceGriddedNA) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
# --- # | ||
|
||
|
||
}) |