如何在R中高效读取与矢量数据相交的栅格数据的一部分?

huangapple go评论121阅读模式
英文:

How to efficiently read part of raster data intersecting with vector data in R?

问题

给定一个大型栅格数据集(ras)(最好是云优化的GeoTIFF)在投影A中,以及一个在投影B中覆盖ras的一小部分的矢量数据集(vec)。在R中,获得与vec相交的栅格数据的高效方式是什么?

一种方法是读取数据,重新投影数据,裁剪数据,然后进行遮罩处理。

ras <- terra::rast(rasterpath)
vec <- sf::st_read(vectorpath)

ras_reproject <- terra::project(ras, terra::crs(vec))
ras_crop <- terra::crop(ras_reproject, vec)

但是重新投影需要很长时间,而且我不想将整个栅格数据集加载到内存中。

是否有包含类似于read_raster(rasterpath, extent=vec)功能的软件包?

英文:

Given a large raster dataset (ras) (ideally Cloud optimized GeoTIFF) in projection A and a vector dataset (vec) in projection B covering a small part of ras. What is an efficient way to get the raster data intersecting with vec in R?

One way would be to read the data, reproject it, crop it, mask it.

ras &lt;- terra::rast(rasterpath)
vec &lt;- sf::st_read(vectorpath)

ras_reproject &lt;- terra::project(ras, terra::crs(vec))
ras_crop &lt;- terra::crop(ras_reproject , vec)

But reprojection takes forever and I do not want to load the entire raster dataset in Memory.

Is there package with a function like read_raster(rasterpath, extent=vec)?

答案1

得分: 1

我不确定这是否在所有情况下都有效,但{{ezwarp}}库和基于gdal构建的{{vapour}}库似乎已经做到了我想要的。

library(ezwarp)

ras_crop <- ezwarp(
  x = rasterpath, y = rasterpath, res = 10, cutline = vec,
  crop_to_cutline = TRUE, engine = "vapour"
)
英文:

I am not sure if this works out in all situations yet,
but the {{ezwarp}} library and the underlying {{vapour}} library which builds on gdal kind of did what I wanted

library(ezwarp)

ras_crop &lt;- ezwarp(
  x = rasterpath, y = rasterpath, res = 10, cutline = vec,
  crop_to_cutline = TRUE, engine = &quot;vapour&quot;
)

huangapple
  • 本文由 发表于 2023年7月27日 18:32:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778859.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定