r - 有没有办法使用 terra 包中的 rast 函数从带有缓冲区的栅格中提取数据?
问题描述
我正在使用光栅包从光栅中提取一些坐标的值。我打算使用 1 公里的缓冲区进行提取。但是在 terra pakcage 中使用提取功能似乎是不可能的。
如果您能帮助我,将不胜感激。
解决方案
只需将坐标转换为点,然后在它们周围创建一个缓冲区这里是具有不同包的基准:
library(terra)
library(raster)
library(exactextractr)
library(sf)
#create example raster
r <- raster(ncol=36, nrow=18, vals=1:(18*36))
# extract values with coordinates (converted to points)
#create some coordinates
xy <- cbind(-50, seq(-80, 80, by=20))
#convert to points
sp <- SpatialPoints(xy)
#create a buffer around the points
sp_buffer <-st_buffer(st_as_sf(sp),20)
#extract ----------------------------------------------------------------
#raster
raster::extract(r, sp_buffer)
#terra
terra::extract(r, sp_buffer)
#exactextract
exactextractr::exact_extract(r,sp_buffer)
library(microbenchmark)
microbenchmark(raster=raster::extract(r, sp_buffer),
terra=terra::extract(r, sp_buffer),
exactextract=exactextractr::exact_extract(r,sp_buffer)
)
Unit: milliseconds
expr min lq mean median uq max neval cld
raster 56.0899 59.19100 65.547358 60.64650 62.12665 300.6713 100 b
terra 56.3695 58.90810 65.549521 60.47245 62.22330 179.3679 100 b
exactextract 8.0596 8.53215 9.447963 8.71500 8.91085 81.8115 100 a
如您所见exact_extract
,这是从多边形中提取值的最快函数(快 7 倍)
推荐阅读
- xpath - 使用 Google 表格 importxml 函数从同义词库中获取单词的同义词
- mysql - MySql - 在选择查询中使用变量,而 where 条件又增加了一行
- tensorflow - 我如何知道内置的 TensorFlow 函数是否可微分?
- gensim - 有没有办法使用矩阵乘法从 gensim LDA 预训练模型中推断出未见文档的主题分布?
- angular - 角度 api 未执行
- elasticsearch - 在elasticsearch中,如何查询日期字段为任何时间的所有文档 Jan 1 2020 Pacific Time zone
- python - 如何关闭 mypy 检查没有 .py 文件的文件夹
- debugging - 我无法通过 Google Cloud Platform 中的防火墙规则打开某些端口
- android - 在 Firestore 中添加/覆盖数组类型的字段
- vim - Vim - 在命令行中更改光标