[R-sig-Geo] Time index is lost after masking raster stack

Thiago V. dos Santos thi_veloso at yahoo.com.br
Wed Feb 10 18:27:11 CET 2016


Dear all,

I usually have to crop - and mask - multilayer raster objects (derived from netcdf files) using a polygon.


I noticed that all time (date) information of the raster objects are lost after the masking operation. Please see this example:

--------------------------
library(raster)

# Create the date sequence
idx <- seq(as.Date("2000/1/1"), as.Date("2000/12/31"), by = "day")

# Create raster stack and assign dates
r <- raster(ncol=20, nrow=20)
s <- stack(lapply(1:length(idx), function(x) setValues(r, runif(ncell(r)))))
s <- setZ(s, idx)
getZ(s) # everything looks good

# Create spatial objects for cropping
e <- extent(-80, -40, -50, -20)
p <- as(e, 'SpatialPolygons')

# Crop and mask
c <- crop(s, e)
getZ(c) # everything still looks good

m <- mask(c, p)
getZ(m) # this returns NULL

--------------------------

Of course it would be easy to manually assign the time index on the masked object - something like m <- setZ(m, getZ(c)) - but it would be great to have a "native" way of preserving it.

Am I doing something wrong with the mask function or is this something that could be addressed in the next release of raster? 
Greetings,
 -- Thiago V. dos Santos

PhD student
Land and Atmospheric Science
University of Minnesota



More information about the R-sig-Geo mailing list