Hi,

I have recently ran into a problem with the 'crosstab' function, using
R.3.0 on
windows 7 64 bit. The raster package was downloaded with
> install.packages('raster')
yesterday (18/04/2013).

I did not have the problem using earlier versions of R (2.15.2) and raster.

When crosstabulating some large rasters (canProcessInMemory(x)=FALSE),
crosstab fails if a large 'block' in one of the rasters has entirely NA
values.

The same problem does not occur if rasterOptions(maxmemory) is large enough
to
avoid block-by-block processing.

The following example should show the error:

#####################################
library(raster)
library(rgdal)

## Function to run the test
test_crosstab<-function(){
    p4strs=make_EPSG()

    myCRS=CRS(p4strs[1544,3])

    rast_ext=extent(c(500000, 505000),
                    c(1610000, 1615000))

    n_r=5000
    n_c=5000

    r1=raster(rast_ext,
              nrows=n_r,ncols=n_c,
              crs=myCRS)

    values(r1)=runif(n_r*n_c)
    r1[1:1500,]=NA # Sets all of the first 'block' to NA's

    r2=r1
    values(r2)=runif(n_r*n_c)

    ct_test=crosstab(r1,r2,digits=1,progress='text')
}

# With small enough maxmemory, r1 has all NA values in the first 'block',
and the
# following  fails
rasterOptions(maxmemory=1e+08)
test_crosstab()

# This one works for me (machine with 40GB Memory)
rasterOptions(maxmemory=1e+09)
test_crosstab()

####################################


I have tracked down the source of the error, to lines 42-44 in the crosstab
source code.
##
d <- getValuesBlock(x, row=tr$row[i], nrows=tr$nrows[i])
d <- lapply(1:nl, function(i) round(d[, i], digits=digits))
d <- as.data.frame(do.call(table, c(d, ...)))
##
The 'as.data.frame' step fails if one of the rasters has all NA values in
the first block which is processed.

Possibly the code could be modified by checking for all NA values in each
block, and skipping if TRUE??

Thanks,
Gareth.

	[[alternative HTML version deleted]]

