Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create raster mosaic using list of rasters?

Tags:

r

mosaic

raster

I need to create several raster mosaics. I'm using Package raster version 2.0-31 on a 64bits windows computer. I believe I did my homework checking through all possible blogs and asking this question to some colleagues, but still can't find a solution.

The problem I have is that I can't create a mosaic if my grids are listed in a raster object. I found this example that I though I could apply, but not, I get a weird error message. The example below represents my problem:

r <- raster()
r1 <- crop(r, extent(-10, 10, -10, 10))
r2 <- crop(r, extent(0, 20, 0, 20))
r3 <- crop(r, extent(10, 30, 10, 30))

r1[] <- 1:ncell(r1)
r2[] <- 1:ncell(r2)
r3[] <- 1:ncell(r3)
rasters1 <- list(r1, r2, r3)

mos <- mosaic(rasters1,fun=mean)

This is the error I get:

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

I also tried the function suggested in here, but didn't work either.

fmerge <- function(rasters1, fun, ...){
  ex <- raster(union(rasters1))
  res(ex) <- res(rasters1[[1]])
  for( i in 1:length(rasters1) )
    rasters[[i]] <- merge(rasters1[[i]], ex)
  rasters <- stack(rasters1)
  fun(rasters1, ...)
}

rfm <- fmerge(rasters1, mean, na.rm=T)

This is the error message:

Error in raster(union(rasters1)) : error in evaluating the argument 'x' in selecting a method for function 'raster': Error in as.vector(y) : argument "y" is missing, with no default

like image 999
LuluPor Avatar asked Mar 08 '13 06:03

LuluPor


People also ask

How do I add rasters to a mosaic dataset?

Open the Add Rasters To Mosaic Dataset tool. Do this in ArcCatalog or the Catalog window by right-clicking a mosaic dataset and clicking Add Rasters. Drag and drop a folder or dataset onto a mosaic dataset. Navigate through the system toolboxes to the Add Rasters To Mosaic Dataset tool.

What is a raster mosaic dataset?

A mosaic dataset consists of many parts: A catalog that provides the source of the pixels and footprints of the rasters. A feature class that defines the boundary. A set of mosaicking rules that are used to dynamically mosaic the rasters. A set of properties used to control the mosaicking and any image extraction.


1 Answers

This seems to be a regression in newer versions of raster. Your example code runs as expected in raster 1.9-70 (and R 2.13.1) but gives the same error as you receive in raster 2.0-41 (ad R 2.15.3). You may wish to email the maintainer Robert J. Hijmans to point this out.

In the meantime, this problem can be worked around. Looking at the difference in code between mosaic in raster 1.9-70 and mosaic in raster 2.0-41, you can see that the method that accepted a list has been removed. Instead, there is now only a method that accepts individual rasters. So if you have lots of rasters, you are meant to call the function like this:

mos1 <- mosaic(r1, r2, r3, fun=mean)

This isn't very convenient, however, if you are building your list of rasters to mosaic dynamically. R does have a helper function to help you in this kind of situation, do.call. What do.call does is take a function and a list, and it calls that function using the items in the list as arguments. So you can use this, as long as you add fun=mean to your argument list:

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

You can double check that these two methods give the same results:

stopifnot(identical(mos1, mos2))

This can be wrapped into a simple convenience function that and bound to the relevant call signature, so your original code will work unmodified:

setMethod('mosaic', signature(x='list', y='missing'), 
function(x, y, fun, tolerance=0.05, filename=""){
    stopifnot(missing(y))
    args <- x
    if (!missing(fun)) args$fun <- fun
    if (!missing(tolerance)) args$tolerance<- tolerance
    if (!missing(filename)) args$filename<- filename
    do.call(mosaic, args)
})
like image 161
fmark Avatar answered Sep 20 '22 06:09

fmark