栅格列表中同一日期的镶嵌栅格

Mosaicking rasters from the same date in a list of rasters

我正在使用 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

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

你可以在一个循环中完成。使用可重现的示例:

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)