Skip to content

Commit

Permalink
fixes #1213
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Jul 4, 2023
1 parent f3b3fde commit 2dae05e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
7 changes: 6 additions & 1 deletion R/zonal.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]]), ...)
}
Expand Down
9 changes: 6 additions & 3 deletions man/global.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 2dae05e

Please sign in to comment.