Random sampling from a large raster layer
I have a large Rasterlayer with integers from 0 to 44.
class : RasterLayer
dimensions : 29800, 34470, 1027206000 (nrow, ncol, ncell)
resolution : 10, 10 (x, y)
extent : 331300, 676000, 5681995, 5979995 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs
data source : /home/mkoehler/stk_rast_whz
names : stk_rast_whz
values : 0, 44 (min, max)
I want to make a stratified sample of 5000 points per stratum. I am getting the following error:
POINTS<-sampleStratified(b, size=5000, na.rm=T, xy=F)
(Error in ys[[i]] <- y : attempt to select less than one element)
Here is the code that reproduces the problems (even if only selecting 1 item per stratum):
set.seed(10)
r <- raster(ncol=5000, nrow=5000)
names(r) <- 'stratum'
r[] <- round((runif(ncell(r)))*44)
sampleStratified(r, size=1,xy=T)
Error in ys[[i]] <- y : attempt to select less than one element
Trying with fewer layers and changing the "size" or "exp" settings has no effect. R: [64-bit] C: \ Program Files \ R \ R-3.1.1
Any ideas?
early!
source to share
This seems to be a bug (as in raster
2.3-12) and occurs when (1) your raster object contains cells with a value 0
and (2) the raster cannot be processed in memory (i.e. canProcessInMemory(r)
is FALSE
).
The function iterates over the values of the unique cells it creates freq(r)
, and then indexes the list in turn on each of these values. If one of these values is zero, an error will be raised because the 0th element does not exist. For example:
list()[[0]]
# Error in list()[[0]] : attempt to select less than one element]
You will notice that no error is thrown if you fill in r
with, for example r[] <- sample(44, ncell(r), replace=TRUE)
, since it will not have zeros.
When the raster can be processed in memory, the function iterates over the line numbers freq(r)
, and therefore subsequent indexing of the list is reasonable.
I contacted the maintainer to report this bug.
In the meantime, as an interim fix, you can use something like the following to adjust a copy of the function (which will remain available in the current R session).
sampleStratified2 <-
eval(parse(text=sub('sr\\[, 2\\] == i', 'sr[, 2] == f[i, 1]',
sub('i in f\\[, 1\\]', 'i in seq_len(nrow(f))',
deparse(getMethod(sampleStratified,
signature='RasterLayer')@.Data))
)))
sampleStratified2(r, size=1, xy=TRUE)
source to share