5

I am working from the post here: How can I create raster mosaic using list of rasters? to create a raster mosaic using a list of rasters. The example in the answer given by fmark works perfectly but I get an error when I follow the steps using my own data. Not sure where I am going wrong, any help would be very much appreciated!

R version 2.15.3 (2013-03-01)
Platform: x86_64-unknown-linux-gnu (64-bit)
locale:
[1] C
attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base
other attached packages:
[1] raster_2.2-12 rgdal_0.8-10  sp_1.0-14
loaded via a namespace (and not attached):
[1] grid_2.15.3     lattice_0.20-15 tools_2.15.3

I used the function from How to iterate over a list preserving the format of the results? to generate my raster list.

ListRasters <- function(list_names) {
  raster_list <- list() # initialise the list of rasters
   for (i in 1:(length(list_names))){ 
    grd_name <- list_names[i] # list_names contains all the names of the images in .grd format
    raster_file <- raster(grd_name)
   }
  raster_list <- append(raster_list, raster_file) # update raster_list at each iteration
}

Then I generate my list names and create my raster list from them.

wgs84.tif.list <- list.files(path=mod.dir, pattern=glob2rx("*_wgs84.tif"), full.names=T,recursive=F)

list_names <- NULL
for (i in 1:length(wgs84.tif.list)) {
  list_names <- c(list_names, wgs84.tif.list[i])
}

raster.list <-sapply(list_names, FUN = ListRasters)

raster.list$fun <- mean
mos <- do.call(mosaic, raster.list)

This is the error I get:

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

My raster.list starts off like this (it contains 11 rasters):

 $`/import/c/w/kbennett/MODSCAG/snow-dav.jpl.nasa.gov/modscag-historic/2002/091/MOD09GA.A2002091.h08v03.005.2007124035032snow_fraction_wgs84.tif`
class       : RasterLayer
dimensions  : 2400, 2400, 5760000  (nrow, ncol, ncell)
resolution  : 463.3127, 463.3127  (x, y)
extent      : -11119737, -10007786, 5559984, 6671935  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : /import/c/w/kbennett/MODSCAG/snow-dav.jpl.nasa.gov/modscag-historic/2002/091/MOD09GA.A2002091.h08v03.005.2007124035032snow_fraction_wgs84.tif
names       : MOD09GA.A2002091.h08v03.005.2007124035032snow_fraction_wgs84
values      : 0, 255  (min, max)
Community
  • 1
  • 1
user3367135
  • 131
  • 2
  • 12

4 Answers4

14

My rasters were not named correctly. To rectify this ran, before calling fun on it:

names(rasters.list) <- NULL

Then:

raster.list$fun <- mean
mos <- do.call(mosaic, raster.list)
foo
  • 141
  • 1
  • 3
5

To expand a bit on foo's answer. You can use sapply to create a list of RasterLayer objects.

rlist <- sapply(list_names)

Then add the names of the other arguments. The first ones are 'x' and 'y' (see ?mosaic). However it will also work if they are NULL (as their position will be used).

names(rlist)[1:2] <- c('x', 'y')
rlist$fun <- mean
rlist$na.rm <- TRUE

And now call do.call

x <- do.call(mosaic, rlist) 
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • Thanks Robert! I am wondering if there is a way to mimic the behavior of ArcGIS Mosaic to New raster in R? That is specify order priority when overlapping occur e.g., use top most or last raster https://desktop.arcgis.com/en/arcmap/10.6/tools/data-management-toolbox/mosaic-to-new-raster.htm – Tung Mar 12 '20 at 02:33
  • 1
    For that, you can use `raster::merge` – Robert Hijmans Mar 13 '20 at 03:38
  • Does the solution works for `terra` package also? When using the same code with `terra::rast` function, I am getting `Error: [src] list elements should be 'SpatRaster' is of class: logical`. – UseR10085 Dec 02 '21 at 10:10
  • 1
    For `terra::mosaic` it should be `rlist <- list(x, y, z) rsrc <- src(rlist) m <- mosaic(rsrc)` – moho wu Dec 10 '21 at 11:53
0

how about that? Im noob in R.

lista = list of rasters
mosaicar = function(lista){
  raster = lista[[1]]
  for (i in 2:length(lista)){
    raster1 = mosaic(raster, lista[[i]], fun = max)
    raster = raster1
  }
  return(raster)
}
Nino Filiu
  • 16,660
  • 11
  • 54
  • 84
Arthur
  • 1
  • 1
-1

As mentioned by @Bappa Das above, the provided solution does not work on terra. @moho wu did not mention the na.rm issue. It remains unclear how to pass the na.rm to terra::mosaic. If anywone has a working answer...

Stefano Barbi
  • 2,978
  • 1
  • 12
  • 11