0

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)
turbo
  • 1,233
  • 14
  • 36
FraNut
  • 676
  • 1
  • 11
  • 22

1 Answers1

0

I found the answer here. Most of the raster functions cannot work with a RasterStackTS object. However the raster object is kept in slot @raster. Indeed this line runs, using the data from the example I've posted before:

sp_basin_mean <- extract(rt@raster, shp)
Community
  • 1
  • 1
FraNut
  • 676
  • 1
  • 11
  • 22