-
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
1 parent
7221f76
commit 7e282fc
Showing
7 changed files
with
155 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
Version: 1.0 | ||
ProjectId: 3ed6f0ab-539e-414c-ac67-f900618cf932 | ||
|
||
RestoreWorkspace: No | ||
SaveWorkspace: No | ||
|
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#' Grow a categorical SpatRaster by certain amount of pixels or distance. | ||
#' | ||
#' @description | ||
#' A common issue in aggregating categorical rasters is that coastal and boundary | ||
#' gridcells tend to decrease owing to aggregation effects. This can cause issues | ||
#' of non-matching grid cells later on. | ||
#' This simple function takes a categorical [`SpatRaster`] object and grows it | ||
#' into no-data areas (those with \code{NA} values) within an optionally provided | ||
#' distance. | ||
#' | ||
#' @note | ||
#' Only \code{NA} grid cells will be filled! | ||
#' | ||
#' @param x A categorical [`SpatRaster`] to be grown. | ||
#' @param iter A [`numeric`] values of the maximum number of iterations from the grid | ||
#' cell border of which \code{x} should be grown (Default: \code{2}). | ||
#' | ||
#' @returns [`SpatRaster`] | ||
#' | ||
#' @keywords spatial | ||
#' | ||
#' @seealso | ||
#' \code{\link[terra]{buffer}}, | ||
#' \code{\link[terra]{focal}} | ||
#' | ||
#' @examples | ||
#' set.seed(42) | ||
#' ras <- terra::rast(ncol = 100, nrow = 100, xmin = 0, xmax = 100, | ||
#' ymin = 0, ymax = 100, resolution = 10, crs = NA) | ||
#' | ||
#' # Fill with dummy values but keep half as NA | ||
#' terra::values(ras) <- c(rep(NA,50),rbinom(terra::ncell(ras)/2, 10, 0.5)) | ||
#' | ||
#' # Convert to factor | ||
#' ras <- terra::as.factor(ras) | ||
#' assertthat::assert_that(terra::is.factor(ras)) | ||
#' | ||
#' ras_nona <- spl_growGrid(x = ras, iter = 10) | ||
#' terra::plot(ras_nona) | ||
#' | ||
#' @export | ||
spl_growGrid <- function(x, iter = 2) { | ||
|
||
assertthat::assert_that( | ||
inherits(x, "SpatRaster"), | ||
is.numeric(iter) && iter > 0 | ||
) | ||
|
||
# Check if there is any NA, otherwise return | ||
if(!terra::global(x, "anyNA")[,1]) return(x) | ||
|
||
# Save categories | ||
if(terra::is.factor(x)) cats <- terra::cats(x) else cats <- NULL | ||
|
||
# Create dummy and progress per iteration | ||
new <- x | ||
pb <- progress::progress_bar$new(total = iter) | ||
|
||
for(i in 1:iter){ | ||
pb$tick() | ||
|
||
# Check if no-data is still present. If no, skip | ||
if(!terra::global(new, "anyNA")[,1]) next() | ||
|
||
# Buffer | ||
new <- terra::focal(x = new, | ||
w = 3, | ||
fun = "modal", | ||
na.policy = "only" | ||
) | ||
} | ||
|
||
# Convert to factor again | ||
if(terra::is.factor(x)) new <- terra::as.factor(new) | ||
|
||
# Return output | ||
return(new) | ||
} |
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
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 |
---|---|---|
|
@@ -22,6 +22,4 @@ test_that("Spatial object modifications", { | |
) | ||
expect_s4_class(r1_filled2, "SpatRaster") | ||
# --- # | ||
|
||
|
||
}) |
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,25 @@ | ||
test_that("Testing to grow a grid", { | ||
|
||
suppressWarnings(requireNamespace("terra", quietly = TRUE)) | ||
|
||
set.seed(42) | ||
ras <- terra::rast(ncol = 100, nrow = 100, xmin = 0, xmax = 100, | ||
ymin = 0, ymax = 100, resolution = 10, crs = NA) | ||
|
||
# Fill with dummy values but keep half as NA | ||
terra::values(ras) <- c(rep(NA,50),rbinom(terra::ncell(ras)/2, 10, 0.5)) | ||
|
||
# Convert to factor | ||
ras <- terra::as.factor(ras) | ||
# Check | ||
testthat::expect_s4_class(ras, "SpatRaster") | ||
testthat::expect_true(terra::is.factor(ras)) | ||
|
||
# Grow grid | ||
testthat::expect_no_error( | ||
ras_nona <- spl_growGrid(x = ras, iter = 10) | ||
) | ||
testthat::expect_s4_class(ras_nona, "SpatRaster") | ||
# Expect no NA to remain | ||
testthat::expect_false( terra::global(ras_nona, "anyNA")[,1] ) | ||
}) |