[英]Error in mosaic of rasters from different extent using terra package in R?
I have a folder that contains around 191 GeoTIFF files (each file is a different DEM (elevation) tile of a much larger area).我有一个文件夹,其中包含大约 191 个 GeoTIFF 文件(每个文件都是更大区域的不同 DEM(高程)图块)。 I want to merge all the tiles into one raster file.
我想将所有图块合并到一个光栅文件中。 I am using the
terra
package and was successfully able to load each raster and aggregate them from 2 metre resolution to 30 metre resolution.我正在使用
terra
package 并成功加载每个栅格并将它们从 2 米分辨率聚合到 30 米分辨率。 However, when running the mosaic
function to merge them all, I run into an error (see error message below).但是,当运行
mosaic
function 将它们全部合并时,我遇到了一个错误(请参阅下面的错误消息)。 I have been able to run the mosaic function on a smaller subset of just three tiles, but when I scale up to all the files, this becomes an issue.我已经能够在只有三个图块的较小子集上运行马赛克 function,但是当我扩展到所有文件时,这成为一个问题。
By calling the summary of the rasters (see below), the aggregation does slightly change the extent - could this be the issue?通过调用栅格的摘要(见下文),聚合确实会稍微改变程度 - 这可能是问题吗?
resample
might be an option, but each individual raster has a different extent and I'm not exactly sure how to implement this fix. resample
可能是一个选项,但每个单独的栅格都有不同的范围,我不确定如何实施此修复。
Not sure a sample data set would help since I know the functions work.不确定示例数据集是否会有所帮助,因为我知道这些功能有效。 I am running this code on a high-performance cluster so it's not very efficient to run small batches of code.
我在高性能集群上运行此代码,因此运行小批量代码效率不高。
library(terra)
files <- as.list(list.files("./DEM_tiles", full.names = TRUE))
raster.list <- lapply(files, rast)
for(i in 1:length(raster.list)){
raster.list[[i]] <- aggregate(raster.list[[i]], fact = 15)
}
raster.mosaic <- do.call(mosaic, raster.list)
> Error: [mosaic] internal error: extents do not match ()
> Execution halted
Below is an example of what two of the tiles look like:下面是两个图块的示例:
### Before Aggregation
[[1]]
class : SpatRaster
dimensions : 25000, 25000, 1 (nrow, ncol, nlyr)
resolution : 2, 2 (x, y)
extent : -1800000, -1750000, -6e+05, -550000 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
source : 35_23_1_1_2m_v3.0_reg_dem.tif
name : 35_23_1_1_2m_v3.0_reg_dem
[[2]]
class : SpatRaster
dimensions : 25000, 25000, 1 (nrow, ncol, nlyr)
resolution : 2, 2 (x, y)
extent : -1800000, -1750000, -550000, -5e+05 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
source : 35_23_1_2_2m_v3.0_reg_dem.tif
name : 35_23_1_2_2m_v3.0_reg_dem
### After Aggregation
[[1]]
class : SpatRaster
dimensions : 1667, 1667, 1 (nrow, ncol, nlyr)
resolution : 30, 30 (x, y)
extent : -1800000, -1749990, -600010, -550000 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
source : memory
name : 35_23_1_1_2m_v3.0_reg_dem
min value : -15.62178
max value : 233.6489
[[2]]
class : SpatRaster
dimensions : 1667, 1667, 1 (nrow, ncol, nlyr)
resolution : 30, 30 (x, y)
extent : -1800000, -1749990, -550010, -5e+05 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
source : memory
name : 35_23_1_2_2m_v3.0_reg_dem
min value : -15.27713
max value : 243.0772
I can reproduce the message like this我可以像这样重现消息
library(terra)
r1 <- rast(nrow=1667, ncol=1667, ext=c(-1800000, -1749990, -600010, -550000))
r2 <- rast(nrow=1667, ncol=1667, ext=c(-1800000, -1749990, -550010, -5e+05))
values(r1) <- 1:ncell(r1)
values(r2) <- 1:ncell(r2)
m <- mosaic(r1, r2)
#Error: [mosaic] internal error: extents do not match ()
And merge
fails too. merge
也失败了。 This is a bug that I will look into.这是我将研究的错误。 It works in
raster
它适用于
raster
library(raster)
x1 <- raster(r1)
x2 <- raster(r2)
m <- mosaic(x1, x2, fun=mean)
mm <- merge(x1, x2)
Later:之后:
The error was because the rasters were not aligned.错误是因为栅格未对齐。 With
terra
1.2-1 (currently the development version ) I now get使用
terra
1.2-1 (目前是开发版本)我现在得到
library(terra)
#terra version 1.2.1
crs <- "+proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +x_0=0 +y_0=0 +datum=WGS84 +units=m"
r1 <- rast(nrow=1667, ncol=1667, ext=c(-1800000, -1749990, -600010, -550000), crs=crs)
r2 <- rast(nrow=1667, ncol=1667, ext=c(-1800000, -1749990, -550010, -5e+05), crs=crs)
values(r1) <- 1:ncell(r1)
values(r2) <- 1:ncell(r2)
m <- mosaic(r1, r2)
#Warning message:
#[mosaic] rasters did not align and were resampled
m
#class : SpatRaster
#dimensions : 3334, 1667, 1 (nrow, ncol, nlyr)
#resolution : 30, 30 (x, y)
#extent : -1800000, -1749990, -600010, -499990 (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
#source : memory
#name : lyr.1
#min value : 1
#max value : 2778889
Also还
@Elia's suggestion is a good work-around: @Elia 的建议是一个很好的解决方法:
r1 <- writeRaster(r1, "test1.tif", overwrite=TRUE)
r2 <- writeRaster(r2, "test2.tif", overwrite=TRUE)
v <- vrt(c("test1.tif", "test2.tif"), "test.vrt", overwrite=TRUE)
v
#class : SpatRaster
#dimensions : 3334, 1667, 1 (nrow, ncol, nlyr)
#resolution : 30, 30 (x, y)
#extent : -1800000, -1749990, -600020, -5e+05 (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
#source : test.vrt
#name : test
#min value : 1
#max value : 2778889
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.