Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Raster mosaic from list of rasters?

Tags:

r

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)
like image 359
user3367135 Avatar asked Mar 01 '14 03:03

user3367135


2 Answers

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)
like image 195
foo Avatar answered Nov 04 '22 19:11

foo


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) 
like image 5
Robert Hijmans Avatar answered Nov 04 '22 20:11

Robert Hijmans