我有一个rasterstack
,我需要使用 shapefile 从每个栅格中提取值。
我知道使用“普通”光栅堆栈很容易,但我有一个RasterStackTS
对象。不幸的是,我必须管理一个RasterStackTS
对象,因为我已经从以前的计算中获得了 if。
这是一个可重现的例子,取自 Babak Naimi 的网站。
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)