【发布时间】:2018-04-20 08:04:46
【问题描述】:
我在一个文件夹中有多个栅格。我需要在多边形形状文件(有更多 2500 个多边形)上提取这些栅格中的每一个的平均值。
我遇到了两个函数 zonal 和 extract。它说 extract 也可以用于点、线和多边形。这是唯一的区别吗? (是/没有预期)
如何从这些多个栅格中提取平均值并根据这些提取的平均值的文件名指定不同的列名?
编辑::
我在某处找到了一个代码并实现了它。但这需要很长时间,而且一点进展都没有。
grids <- list.files("my_path", pattern = "*.tif$")
#check the number of files in the raster list (grids)
length <- length(grids)
#read-in the polygon shapefile
poly <- readShapePoly("my_path/supplimentY.shp")
#create a raster stack
s <- stack(paste0("my_path/", grids))
#extract raster cell count (sum) within each polygon area (poly)
for (i in 1:length(grids)){
ex <- extract(s, poly, fun='mean', na.rm=TRUE, df=TRUE, weights = TRUE)
# the code doesnot progress from here onwards.
# i checked it by adding this line:: print(i)
}
#write to a data frame
dfr <- data.frame(ex)
【问题讨论】: