【发布时间】:2021-05-08 18:58:03
【问题描述】:
我有多个栅格分散在一个区域上,一个 shp 在一些栅格上具有多个多边形。
like this image, there are many others
我想将每个图像中这些多边形的中值提取到一个相同的 csv 中,而不会丢失 shp 的属性值。我可以一个一个地做,但必须有一种方法来自动化它。我尝试使用 for 循环,但返回的 csv 仅包含列表中 1 个图像的值。
dir<-"C:/Users"
listImages <- lapply(list.files(dir, pattern = ".tif$", full.names = TRUE), raster)
shp<-readOGR("shapefile.shp")
for(i in listImages) {
imgextr<-extract(i, shp, fun=median, df=TRUE )
write.csv(imgextr, file="test.csv")
}
这是我能做到的。我真的很想避免为每个栅格创建一个 csv,因为它很多。我也尝试过使用 lapply,但没有成功:
extractor <- function(img){
imgextr<-extract(i, shp, fun=median, df=TRUE)
write.csv(imgextr, file = 'test.csv')))
}
lapply(listImages, FUN=extractor)
这里我得到的错误是“basename(a_csv) 中的错误:预期的字符向量参数”
任何有助于我理解的帮助和解释将不胜感激。
【问题讨论】: