Skip to content

Commit

Permalink
fixes #1315
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Oct 18, 2023
1 parent 0ca8b33 commit 03d9fc3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
9 changes: 5 additions & 4 deletions R/zonal.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,19 @@ setMethod("zonal", signature(x="SpatRaster", z="SpatRaster"),


setMethod("zonal", signature(x="SpatRaster", z="SpatVector"),
function(x, z, fun="mean", ..., w=NULL, weights=FALSE, exact=FALSE, touches=FALSE, as.raster=FALSE, filename="", wopt=list()) {
function(x, z, fun="mean", na.rm=FALSE, w=NULL, weights=FALSE, exact=FALSE, touches=FALSE, as.raster=FALSE, as.polygons=FALSE, filename="", wopt=list()) {
opt <- spatOptions()
narm <- isTRUE(list(...)$na.rm)
txtfun <- .makeTextFun(fun)
if (!inherits(txtfun, "character")) {
error("zonal", "this 'fun' is not supported. You can use extract instead")
} else {
if (is.null(w)) {
out <- x@cpp$zonal_poly(z@cpp, txtfun, weights[1], exact[1], touches[1], narm, opt)
out <- x@cpp$zonal_poly(z@cpp, txtfun, weights[1], exact[1], touches[1], na.rm, opt)
} else {
if (txtfun != "mean") {
error("zonal", "fun must be 'mean' when using weights")
}
out <- x@cpp$zonal_poly_weighted(z@cpp, w@cpp, weights[1], exact[1], touches[1], narm, opt)
out <- x@cpp$zonal_poly_weighted(z@cpp, w@cpp, weights[1], exact[1], touches[1], na.rm, opt)
}
messages(out, "zonal")
out <- .getSpatDF(out)
Expand All @@ -186,6 +185,8 @@ setMethod("zonal", signature(x="SpatRaster", z="SpatVector"),
}
x <- rasterize(z, x, 1:nrow(z))
subst(x, 1:nrow(out), out, filename=filename, wopt=wopt)
} else if (as.polygons) {
cbind(z, out)
} else {
out
}
Expand Down
5 changes: 3 additions & 2 deletions man/zonal.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ You can also summarize values of a SpatVector for each polygon (zone) defined by
\S4method{zonal}{SpatRaster,SpatRaster}(x, z, fun="mean", ..., w=NULL, wide=TRUE,
as.raster=FALSE, filename="", overwrite=FALSE, wopt=list())

\S4method{zonal}{SpatRaster,SpatVector}(x, z, fun=mean, ..., w=NULL, weights=FALSE,
exact=FALSE, touches=FALSE, as.raster=FALSE, filename="", wopt=list())
\S4method{zonal}{SpatRaster,SpatVector}(x, z, fun="mean", na.rm=FALSE, w=NULL, weights=FALSE,
exact=FALSE, touches=FALSE, as.raster=FALSE, as.vector=FALSE, filename="", wopt=list())

\S4method{zonal}{SpatVector,SpatVector}(x, z, fun=mean, ..., weighted=FALSE, as.polygons=FALSE)
}
Expand All @@ -44,6 +44,7 @@ You can also summarize values of a SpatVector for each polygon (zone) defined by

\item{weighted}{logical. If \code{TRUE}, a weighted.mean is computed and \code{fun} is ignored. Weights are based on the length of the lines or the area of the polygons in \code{x} that intersect with \code{z}. This argument is ignored of \code{x} is a SpatVector or points}
\item{as.polygons}{logical. Should the results be merged with the attributes of \code{z}?}
\item{na.rm}{logical. If \code{TRUE}, \code{NA}s are removed}
}

\value{
Expand Down

0 comments on commit 03d9fc3

Please sign in to comment.