Merging multiple rasters in R

后端 未结 7 1249
傲寒
傲寒 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 19:57

    The 'merge' function from the Raster package is a little slow. For large projects a faster option is to work with gdal commands in R.

    library(gdalUtils)
    library(rgdal)
    

    Build list of all raster files you want to join (in your current working directory).

    all_my_rasts <- c('r1.tif', 'r2.tif', 'r3.tif')
    

    Make a template raster file to build onto. Think of this a big blank canvas to add tiles to.

    e <- extent(-131, -124, 49, 53)
    template <- raster(e)
    projection(template) <- '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'
    writeRaster(template, file="MyBigNastyRasty.tif", format="GTiff")
    

    Merge all raster tiles into one big raster.

    mosaic_rasters(gdalfile=all_my_rasts,dst_dataset="MyBigNastyRasty.tif",of="GTiff")
    gdalinfo("MyBigNastyRasty.tif")
    

    This should work pretty well for speed (faster than merge in the raster package), but if you have thousands of tiles you might even want to look into building a vrt first.

提交回复
热议问题