Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

r - How can I create raster mosaic using list of rasters?

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...