在R中合并多个栅格 [英] Merging multiple rasters in R

查看:702
本文介绍了在R中合并多个栅格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试寻找一种省时的方法来合并R中的多个栅格图像.这些是来自乞力马扎罗山南部地区的相邻ASTER场景,我的目标是将它们放在一起以获得一张大图像.

I've been trying to find a time-efficient way to merge multiple raster images in R. These are adjacent ASTER scenes from the southern Kilimanjaro region, and my target is to put them together to obtain one large image.

这是我到目前为止所得到的(对象"ast14dmo"代表RasterLayer对象的列表):

This is what I got so far (object 'ast14dmo' representing a list of RasterLayer objects):

# Loop through single ASTER scenes
for (i in seq(ast14dmo.sd)) {
  if (i == 1) {
    # Merge current with subsequent scene
    ast14dmo.sd.mrg <- merge(ast14dmo.sd[[i]], ast14dmo.sd[[i+1]], tolerance = 1)
  } else if (i > 1 && i < length(ast14dmo.sd)) {
    tmp.mrg <- merge(ast14dmo.sd[[i]], ast14dmo.sd[[i+1]], tolerance = 1)
    ast14dmo.sd.mrg <- merge(ast14dmo.sd.mrg, tmp.mrg, tolerance = 1)
  } else {
    # Save merged image
    writeRaster(ast14dmo.sd.mrg, paste(path.mrg, "/AST14DMO_sd_", z, "m_mrg", sep = ""), format = "GTiff", overwrite = TRUE)
  }
}

您肯定会猜到,该代码有效.但是,考虑到每个单个栅格对象的大小大约为70 mb,合并需要花费很长时间.我还尝试了Reduce和do.call,但是失败了,因为我无法传递参数"tolerance",从而绕过了光栅文件的不同来源.

As you surely guess, the code works. However, merging takes quite long considering that each single raster object is some 70 mb large. I also tried Reduce and do.call, but that failed since I couldn't pass the argument 'tolerance' which circumvents the different origins of the raster files.

有人知道如何加快处理速度吗?

Anybody got an idea of how to speed things up?

推荐答案

您可以使用do.call

ast14dmo.sd$tolerance <- 1
ast14dmo.sd$filename <- paste(path.mrg, "/AST14DMO_sd_", z, "m_mrg.tif", sep = "")
ast14dmo.sd$overwrite <- TRUE
mm <- do.call(merge, ast14dmo.sd)

这里有一些数据,来自raster::merge

Here with some data, from the example in raster::merge

r1 <- raster(xmx=-150, ymn=60, ncols=30, nrows=30)
r1[] <- 1:ncell(r1)
r2 <- raster(xmn=-100, xmx=-50, ymx=50, ymn=30)
res(r2) <- c(xres(r1), yres(r1))
r2[] <- 1:ncell(r2)

x <- list(r1, r2)
names(x) <- c("x", "y")
x$filename <- 'test.tif'
x$overwrite <- TRUE
m <- do.call(merge, x)

这篇关于在R中合并多个栅格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆