我最近一直在使用 fasterize 包将 sf 多边形转换为栅格:
https://cran.rstudio.com/web/packages/fasterize/fasterize.pdf
当我处理大文件时,最好直接写入磁盘而不是内存。因此,例如,与其这样做:
Fasterize(polygon_file, raster_template, field = 'value'(
我会这样做:
Fasterize(polygon_file, raster_template, field = 'value', file = 'output.tif'(
这似乎是不可能的。有人对如何做到这一点有建议吗?
谢谢。
你有没有考虑过 R "gdalUtils" 库?
gdal_rasterize(( 函数执行的操作与 fasterize BUT 将栅格对象输出到文件的操作相同。根据我的经验,gdalUtils实际上比光栅更快,即使在使用fasterize时也是如此。
这是他们文档中的示例
gdal_setInstallation()
valid_install <- !is.null(getOption("gdalUtils_gdalPath"))
if(require(raster) && require(rgdal) && valid_install)
{
# Example from the original gdal_rasterize documentation:
# gdal_rasterize -b 1 -b 2 -b 3 -burn 255 -burn 0
# -burn 0 -l tahoe_highrez_training tahoe_highrez_training.shp tempfile.tif
dst_filename_original <- system.file("external/tahoe_highrez.tif", package="gdalUtils")
# Back up the file, since we are going to burn stuff into it.
dst_filename <- paste(tempfile(),".tif",sep="")
file.copy(dst_filename_original,dst_filename,overwrite=TRUE)
#Before plot:
plotRGB(brick(dst_filename))
src_dataset <- system.file("external/tahoe_highrez_training.shp", package="gdalUtils")
tahoe_burned <- gdal_rasterize(src_dataset,dst_filename,
b=c(1,2,3),
burn=c(0,255,0),
l="tahoe_highrez_training",
verbose=TRUE,
output_Raster=TRUE)
#After plot:
plotRGB(brick(dst_filename))
}
如果移除数据帧"tahoe_burned",则甚至不必将栅格加载到内存中;
gdal_rasterize(src_dataset,dst_filename,
b=c(1,2,3),
burn=c(0,255,0),
l="tahoe_highrez_training",
verbose=TRUE,
output_Raster=TRUE)
关键是要指定output_Raster=TRUE
我希望这能回答你的问题。