1

尝试从数百个栅格制作马赛克时遇到了一个奇怪的问题。我使用的卫星图像没有完全对齐或具有完全相同的分辨率,因此我按照此处找到的步骤重新采样我的栅格,然后对其进行镶嵌。

我开始对只有四个图像的子集进行测试,并且这样做没有问题(必须手动计算完整范围unionExtent,因为新版union只允许两个范围参数):

# Reading raster files
rst <- lapply(list.files(), FUN = stack)

# Extracting individual extents
rst_ext <- lapply(rst, FUN =  extent)

# Calculating full extent
xmin_rst <- c(); xmax_rst <- c(); ymin_rst <- c(); ymax_rst <- c();
for (i in 1:length(rst_ext)) {
    xmin_rst <- c(xmin_rst, rst_ext[[i]]@xmin)
    ymin_rst <- c(ymin_rst, rst_ext[[i]]@ymin)
    xmax_rst <- c(xmax_rst, rst_ext[[i]]@xmax)
    ymax_rst <- c(ymax_rst, rst_ext[[i]]@ymax)
}
full_extent <- extent(min(xmin_rst), max(xmax_rst),
                      min(ymin_rst), max(ymax_rst))

# Creating raster from full extent and first rasters' CRS and resolution
bounding_rst <- raster(full_extent, 
                   crs = crs(rst[[1]]),
                   res = res(rst[[1]]))

# Resampling rasters to match attributes of the bounding raster
rst_resampled <- lapply(X = rst, fun = function(x) {
    target_rst <- crop(bounding_rst, x)
    resample(x, target_rst, method="bilinear")
})

# Creating mosaic
rst_mosaic <- do.call("mosaic", c(rst_resampled, fun = mean))

结果没问题,但是当然,我不想将所有这些栅格保存在我的记忆中,因为我已经用完了。我决定将它们保存在一个新文件夹中并将它们重新读取为stack,然后制作马赛克。

# Function to crop, resample and write to a new GeoTIFF
resample_write <- function(x) {
  target_rst <- crop(bounding_rst, x)
  x <- resample(x, target_rst, method="bilinear")
  save_name <- gsub("\\.1", 
                    "_resampled.tif", 
                    names(x)[1]) # Modifying name of 1st band
  writeRaster(x,
              filename = paste("../testing_resampling/", 
                               save_name, sep = ""),
              format = "GTiff")
}

# Running the function
lapply(rst, FUN = resample_write)

# Reading resampled images
setwd("../testing_resampling/")
rst_resampled2 <- lapply(list.files(), FUN = stack)

## Making the mosaic
rst_mosaic2 <- do.call("mosaic", c(rst_resampled2, fun = mean))

这给出了以下错误:

> rst_mosaic2 <- do.call("mosaic", c(rst_resampled2, fun = mean))
Error in compareRaster(x, extent = FALSE, rowcol = FALSE, orig = TRUE,  : 
  different origin

我能够通过设置增加 to 的容差参数来解决它,mosaic0.4仍然不明白为什么rst_resampled1rst_resampled2产生不同的mosaic结果。

将它们与它们进行比较compareRastercellStats告诉我它们完全相同。

4

0 回答 0