Mosaicking rasters from the same date in a list of rasters

742 views Asked by At

I am working with R Studio 0.99.902.

I have got a list of rasters (time series of S2 band 5 of a specific tile). The acquisitions of some dates are split into two files which I need to mosaic (they are two different areas of the same tile). This will allow me, later, to build the time series stack. I would like R to detect automatically the files which were acquired on the same date and to mosaick them, without me writing a mosaic function for every couple of duplicated rasters.

This is my raster list:

 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"

As you can see, some rasters have the same date, but not exactly the same name. In order to find duplicates, I extracted the date of each file from its name, and I assigned the dates as the names of the objects in the list. names(lst_B5) <- dates_2 where

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

This allows me to find duplicates within the list:

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

But I still don't know how to automatically mosaic the duplicates. Have you got any hint?

1

There are 1 answers

5
aldo_tapia On BEST ANSWER

You can do it inside a loop. Using a reproducible example:

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
  }
}

And check results:

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

enter image description here

enter image description here

enter image description here

In you case, you can load all raster inside a list (to maintain example's code) with something like:

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

Or:

rlist <- lapply(lst_B5, FUN=raster)