Search code examples
rgisrastergdalr-raster

Why is Mosaic rasters not working in some files?


I want to create a new raster from 2 tiles. For the date "247" everything works fine. But for the next date "248", the raster I'm getting in return is only the first tile h12v12, is not merging the consecutive one (h13v12).

This is just an example. I've worked with more files and had the same problem with some random files. Still, for some of them, I've got the desired results.

The files: https://drive.google.com/drive/folders/1FSUAg-H8ePP9jeZjCTatlGFsDsKCq3cN?usp=sharing

#Open files for a day 
files <- c("AOD2022248.h12v12.tif", "AOD2022248.h13v12.tif")
files_2 <- c("AOD2022247.h12v12.tif", "AOD2022247.h13v12.tif")

#create stack for each one
test_1 <- stack(files[1])
test_2 <- stack(files[2])

test_3 <- stack(files_2[1])
test_4 <- stack(files_2[2])

The first try was with raster::mosaic. For the 248 file, I've got an error. For the 247 file, it worked.

joint <- mosaic(test_1, test_2, fun=mean, filename = "joint.tif", overwrite=TRUE)

Error in v[cells, i] <- as.vector(getValues(x[[i]])) : 
  number of items to replace is not a multiple of replacement length

joint2 <- mosaic(test_3, test_4, fun=mean, filename = "joint2.tif", overwrite=TRUE)

I don't get this error, the rasters are not the same extent because they are supposed to be side by side, not on top of each other.

So... the second try was with gdalUtils::mosaic_rasters. Although I didn't get any errors here, when I open the new tif in QGIS the 248 only has the first tile, and the 247 has the two of them.

mosaic_rasters(files, dst_dataset= "files.tif")

[1] "AOD2022248.h12v12.tif" "AOD2022248.h13v12.tif"
NULL

mosaic_rasters(files_2, dst_dataset= "files2.tif")
[1] "AOD2022247.h12v12.tif" "AOD2022247.h13v12.tif"
NULL

When I use verbose = TRUE in mosaic_rasters...

I've got for 247

Input file size is 2400, 12000...10...20...30...40...50...60...70...80...90...100 - done.

and for 248

Input file size is 1200, 12000...10...20...30...40...50...60...70...80...90...100 - done.

I also compared the rasters to see if there is any difference in h12v12 or h13v12 between days but they are the same.

> compareRaster(test_1, test_3)
[1] TRUE
> compareRaster(test_2, test_4)
[1] TRUE

If the files are the same, why mosaic/merge is only working right with some of them?


Solution

  • The raster stacks need to have the same number of layers

    nlayers(test_1)
    # [1] 4
    nlayers(test_2)
    # [1] 3
    

    If we add another dummy layer filled with NA, then the mosaic works. I assumed that the missing layer is the fourth, but you may need to figure out which one is missing in your specific case.

    test_2a = stack(test_2, init(test_2[[1]], fun=function(x) rep(NA,x)))
    joint <- mosaic(test_1, test_2a, fun=mean, filename = "joint.tif", overwrite=TRUE)
    

    Or, we can use package terra instead of raster

    library(terra)
    test_1 <- rast(files[1])
    test_2 <- rast(files[2])
    joint <- mosaic(test_1, test_2)