我在一个文件夹里有多个光栅。我需要在一个多边形形状文件上提取每个光栅的平均值(有更多的2500个多边形)。
我遇到了两个函数zonal和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)发布于 2018-04-20 15:19:26
您不需要循环(每次迭代都重复相同的操作!)。
应该是这样的:
library(raster)
ff <- list.files("my_path", pattern = "\\.tif$", full=TRUE)
s <- stack(ff)
poly <- shapefile("my_path/supplimentY.shp")
ex <- extract(s, poly, fun='mean', na.rm=TRUE, df=TRUE, weights = TRUE)发布于 2022-02-22 03:20:42
我用同样的代码来计算区域边界的气候学的纬向平均值,我花了5-6分钟的时间来处理2736层的栅格数据。
layers <- length(clim)
for (i in 1:length(clim)) {
ex <- extract(clim, shpwb, fun=mean, na.rm=TRUE, df=TRUE)
}
df <- data.frame(ex)
write.csv(df, file = "E:/Central University of Jharkhand/3rd Semester/Climatology/R Studio/CSV.csv")https://stackoverflow.com/questions/49936943
复制相似问题