如何使用栅格列表创建栅格马赛克? [英] How can I create raster mosaic using list of rasters?
问题描述
我需要创建几个光栅马赛克.我在 64 位 Windows 计算机上使用 Package raster version 2.0-31.我相信我做了功课,检查了所有可能的博客并向一些同事提出了这个问题,但仍然找不到解决方案.
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)
这是我得到的错误:
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)
这是错误信息:
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
推荐答案
这似乎是较新版本的光栅的回归.您的示例代码在 raster 1.9-70(和 R 2.13.1)中按预期运行,但给出的错误与您在 raster 2.0-41(ad R 2.15.3)中收到的错误相同.您可能希望通过电子邮件向维护者 Robert J. Hijmans 指出这一点.
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.
与此同时,这个问题是可以解决的.查看光栅 1.9-70 中的马赛克 和 mosaic in raster 2.0-41,可以看到接受列表的方法被移除了.相反,现在只有一种方法可以接受单个栅格.所以如果你有很多栅格,你应该像这样调用函数:
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)
这不是很方便,但是,如果您正在构建要动态镶嵌的栅格列表.R 确实有一个辅助函数可以在这种情况下为您提供帮助,do.call
.do.call
所做的是接受一个函数和一个列表,并使用列表中的项目作为参数调用该函数.所以你可以使用它,只要你将 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)
})
这篇关于如何使用栅格列表创建栅格马赛克?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!