【问题标题】:How to extract raster values from a RasterStackTS object using a shapefile?如何使用 shapefile 从 RasterStackTS 对象中提取栅格值?
【发布时间】:2015-07-17 09:09:33
【问题描述】:

我有一个rasterstack,我需要使用 shapefile 从每个栅格中提取值。

我知道使用“普通”光栅堆栈很容易,但我有一个 RasterStackTS 对象。不幸的是,我必须管理一个 RasterStackTS 对象,因为我已经从之前的计算中获得了 if。

这是一个可重现的示例,取自 Babak Naimi 的 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)

【问题讨论】:

  • 我试过extract(rt, shp, time = d[1]),它创建了一个损坏的对象。很有趣。
  • 对...我从 extract(rt, shp) 得到的错误是“下标越界”。
  • t(x@raster[i, drop=drop]) in this line "倾斜" 数据只有一行,并且行 1:4 当然不是子集。
  • 这意味着我应该使用 for 循环逐层应用提取函数?

标签: r extract r-raster


【解决方案1】:

我找到了答案here。 大多数栅格函数不能与RasterStackTS 对象一起使用。 但是,光栅对象保存在插槽 @raster 中。确实,这条线运行,使用我之前发布的示例中的数据:

sp_basin_mean <- extract(rt@raster, shp)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-21
    相关资源
    最近更新 更多