2

I need to merge together 20 32 bit tiff. For that I tried ArcGis but it keeps crashing (the individual tiff are about 1GB). I need a finale Tiff in order to run some analysis and I figured that having 20 different file would be messy for the user who is going to do the analysis.

I found a mosaic tool in the raster package in R but I can't really figure out how to use it. My first idea was to create a list of the tiff and then ask the mosaic tool to process it but it gives me error.

Error in function (classes, fdef, mtable) : unable to find an inherited method for function "mosaic", for signature "list", "missing"

I found this script online but it seems that many function doesn't work anymore

require(raster)
input.rasters <- lapply(list.files(pattern="^TmB50.*[.]tif$"), raster)
full.extent <- unionExtent(input.rasters)
bounding.raster <- raster(full.extent,crs=projection(input.rasters[[1]]))
res(bounding.raster) <- res(input.rasters[[5]])
resampled.rasters <- lapply(input.rasters, function(input.raster) {
target.raster <- crop(bounding.raster, input.raster)
resample(input.raster, target.raster, method="bilinear")
})
raster.mosaic <- mosaic(resampled.rasters, fun=max)

source http://www.nceas.ucsb.edu/scicomp/usecases/createrasterimagemosaic

Any idea how I could do that?

1
  • my advise is: don't even try to do it in R ! you can't mosaic 20 files with 1Gb each in R, without facing 999999999 diferent problems,
    – Gago-Silva
    Jan 17, 2013 at 19:58

4 Answers 4

2

Just to keep this tread current, I would highly recommend performing raster mosaics using the terra package. I would not argue that R still has its limitations but, I have mosaiced data, using terra, resulting in ~60GB - 240GB float LZW compressed tiff. Trying the mosaic in ArcGIS failed, even with different image subsets in trying to break the problem into smaller chunks. The processing time of terra::mosaic was even tolerable.

Syntactically, it is similar to the raster package aside from the raster object being a SpatRast class read using terra::rast. One nice addition is being able to leverage an image collection object, think stack/brick without the matching extent constraints.

For illustration, here is some dummy data.

library(terra)
r1 <- terra::rast(xmin=-110, xmax=-80, ymin=40, ymax=70, 
           ncols=30, nrows=30)
  terra::values(r1) <- runif(ncell(r1))
r2 <- terra::rast(xmin=-85, xmax=-55, ymax=60, ymin=30, 
           ncols=30, nrows=30)
  terra::values(r2) <- runif(ncell(r2))
r3 <- terra::rast(xmin=-60, xmax=-30, ymax=50, ymin=20, 
          ncols=30, nrows=30)
  terra::values(r3) <- runif(ncell(r3))

Here we use terra::src to create an image collection. When passed to the terra::mosaic function, it replaces the need for do.call. Note the use of a nested list call in creating the image collection.

rsrc <- terra::src(list(r1, r2, r3))
  plot( m <- mosaic(rsrc) )
0

I would advise against image processing with R. Rather, I would revisit mosaicing your imagery with ArcGIS. I used the following model recently to mosaic approximately 40 1m CIR raster images into a 25 GB mosaic (shown below). ArcGIS is definitely capable of large scale processing if you do it correctly. A few ideas:

  • Make sure to set the raster storage environment for compression, building pyramids and calculating statistics.
  • You'll likely have better success using Mosaic To New Raster rather than Mosaic (Data Management).
  • Run 64 bit background geoprocessing for ArcGIS 10.1 SP1 if you are still encountering issues, available here.

I certainly do not want to dissuade you from raster processing with R, however, I think you will find a host of other issues when forcing R to process big raster data.

enter image description here

enter image description here

4
  • Thank you for the answer, I give another try then. What do you mean by Make sure to set the raster storage environment for compression, building pyramids and calculating statistics. My raster doesnt have the same resolution should I fix that beforehand? Jan 17, 2013 at 20:08
  • Right click on the Mosaic To New Raster tool in the model, select Properties and place a check next to Raster Storage box. When you check that box, by default full pyramids will be built, statistics will be calculated and LZ77 compression will be implemented. These are primarily used so that you can efficiently view the mosaic after it is created. What is your raster resolution now?
    – Aaron
    Jan 17, 2013 at 20:18
  • The resolution is about 0.5m/pixel Jan 17, 2013 at 21:19
  • @user10918 I should also point out that the inputs must have the same number of bands and bit depth for the tool to work.
    – Aaron
    May 16, 2013 at 17:15
0

If you want to apply the mosaic function to a list of raster objects, you can always use the do.call function. Below is an example using 3 simple raster objects, each having all pixel values equal to 1:

        library(raster)

        #Create raster layers.
        r1<- raster(nrows=108, ncols=21, xmn=0, xmx=10, vals = 1)
        r2<- raster(nrows=108, ncols=21, xmn=0, xmx=10, vals = 1)
        r3<- raster(nrows=108, ncols=21, xmn=0, xmx=10, vals = 1)

        #Make list object containing those raster objects.
        x <- list(r1, r2, r3)

        #Add other arguments for mosaic function to the list.
        x$fun <- sum
        x$na.rm <- TRUE

        #Use do.call to apply the resulting "list of arguments" to the mosaic 
        #function.
        x.mosaic <- do.call(mosaic, x)
0

I wonder if 32 bit is needed? You can save a lot of space if you can go from 32-bit floating point to 8-bit integer. For example, 0 - 1 could be scaled from 0 to 100 as 8-bit integer using integers if you don't need the decimals. Similarly, you could scale -1 to 1 as -100 to 100. You also want to consider resampling to a coarser spatial scale if it is feasible for your question. If nothing else resampling to coarser scales can be used as a technique for estimating the amount of time needed for processing large files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.