1

数百のラスターからモザイクを作成しようとしたときに、奇妙な問題に遭遇しました。私が使用している衛星画像は完全に位置合わせされていないか、まったく同じ解像度を共有していないため、ここにある手順に従ってラスターをリサンプリングし、モザイク化しました。

私は 4 つの画像のみのサブセットでテストを開始しましたが、これを行うのに問題はありませんでした (手動で完全な範囲を計算する必要がunionExtentあり、新しいunionものでは 2 つの範囲引数しか許可されていません)。

# 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結果が得られました。

compareRaster両方をand と比較するとcellStats、まったく同じであることがわかります。

4

0 に答える 0