Skip to content

Commit

Permalink
for #905
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Dec 24, 2024
1 parent 1235b59 commit e03ca94
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
11 changes: 4 additions & 7 deletions R/sample.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ sampleStratMemory <- function(x, size, replace, lonlat, ext=NULL, weights=NULL,
weights <- crop(weights, ext)
}
} else {
cells <- 1:ncell(x)
cells <- cells(x)
}

if (!is.null(weights)) {
Expand Down Expand Up @@ -104,7 +104,7 @@ sampleStratMemory <- function(x, size, replace, lonlat, ext=NULL, weights=NULL,



sampleStratified <- function(x, size, replace=FALSE, as.df=TRUE, as.points=FALSE, values=TRUE, cells=TRUE, xy=FALSE, ext=NULL, warn=TRUE, exp=5, weights=NULL) {
sampleStratified <- function(x, size, replace=FALSE, as.df=TRUE, as.points=FALSE, values=TRUE, cells=TRUE, xy=FALSE, ext=NULL, warn=TRUE, exp=5, weights=NULL, exhaustive=FALSE) {

if (nlyr(x) > 1) {
x <- x[[1]]
Expand All @@ -116,12 +116,9 @@ sampleStratified <- function(x, size, replace=FALSE, as.df=TRUE, as.points=FALSE

lonlat <- is.lonlat(x, perhaps=TRUE, warn=FALSE)

if (blocks(x, n=4)$n == 1) {

if ((blocks(x, n=4)$n == 1) || exhaustive) {
res <- sampleStratMemory(x, size, replace, lonlat, ext, weights, warn)

} else {

f <- unique(x)[,1]
exp <- max(1, exp)
ss <- exp * size * length(f)
Expand Down Expand Up @@ -463,7 +460,7 @@ setMethod("spatSample", signature(x="SpatRaster"),
if (as.raster) return(sampleRaster(x, size, method, replace, ext, warn))

if (method == "stratified") {
return( sampleStratified(x, size, replace=replace, as.df=as.df, as.points=as.points, cells=cells, values=values, xy=xy, ext=ext, warn=warn, exp=exp, weights=weights) )
return( sampleStratified(x, size, replace=replace, as.df=as.df, as.points=as.points, cells=cells, values=values, xy=xy, ext=ext, warn=warn, exp=exp, weights=weights, exhaustive=exhaustive) )
} else if (!is.null(weights)) {
error("spatSample", "argument weights is only used when method='stratified'")
}
Expand Down
2 changes: 1 addition & 1 deletion man/sample.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ In order to assure regularity when requesting a regular sample, the number of ce

\item{exp}{numeric >= 1. "Expansion factor" that is multiplied with \code{size} to get an initial sample used for stratified samples and random samples with \code{na.rm=TRUE} to try to get at least \code{size} samples}

\item{exhaustive}{logical. If \code{TRUE} and \code{na.rm=TRUE} first all cells that are not \code{NA} are determined and a sample is taken from these cells. This is useful when you are dealing with a very large raster that is sparse (most cells are NA). Otherwise, the default approach may not find enough samples. This should not be used in other cases, especially not with large rasters that mostly have values}
\item{exhaustive}{logical. If \code{TRUE}, and (\code{method=="random"} and \code{na.rm=TRUE}) or \code{method=="stratified"}, all cells that are not \code{NA} are determined and a sample is taken from these cells. This is useful when you are dealing with a very large raster that is sparse (most cells are NA). Otherwise, the default approach may not find enough samples. This should not be used in other cases, especially not with large rasters that mostly have values}
}


Expand Down

0 comments on commit e03ca94

Please sign in to comment.