[英]Using mosaic in r for merge multiple geotiff
I have 50 geotiff files in the same folder.我在同一个文件夹中有 50 个 geotiff 文件。 All of them represented elevation data in part of the world.
所有这些都代表了世界部分地区的高程数据。 I would like to merge certain geotiff files, and I found mosaic in R may help us.
我想合并某些 geotiff 文件,我发现 R 中的马赛克可能对我们有帮助。 I have moved those geotiff into the same folder, and I wrote a R script show below:
我已将这些 geotiff 移动到同一个文件夹中,并编写了如下所示的 R 脚本:
setwd()
a<-lista.files(pattern="*.tiff",file.name=TRUE)
combind<-merge(a,fun=mean)
However, this script returned an error: error in as.data.frame(y)但是,此脚本返回错误:as.data.frame(y) 中的错误
May I ask how could I improve my script?请问我如何改进我的脚本?
You can make use of the powerful GDAL
functions.您可以使用强大的
GDAL
功能。 From my experience these are much faster than pure R code.根据我的经验,这些比纯 R 代码快得多。
My approach would be with library(gdalUtils)
:我的方法是使用
library(gdalUtils)
:
First, build a virtual raster file ( vrt ):首先,构建一个虚拟光栅文件( vrt ):
library(gdalUtils)
setwd(...)
gdalbuildvrt(gdalfile = "*.tif", # uses all tiffs in the current folder
output.vrt = "dem.vrt")
Then, copy the virtual raster to a actual physical file:然后,将虚拟光栅复制到实际的物理文件中:
gdal_translate(src_dataset = "dem.vrt",
dst_dataset = "dem.tif",
output_Raster = TRUE # returns the raster as Raster*Object
# if TRUE, you should consider to assign
# the whole function to an object like dem <- gddal_tr..
options = c("BIGTIFF=YES", "COMPRESSION=LZW"))
Another pure (and probably slower ) raster
package solution would be:另一个纯(可能更慢)
raster
包解决方案是:
f <- list.files(path = "your/path", pattern = ".tif$", full.names = TRUE)
rl <- lapply(f, raster)
do.call(merge, c(rl, tolerance = 1))
you have to adjust the tolerance
since the raster files will probably not have the same origin.您必须调整
tolerance
因为光栅文件可能不会具有相同的原点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.