【问题标题】:How to merge 101 rasters files in R? [closed]如何在 R 中合并 101 个栅格文件? [关闭]
【发布时间】:2023-03-27 08:14:01
【问题描述】:

我想将 101 个光栅文件合并为一个扩展名为 .geoTIFF 的文件。我怎样才能在 R 工作室中做到这一点?我在一个带有 .asc 扩展名的文件夹中有文件。

【问题讨论】:

  • geoTIFF 文件有什么特别之处吗?或者您一般需要一个简单连接二进制文件的解决方案?
  • 这些是分辨率为 1x1m 的像素,我需要将它们合并成一个大的 geoTIFF

标签: r gis raster


【解决方案1】:

下面的代码将 .asc 文件合并为一个文件。如果 geoTIFF 文件需要的不仅仅是合并单个文件 - 您应该提供该信息。

con_write <- file('newfile.geotiff','ab')

max_file_size <- 1e6L # you may need to replace this number to something bigger then the size of a biggest single .asc file

for (this_file in list.files('path/to/your/files', '*.asc', full.names = T)){
  con_read <- file(this_file, 'rb')
  content <- readBin(con.read, 'raw', max_file_size)
  close(con_read)
  
  writeBin(content, con_write)
}

close(con_write)

【讨论】:

    【解决方案2】:

    您可以在?raster::merge 的示例中看到它是如何完成的。如果这不能说明您需要做什么,那么您至少应该在该示例的基础上构建并显示您遇到的问题。

    要获取文件名,您可以执行以下操作

    fasc <- list.files(pattern="\\.asc$")
    

    创建RasterLayer 对象

    library(raster)
    x <- lapply(fasc, raster)
    

    现在您有了一个包含 RasterLayers 的列表。让我们为示例创建一个

    r1 <- raster(xmx=-150, ymn=60, ncols=30, nrows=30)
    values(r1) <- 1:ncell(r1)
    r2 <- raster(xmn=-100, xmx=-50, ymx=50, ymn=30)
    res(r2) <- c(xres(r1), yres(r1))
    values(r2) <- 1:ncell(r2)
    x <- list(r1, r2)
    

    还有这个列表:

    x$filename <- 'test.tif'
    m <- do.call(merge, x)
    
    m
    #class      : RasterLayer 
    #dimensions : 60, 130, 7800  (nrow, ncol, ncell)
    #resolution : 1, 1  (x, y)
    #extent     : -180, -50, 30, 90  (xmin, xmax, ymin, ymax)
    #crs        : +proj=longlat +datum=WGS84 +no_defs 
    #source     : test.tif 
    #names      : test 
    #values     : 1, 1000  (min, max)
    

    本示例假设所有数据源都具有相同的原点空间分辨率

    【讨论】:

      猜你喜欢
      • 2013-03-30
      • 1970-01-01
      • 2015-09-13
      • 2019-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      相关资源
      最近更新 更多