diff --git a/R/zonal.R b/R/zonal.R index 264990800..e0b450434 100644 --- a/R/zonal.R +++ b/R/zonal.R @@ -258,7 +258,7 @@ setMethod("zonal", signature(x="SpatVector", z="SpatVector"), setMethod("global", signature(x="SpatRaster"), - function(x, fun="mean", weights=NULL, ...) { + function(x, fun="mean", weights=NULL, maxcell=Inf, ...) { nms <- names(x) nms <- make.unique(nms) @@ -301,6 +301,11 @@ setMethod("global", signature(x="SpatRaster"), nl <- nlyr(x) res <- list() + if (is.finite(maxcell)) { + maxcell <- round(maxcell) + if (maxcell < 1) error("global", "maxcell should be positive") + x <- spatSample(x, maxcell, "regular", as.raster=TRUE) + } for (i in 1:nl) { res[[i]] <- fun(values(x[[i]]), ...) } diff --git a/man/global.Rd b/man/global.Rd index 1f723721c..a54769b49 100644 --- a/man/global.Rd +++ b/man/global.Rd @@ -9,20 +9,23 @@ \description{ Compute global statistics, that is summarized values of an entire SpatRaster. -If \code{x} is very large \code{global} will fail, except when \code{fun} is one of "mean", "min", "max", "sum", "prod", "range" (min and max), "rms" (root mean square), "sd" (sample standard deviation), "std" (population standard deviation), "isNA" (number of cells that are NA), "notNA" (number of cells that are not NA). +If \code{x} is very large \code{global} can fail, except when \code{fun} is one of these built-in functions "mean", "min", "max", "sum", "prod", "range" (min and max), "rms" (root mean square), "sd" (sample standard deviation), "std" (population standard deviation), "isNA" (number of cells that are NA), "notNA" (number of cells that are not NA). + +The reason that this can fail with large raster and a custom function is that all values need to be loaded into memory. To circumvent this problem you can run \code{global} with a sample of the cells. You can compute a weighted mean or sum by providing a SpatRaster with weights. } \usage{ -\S4method{global}{SpatRaster}(x, fun="mean", weights=NULL, ...) +\S4method{global}{SpatRaster}(x, fun="mean", weights=NULL, maxcell=Inf, ...) } \arguments{ \item{x}{SpatRaster} - \item{fun}{function to be applied to summarize the values by zone. Either as one or more of these character values: "max", "min", "mean", "sum", "range", "rms" (root mean square), "sd", "std" (population sd, using \code{n} rather than \code{n-1}), "isNA", "notNA"; or a proper R function (but these may fail for very large SpatRasters)} + \item{fun}{function to be applied to summarize the values by zone. Either as one or more of these built-in character values: "max", "min", "mean", "sum", "range", "rms" (root mean square), "sd", "std" (population sd, using \code{n} rather than \code{n-1}), "isNA", "notNA"; or a proper R function (but these may fail for very large SpatRasters unless you specify \code{maxcell})} \item{...}{additional arguments passed on to \code{fun}} \item{weights}{NULL or SpatRaster} + \item{maxcell}{positive integer used to take a regular sample of \code{x}. Ignored by the built-in functions.} } \value{