How can I create raster mosaic using list of rasters?

橙三吉。 提交于 2019-11-28 07:41:12

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/15287807/how-can-i-create-raster-mosaic-using-list-of-rasters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!