Merging multiple rasters in R

后端 未结 7 1255
傲寒
傲寒 2021-02-01 19:43

I\'ve been trying to find a time-efficient way to merge multiple raster images in R. These are adjacent ASTER scenes from the southern Kilimanjaro region, and my target is to pu

7条回答
  •  有刺的猬
    2021-02-01 20:09

    I ran into the following problem when trying to mosaic several rasters on top of each other

    In vv[is.na(vv)] <- getValues(x[[i]])[is.na(vv)] :
      number of items to replace is not a multiple of replacement length 
    

    As @Robert Hijmans pointed out, it was likely because of misaligned rasters. To work around this, I had to resample the rasters first

    library(raster)
    
    x  <- raster("Base_raster.tif")
    r1 <- raster("Top1_raster.tif")
    r2 <- raster("Top2_raster.tif")
    
    # Resample
    x1 <- resample(r1, crop(x, r1))
    x2 <- resample(r2, crop(x, r2))
    
    # Merge rasters. Make sure to use the right order
    m <- merge(merge(x1, x2), x)
    
    # Write output
    writeRaster(m,
                filename = file.path("Mosaic_raster.tif"),
                format = "GTiff",
                overwrite = TRUE)
    

提交回复
热议问题