I have a rasterstack
and I need to extract the values from every raster using a shapefile.
I know it's very easy with a "normal" rasterstack, but I have a RasterStackTS
object. Unfortunately I must manage a RasterStackTS
object, because I've obtained if from a previous computation.
Here a reproducible example, taken from Babak Naimi's website.
library(rts)
# location of files
path <- system.file("external", package="rts")
# list of raster files:
lst <- list.files(path=path,pattern='.asc$',full.names=TRUE)
lst
#create a random shapefile over the raster extent
r_4_shp<- raster(lst[1])
shp <- rasterToPolygons(r_4_shp, fun=function(x){x>0.77})
#visualize the shp over the raster
plot(r_4_shp)
plot(shp, add=T)
# creating a RasterStack object
r <- stack(lst)
# corresponding dates to 4 rasters
d <- c("2000-02-01","2000-03-01","2000-04-01","2000-05-01")
d <- as.Date(d)
# creating a RasterStackTS object:
rt <- rts(r,d)
rt
#try to extract raster values with the shapefile
sp_basin_mean <- extract(rt, shp)