[R-sig-Geo] raster::crop() can fail when cropped region is too large to process in memory.
Josh O'Brien
joshmobrien at gmail.com
Mon Mar 4 21:35:07 CET 2013
Hi all,
The raster package function crop() can fail for certain sufficiently
large cropping extents (in terms of number of rows and columns needing
to be extracted).
The raster I'd like to crop is stored on the disk (i.e. fromDisk(r)
--> TRUE) and I'm writing the results to a file: the problem seems to
be that the 'canProcessFromMemory' test in the crop() function is not
conservative enough. (To see the statement I'm referring to, do
'getMethod("crop", signature="Raster") and look for the if/then block
beginning "if (canProcessInMemory(out, 3)) {"
Here is an example that's reproducible on my 32-bit Windows XP
machine, running the current R-devel and raster package version
2.0.41. Do be aware that the first step -- creating a large example
raster -- may take several (up to 10?) minutes:
###############################################
## (1) In one R session create a large raster and save it to disk
library(raster)
r <- raster(ncol=4502, nrow=5738)
r[] <- 1:length(r)
writeRaster(r, filename="bigRast")
## (2) Then, from another clean R session in the same directory
library(raster)
r <- raster("bigRast")
E <- extent(c(-179, 179, -89, 89))
crop(r, E, filename="smallerRast")
## Error: cannot allocate vector of size 193.8 Mb
###############################################
My temporary fix has been to use trace() to edit crop's "Raster"
method so that it writes in blocks whenever canProcessInMemory(out,
10) is FALSE. That's obviously overkill, but it does work.
Unless I'm missing something, this is basically a bug report, though
I'd be happy to learn of any other better workarounds.
Cheers,
Josh
More information about the R-sig-Geo
mailing list