[R-sig-Geo] no slot of name "data" for this object of class "RasterStack"
Peter B. Pearman
pearman at wsl.ch
Thu Jan 6 18:02:14 CET 2011
Dear All,
I would, as a complete novice with raster() and associated R, very much
appreciate any helpful suggestion here.
I created an xy-file and a stack of cropped rasters. The cropping was
done the xy-file is the same size as the rasters (and stack), i.e. I'm
trying to sample all the pixels. But when I try to extract the values
for all pixels of the rasters in the stack, I get the following error
message:
> full.area <- extract(raster.stack,xyfile)
Error in .readCells(x at layers[[j]], cells) :
no slot of name "data" for this object of class "RasterStack"
I would greatly appreciate any constructive comment.
Best,
Peter
#The following is most of the code to create the raster stack and sample
it using extract(), but which instead creates the error message.
library(rgdal)
library(raster)
library(utils)
dir.string <- "../WC_10min/"
vargroup <- c("bio","prec","tmax","tmin","tmean")
numfiles <- c(19,12,12,12,12)
mask <- raster("wheatmask")
extent <- extent(mask)
xmin <- attributes(extent)$xmin
xmax <- attributes(extent)$xmax
ymin <- attributes(extent)$ymin
ymax <- attributes(extent)$ymax
xlength <- dim(mask)[2]
ylength <- dim(mask)[1]
# make a file with coordinates for the mask area
x <- seq(from=xmin, to=xmax, length.out=xlength)
y <- seq(from=ymin,to=ymax,length.out=ylength)
xyfile <- expand.grid(x,y)
...
# create the raster stack
raster.stack <- stack()
# fill the stack with cropped climate rasters
raster.name.index <- 0
for (i in 1:length(numfiles)) {
counter <- numfiles[i]
for(j in 1:counter) {
raster.name.index <- raster.name.index + 1
rasterfile <-
paste("../WC_10min/",vargroup[i],"/",vargroup[i],"_",j,sep="")
eval(parse(text=paste(raw.raster.names[raster.name.index],".raw <-
raster('",rasterfile,"')",sep="")))
eval(parse(text=paste(raw.raster.names[raster.name.index],"<-
crop(",raw.raster.names[raster.name.index],".raw,extent)",sep="")))
eval(parse(text=paste("raster.stack <-
stack(raster.stack,",raw.raster.names[raster.name.index],")",sep="")))
print(paste("stack size is now ",raster.name.index,sep=""))
}
}
full.area <- extract(raster.stack,xyfile)
Here is some more info on the objects:
> head(xyfile)
long lat
1 5.745968 1.982493
2 5.912968 1.982493
3 6.079968 1.982493
4 6.246968 1.982493
5 6.413968 1.982493
6 6.580968 1.982493
# here is a partial attributes listing of the stack
> attributes(raster.stack)
$filename
[1] ""
$layers
$layers[[1]]
class : RasterStack
dimensions : 418, 501, 1 (nrow, ncol, nlayers)
ncell : 209418
projection : +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs
+towgs84=0,0,0
min values : -132
max values : 314
extent : 5.666676, 89.16668, 2.000003, 71.66667 (xmin, xmax, ymin,
ymax)
resolution : 0.1666667, 0.1666667 (x, y)
$layers[[2]]
class : RasterLayer
filename :
dimensions : 418, 501, 1 (nrow, ncol, nlayers)
ncell : 209418
min value : 38
max value : 204
projection : +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs
+towgs84=0,0,0
extent : 5.666676, 89.16668, 2.000003, 71.66667 (xmin, xmax, ymin,
ymax)
resolution : 0.1666667, 0.1666667 (x, y)
$layers[[3]]
class : RasterLayer
filename :
dimensions : 418, 501, 1 (nrow, ncol, nlayers)
ncell : 209418
min value : 16
max value : 88
projection : +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs
+towgs84=0,0,0
extent : 5.666676, 89.16668, 2.000003, 71.66667 (xmin, xmax, ymin,
ymax)
resolution : 0.1666667, 0.1666667 (x, y)
...
I note that the first entry (above) is of class 'RasterStack', not
'RasterLayer'. Could this be important?
More information about the R-sig-Geo
mailing list