terra
terra copied to clipboard
problem with spatSample when dataset cannot be processed in memory
Dear Robert,
I just realised there is a problem with the spatSample function which (I think) is only the case when the SpatRaster cannot be processed in memory. Here is the description of the issue:
Consider the following example:
f <- system.file("ex/elev.tif", package="terra") r <- rast(f) s <- spatSample(r, 200,cells=T,xy=T, na.rm=T) #--------
It works perfectly, But if the r object is a big SpatRaster, the spatSample function (with the arguments: cells=T, xy=T, and na.rm=T) generates the following warning:
Warning message: [spatSample] fewer cells returned than requested
AND it returns fewer samples while the number of notNA cells is more than enough. When both cells and xy are assigned to FALSE, it works fine and returns the sample with the specified size but having either of cells and xy (or both) arguments as TRUE, generates this warning!
The version of the terra package on my Macbook Pro is 1.7-71
Thanks in advance,
Cheers, Babak
I tried to reproduce this issue using the code below, but it works ok. Maybe this problem is more specific to the dataset?
library("terra")
terraOptions(memfrac = 0.1)
f <- system.file("ex/elev.tif", package = "terra")
r <- rast(f)
r <- disagg(r, 300) # 27000 x 28500 pixels
s <- spatSample(r, 50000, cells = TRUE, xy = TRUE, na.rm = TRUE)
nrow(s)
#> [1] 50000
sum(is.na(s$elevation))
#> [1] 0
Here is the dataset that still generates the warning (can be downloaded from the Dropbox link):
https://www.dropbox.com/scl/fi/wgjqr4b9j1kukqwqm28um/FW__sel.tif?rlkey=qcazqpqgs2i7w2mhef5vv3lqg&dl=0
# filename is 'FW__sel.tif'
pr <- rast('FW__sel.tif')
global(pr[[1]],'notNA')
> notNA
> FW_dem_avg_lonlat 2912994 # it is the same for all layers
>
terraOptions(memfrac = 0.5)
blocks(pr)
> $row
> [1] 1 5410
> $nrows
> [1] 5409 2853
> $n
> [1] 2
s <- spatSample(pr, 200, cells = TRUE, xy = TRUE, na.rm = TRUE)
> Warning message:
> [spatSample] fewer cells returned than requested
nrow(s)
> [1] 97
terraOptions(memfrac = 0.8)
blocks(pr)
> $row
> [1] 1
> $nrows
> [1] 8262
> $n
> [1] 1
s <- spatSample(pr, 200, cells = TRUE, xy = TRUE, na.rm = TRUE)
nrow(s)
> [1] 200