Skip to content

Commit

Permalink
fixes #1628
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Nov 25, 2024
1 parent 8f74ac9 commit 12da9b9
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions R/sample.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ sampleWeights <- function(x, size, replace=FALSE, as.df=TRUE, as.points=FALSE, v

sampleStratMemory <- function(x, size, replace, lonlat, ext=NULL, weights=NULL, warn=TRUE) {
if (!is.null(ext)) {
xold <- rast(x)
x <- crop(x, ext)
cells <- cells(xold, ext)
if (!is.null(weights)) {
weights <- crop(weights, ext)
}
} else {
cells <- 1:ncell(x)
}

if (!is.null(weights)) {
Expand All @@ -44,7 +48,7 @@ sampleStratMemory <- function(x, size, replace, lonlat, ext=NULL, weights=NULL,
if (!compareGeom(x, weights)) {
error("spatSample", "geometry of weights does not match the geometry of x")
}
v <- cbind(cell=1:ncell(x), values(x), values(weights))
v <- cbind(cell=cells, values(x), values(weights))
v <- v[!is.na(v[,2]), ]
uv <- sort(unique(v[,2]))
ys <- vector(mode="list", length=length(uv))
Expand All @@ -58,7 +62,7 @@ sampleStratMemory <- function(x, size, replace, lonlat, ext=NULL, weights=NULL,
ys[[i]] <- vv[s,-3]
}
} else if (lonlat) {
v <- cbind(cell=1:ncell(x), values(x), abs(cos(pi * values(init(x, "y")) / 360)))
v <- cbind(cell=cells, values(x), abs(cos(pi * values(init(x, "y")) / 360)))
v <- v[!is.na(v[,2]), ]
uv <- sort(unique(v[,2]))
ys <- vector(mode="list", length=length(uv))
Expand All @@ -72,7 +76,7 @@ sampleStratMemory <- function(x, size, replace, lonlat, ext=NULL, weights=NULL,
ys[[i]] <- vv[s,-3]
}
} else {
v <- cbind(cell=1:ncell(x), values(x))
v <- cbind(cell=cells, values(x))
v <- v[!is.na(v[,2]), ]
uv <- sort(unique(v[,2]))
ys <- vector(mode="list", length=length(uv))
Expand All @@ -94,6 +98,7 @@ sampleStratMemory <- function(x, size, replace, lonlat, ext=NULL, weights=NULL,
warn("spatSample", 'fewer samples than requested are available for group(s): ', paste(ta, collapse=', '))
}
}

ys
}

Expand Down

0 comments on commit 12da9b9

Please # to comment.