0

我正在使用 R Studio 0.99.902。

我有一个栅格列表(特定图块的 S2 波段 5 的时间序列)。一些日期的采集被分成两个文件,我需要镶嵌它们(它们是同一个图块的两个不同区域)。这将允许我稍后构建时间序列堆栈。我希望 R 自动检测在同一日期获取的文件并将它们镶嵌起来,而不需要我为每对重复的栅格编写一个镶嵌函数。

这是我的栅格列表:

 lst_B5
 [1] "09/05/S2A_20160905T104245Z_31UFS_TOC_V100/S2A_20160905T104245Z_31UFS_TOC-B05_20M_V100.tif"
 [2] "09/08/S2A_20160908T105416Z_31UFS_TOC_V100/S2A_20160908T105416Z_31UFS_TOC-B05_20M_V100.tif"
 [3] "09/18/S2A_20160918T105022Z_31UFS_TOC_V100/S2A_20160918T105022Z_31UFS_TOC-B05_20M_V100.tif"
 [4] "09/18/S2A_20160918T105641Z_31UFS_TOC_V100/S2A_20160918T105641Z_31UFS_TOC-B05_20M_V100.tif"
 [5] "09/25/S2A_20160925T104115Z_31UFS_TOC_V100/S2A_20160925T104115Z_31UFS_TOC-B05_20M_V100.tif"
 [6] "09/28/S2A_20160928T105022Z_31UFS_TOC_V100/S2A_20160928T105022Z_31UFS_TOC-B05_20M_V100.tif"
 [7] "09/28/S2A_20160928T105637Z_31UFS_TOC_V100/S2A_20160928T105637Z_31UFS_TOC-B05_20M_V100.tif"
 [8] "10/05/S2A_20161005T104018Z_31UFS_TOC_V100/S2A_20161005T104018Z_31UFS_TOC-B05_20M_V100.tif"
 [9] "10/08/S2A_20161008T105022Z_31UFS_TOC_V100/S2A_20161008T105022Z_31UFS_TOC-B05_20M_V100.tif"
[10] "10/15/S2A_20161015T104513Z_31UFS_TOC_V100/S2A_20161015T104513Z_31UFS_TOC-B05_20M_V100.tif"
[11] "10/18/S2A_20161018T105035Z_31UFS_TOC_V100/S2A_20161018T105035Z_31UFS_TOC-B05_20M_V100.tif"
[12] "10/25/S2A_20161025T104118Z_31UFS_TOC_V100/S2A_20161025T104118Z_31UFS_TOC-B05_20M_V100.tif"
[13] "10/28/S2A_20161028T105615Z_31UFS_TOC_V100/S2A_20161028T105615Z_31UFS_TOC-B05_20M_V100.tif"
[14] "11/04/S2A_20161104T104250Z_31UFS_TOC_V100/S2A_20161104T104250Z_31UFS_TOC-B05_20M_V100.tif"
[15] "11/07/S2A_20161107T105238Z_31UFS_TOC_V100/S2A_20161107T105238Z_31UFS_TOC-B05_20M_V100.tif"
[16] "11/14/S2A_20161114T104309Z_31UFS_TOC_V100/S2A_20161114T104309Z_31UFS_TOC-B05_20M_V100.tif"
[17] "11/17/S2A_20161117T105325Z_31UFS_TOC_V100/S2A_20161117T105325Z_31UFS_TOC-B05_20M_V100.tif"
[18] "11/24/S2A_20161124T104349Z_31UFS_TOC_V100/S2A_20161124T104349Z_31UFS_TOC-B05_20M_V100.tif"
[19] "11/27/S2A_20161127T105404Z_31UFS_TOC_V100/S2A_20161127T105404Z_31UFS_TOC-B05_20M_V100.tif"

如您所见,一些栅格具有相同的日期,但名称并不完全相同。为了查找重复项,我从文件名中提取了每个文件的日期,并将日期指定为列表中对象的名称。名称(lst_B5)<-dates_2 其中

dates_2
 [1] "20160905" "20160908" "20160918" "20160918" "20160925" "20160928" "20160928" "20161005" "20161008" "20161015"
[11] "20161018" "20161025" "20161028" "20161104" "20161107" "20161114" "20161117" "20161124" "20161127"

这使我可以在列表中找到重复项:

duplicated(names(lst_B5))
 [1] FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

但我仍然不知道如何自动镶嵌重复项。你有什么提示吗?

4

1 回答 1

1

您可以在循环中执行此操作。使用可重现的示例:

set.seed(123)

rlist <- list() # store all rasters

rlist[[1]] <- raster(nrows=90, ncols=180, xmn=-180, xmx=0, ymn=-90, ymx=90,vals=rnorm(16200))
rlist[[2]] <- raster(nrows=90, ncols=180, xmn=0, xmx=180, ymn=-90, ymx=90,vals=rnorm(16200))
rlist[[3]] <- raster(nrows=90, ncols=180, xmn=-180, xmx=0, ymn=-90, ymx=90,vals=rnorm(16200))
rlist[[4]] <- raster(nrows=90, ncols=180, xmn=0, xmx=180, ymn=-90, ymx=90,vals=rnorm(16200))

dates_2 <- c("20160908","20160918","20160918","20160925") # you dates

dates_2u <- unique(dates_2) # create a vector with unique dates

rlist2 <- list() # a second list to store processed rasters

for(i in seq_along(dates_2u)){
  idx <- which(dates_2 %in% dates_2u[i]) #some index
  if(length(idx) > 1){
    rlisttemp <- rlist[idx] # to create a temporal list
    rlisttemp$fun <- mean # set function to make mosaic
    rlist2[[i]] <- do.call(mosaic,rlisttemp)
  }else{
    rlist2[[i]] <- rlist[[idx]] # for non-mosaicking images
  }
}

并检查结果:

plot(rlist2[[1]]);plot(rlist2[[2]]);plot(rlist2[[3]])

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在您的情况下,您可以使用以下内容加载列表中的所有栅格(以维护示例的代码):

rlist <- lapply(list.files('/your/path/to/rasters',
                           pattern = '.tif$',full.names = T,
                           recursive = T),FUN=raster)

或者:

rlist <- lapply(lst_B5, FUN=raster)
于 2017-09-14T13:44:16.967 回答