-
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.
new function to calculate the percentage of change between rasters
- Loading branch information
Showing
5 changed files
with
86 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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#' Difference between raster objects | ||
#' | ||
#' @param r1 A SpatRaster object. | ||
#' @param r2 A SpatRaster object. | ||
#' @param perc Boolean. Default is TRUE to calculate the percentage | ||
#' of change between r1 and r2. FALSE gives the absolute number | ||
#' instead. | ||
#' @param filename Character. Save results if a name is provided. | ||
#' | ||
#' @return A SpatRaster object with the difference between | ||
#' r1 and r2. | ||
#' @export | ||
#' | ||
#' @examples | ||
#' \donttest{ | ||
#' library(terra) | ||
#' rich1 <- terra::rast(system.file("extdata", "rich_ref.tif", | ||
#' package = "divraster")) | ||
#' rich2 <- terra::rast(system.file("extdata", "rich_fut.tif", | ||
#' package = "divraster")) | ||
#' differ.rast(rich1, rich2) | ||
#' } | ||
differ.rast <- function(r1, r2, perc = TRUE, filename = "") { | ||
# Check that the rasters have the same resolution and extent | ||
if (!terra::compareGeom(r1, r2, stopOnError = FALSE)) { | ||
stop("r1 and r2 must have the same extent and resolution") | ||
} | ||
|
||
# Calculate the difference | ||
diff <- r2 - r1 # Ensure this returns a raster | ||
|
||
# Calculate percentage difference if requested | ||
if (perc) { | ||
# Calculate percentage difference | ||
perc_diff <- (diff / r1) * 100 | ||
perc_diff[is.na(r1) | r1 == 0] <- NA # Handle NA and zero values properly | ||
names(perc_diff) <- "Percentage Difference" | ||
diff <- perc_diff | ||
} else { | ||
names(diff) <- "Absolute Difference" | ||
} | ||
|
||
# Save the result if a filename is provided | ||
if (filename != "") { | ||
terra::writeRaster(diff, filename = filename, overwrite = TRUE) | ||
} | ||
|
||
return(diff) | ||
} |
Binary file not shown.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.