10

I am trying to mosaic several rasters into single big raster in R. Using script that posted in https://stackoverflow.com/questions/15287807/how-can-i-create-raster-mosaic-using-list-of-rasters But, I have received a warning message and error message.

rasters1 <- list.files("F:\\MOD15A2_LAI_1km\\MOD15A2_LAI_2009", 
                      pattern = "mod15a2.a2009001.*.005.*.img$", 
                      full.names = TRUE, recursive = TRUE)

mos1 <-mosaic(rasters1, fun=mean)

It was reporting error as below

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘extent’ for signature ‘"character"’

Then I tried another version.

rasters1.mosaicargs <- rasters1
rasters1.mosaicargs$fun <- mean

But here some warning message as below

Warning message:
In rasters1.mosaicargs$fun <- mean : Coercing LHS to a list

I have ignored the message then continued

mos2 <- do.call(mosaic, rasters1.mosaicargs)

but here same error mentioned in as an above

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘mosaic’ for signature ‘"character", "character"’
17

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)
1

Just a minor variation on the theme. You can avoid the creation of a blank list and the for loop...

    rast.list <- list()

    for(i in 1:length(rasters1)) { 
rast.list[i] <- raster(rasters1[i])
}

...with an lapply command.

    rast.list <- lapply(1:length(rasters1),
 function(x) {
raster(rasters1[x])
})

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.