Skip to content

Commit

Permalink
fixes #1305
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Oct 7, 2023
1 parent 689a579 commit f26bb26
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
20 changes: 14 additions & 6 deletions R/generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -971,15 +971,23 @@ setMethod("scale", signature(x="SpatRaster"),


setMethod("stretch", signature(x="SpatRaster"),
function(x, minv=0, maxv=255, minq=0, maxq=1, smin=NA, smax=NA, histeq=FALSE, scale=1, filename="", ...) {
opt <- spatOptions(filename, ...)
function(x, minv=0, maxv=255, minq=0, maxq=1, smin=NA, smax=NA, histeq=FALSE, scale=1, maxcell=500000, filename="", ...) {
if (histeq) {
eqStretch <- function(x){
ecdfun <- stats::ecdf(x)(x)
ecdfun(x)
if (nlyr(x) > 1) {
warn("stretch", "only the first layer of x is used")
x <- x[[1]]
}
setValues(x, apply(values(x), 2, function(i) stats::ecdf(i)(i))) * scale
if (scale == 1) {
ecdfun <- stats::ecdf(na.omit(spatSample(x, maxcell, "regular")[,1]))
} else {
ecdfun <- function(y) {
f <- stats::ecdf(na.omit(spatSample(x, maxcell, "regular")[,1]))
f(y) * scale
}
}
app(x, ecdfun, filename=filename, ...)
} else {
opt <- spatOptions(filename, ...)
x@cpp <- x@cpp$stretch(minv, maxv, minq, maxq, smin, smax, opt)
messages(x, "stretch")
}
Expand Down
4 changes: 2 additions & 2 deletions man/stretch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ Linear or histogram equalization stretch of values in a SpatRaster.

For linear stretch, provide the desired output range (\code{minv} and \code{maxv}) and the lower and upper bounds in the original data, either as quantiles (\code{minq} and \code{maxq}, or as cell values (\code{smin} and \code{smax}). If \code{smin} and \code{smax} are both not \code{NA}, \code{minq} and \code{maxq} are ignored.

For histogram equalization, these arguments are ignored, but you can provide the desired scale of the output.
For histogram equalization, these arguments are ignored, but you can provide the desired scale of the output and the maximum number of cells that is used to compute the histogram (empirical cumulative distribution function). Only the first layer of the input SpatRaster is processed when using histogram equalization.
}

\usage{
\S4method{stretch}{SpatRaster}(x, minv=0, maxv=255, minq=0, maxq=1, smin=NA, smax=NA,
histeq=FALSE, scale=1, filename="", ...)
histeq=FALSE, scale=1, maxcell=500000, filename="", ...)
}

\arguments{
Expand Down

0 comments on commit f26bb26

Please sign in to comment.