Search code examples
rrasterr-rasterr-mosaic

Corrupt raster, while merging multiple raster tiles


I have a around 1000 raster tiles. I am trying to merge them as a single raster tile.

library(raster)
raster_tile_path <- list.files("file_path", full.names = TRUE,
                                        pattern = ".tif" ))

merge_tile  <- lapply(raster_tile_path, raster)
merge_tile  <- do.call(merge, c(merge_tile, tolerance = 1))

Upon running, i get this error:

Error in rgdal::getRasterData(con, offset = offs, region.dim = reg, band = object@data@band) : Failure during raster IO

Someone already suggested the source of error: https://stackoverflow.com/a/67607770/9101903 could be a bad or corrupted raster tile. But it seems that there is no solution to identify or ignore this specific raster tile.

Anybody got an idea on how to solve this?


Solution

  • If there is a corrupted file, you should be able to find it like this

    for (i in 1:length(merge_tile)) {
        x = merge_tile[[i]] * 1
    }
    

    When an error occurs, tile i is corrupted

    You could also try

    library(terra)
    v <- vrt(raster_tile_path)
    writeRaster(v, "file.tif")