diff --git a/NAMESPACE b/NAMESPACE index dd3eadb..7611c23 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand export(area.calc) +export(differ.rast) export(dist.centroids) export(load.data) export(spat.alpha) diff --git a/R/diff_rast.R b/R/diff_rast.R new file mode 100644 index 0000000..ee4e5f4 --- /dev/null +++ b/R/diff_rast.R @@ -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) +} diff --git a/inst/extdata/rich_fut.tif b/inst/extdata/rich_fut.tif new file mode 100644 index 0000000..0fb4de7 Binary files /dev/null and b/inst/extdata/rich_fut.tif differ diff --git a/inst/extdata/rich_ref.tif b/inst/extdata/rich_ref.tif new file mode 100644 index 0000000..8e85946 Binary files /dev/null and b/inst/extdata/rich_ref.tif differ diff --git a/man/differ.rast.Rd b/man/differ.rast.Rd new file mode 100644 index 0000000..e56a55c --- /dev/null +++ b/man/differ.rast.Rd @@ -0,0 +1,36 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/diff_rast.R +\name{differ.rast} +\alias{differ.rast} +\title{Difference between raster objects} +\usage{ +differ.rast(r1, r2, perc = TRUE, filename = "") +} +\arguments{ +\item{r1}{A SpatRaster object.} + +\item{r2}{A SpatRaster object.} + +\item{perc}{Boolean. Default is TRUE to calculate the percentage +of change between r1 and r2. FALSE gives the absolute number +instead.} + +\item{filename}{Character. Save results if a name is provided.} +} +\value{ +A SpatRaster object with the difference between +r1 and r2. +} +\description{ +Difference between raster objects +} +\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) +} +}