创建循环以从多个栅格中提取数据 [英] Create a loop to extract data from multiple raster
问题描述
我有8个栅格,我厌倦了使用一组点来堆叠和提取数据.但这没有奏效,因为栅格的程度不同.
I have 8 rasters that I tired stacking and extracting data from using a set of points. But It didn't work out because rasters have a different extent.
我真的不擅长创建循环.如何在R Studio中编写一个循环函数,在其中我可以循环提取函数并从每个栅格中提取数据并将数据导出到表中.下面是我当前的脚本.
I am really bad at creating loops. How would a write a loop function in R studio where I could loop the extract function and extract data from each raster and export data to a table. Below is my current script.
H1 <- readOGR(dsn = ".", layer = "Shapefile")
L1= raster('raster.tiff')
L2= raster('raster.tiff')
L3= raster('raster.tiff')
L4= raster('raster.tiff')
L5= raster('raster.tiff')
L6= raster('raster.tiff')
L7= raster('raster.tiff')
L8= raster('raster.tiff')
files<- list()
files[[1]] <- L1
files[[2]] <- L2
files[[3]] <- L3
files[[4]] <- L4
files[[5]] <- L5
files[[6]] <- L6
files[[7]] <- L7
files[[8]] <- L8
推荐答案
我假设您的意思是函数 raster :: extract
.另外,您的示例还不完整,因此我无法提供完整的解决方案.
I'm assuming you mean the function raster::extract
. Also, your example is not complete, so I cannot provide a complete solution.
此外,我假设您要提取的一组点定义为称为 coord
的矩阵.
Also, I'm assuming that you have a set of points you want to extract defined as a matrix called coord
.
如果我的假设正确,则可以使用
If my assumtions are correct, you can use
d <- lapply(files, extract, y = coord)
第三个参数被转发到 extract
,因此您可以有效地循环
The third argument is forwarded to extract
, so you're effectively looping
extract(files[[1]], y = coord)
extract(files[[2]], y = coord)
...
在文件
中,您将获得一个列表,其中每个元素包含一个元素.然后,您需要将它们合并在一起,例如使用
You get a list with one element per element in files
. Then you need to merge them together, for example with
library(dplyr)
output <- bind_rows(d, .id = "raster")
这篇关于创建循环以从多个栅格中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!