从光盘读取光栅而不是从存储器读取光栅时,马赛克会失败

时间:2018-02-21 13:45:56

标签: r r-raster r-mosaic

我试图用数百个栅格制作马赛克时遇到了一个奇怪的问题。我正在使用的卫星图像未完全对齐或分享完全相同的分辨率,因此我按照找到的步骤here重新采样我的栅格然后镶嵌它们。

我开始测试仅四个图像的子集并且没有问题这样做(必须手动计算自unionExtent以来的完整范围而较新的union仅允许两个范围参数):< / p>

# 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

我能够通过将mosaic的容差参数设置为0.4来解决这个问题,但仍然不明白为什么rst_resampled1rst_resampled2产生不同{ {1}}结果。

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

0 个答案:

没有答案