Mosaicking rasters in R?

The issue here is that mosaic and do.call are expecting a raster object in the list and not just character names of the raster that is contained in the "rasters1" vector. You are, in effect, asking to mosaic a name in a vector and not a raster object.

# Create some example data
require(raster)
    r <- raster(ncol=100, nrow=100)
      r1 <- crop(r, extent(-10, 11, -10, 11))
        r1[] <- 1:ncell(r1)
          r2 <- crop(r, extent(0, 20, 0, 20))
          r2[] <- 1:ncell(r2)
      r3 <- crop(r, extent(9, 30, 9, 30))
    r3[] <- 1:ncell(r3)

# If I create a list object of the raster names, as your are doing with list.files, 
#    do.call will fail with a character signature error 
rast.list <- list("r1","r2","r3")   
  rast.list$fun <- mean     
    rast.mosaic <- do.call(mosaic,rast.list)

# However, if I create a list contaning raster objects, the do.call function 
#   will work when mosaic is passed to it.      
rast.list <- list(r1, r2, r3)     
  rast.list$fun <- mean
    rast.mosaic <- do.call(mosaic,rast.list)
      plot(rast.mosaic)

# You could specify a for loop to create a list object, 
#   contaning raster objects
rasters1 <- list.files("F:/MOD15A2_LAI_1km/MOD15A2_LAI_2009", 
                       pattern="mod15a2.a2009001.*.005.*.img$", 
                       full.names=TRUE, recursive=TRUE)
rast.list <- list()
  for(i in 1:length(rasters1)) { rast.list[i] <- raster(rasters1[i]) }

# And then use do.call on the list of raster objects
rast.list$fun <- mean
  rast.mosaic <- do.call(mosaic,rast.list)
    plot(rast.mosaic)

Tags:

R

Raster

Mosaic