我想从包含农药使用的光栅文件中提取一些施用率、总施用量和面积。我有很多文件需要这样做,而光栅速度很慢,所以我需要在 terra 中执行此操作,但努力复制遮罩功能。
这是栅格中的代码(从此处复制:使用栅格计算农药的平均施用量和总施用量,但数字不相加):
library(tidyverse)
## With raster ----
data(wrld_simpl)
r <- raster::raster("https://raw.github.com/hansronald/Pesticide-data/master/APR_Soybean_Glyphosate_2015_L.tif")
r <- raster::clamp(r, lower=0, useValues=FALSE)
# area in ha
a <- raster::area(r) * 100
## Get the mean application rate
mean_app <- raster::extract(r, wrld_simpl, fun = mean, na.rm = TRUE)
rtot <- r * a
## Get the total application for each country
tot_app <- raster::extract(rtot, wrld_simpl, fun = sum, na.rm = TRUE)
## Get the total area for each country
rarea <- mask(a, r)
tot_area <- raster::extract(rarea, wrld_simpl, fun = sum, na.rm = TRUE)
在土地
## Terra ----
data(wrld_simpl)
wrld_simpl = vect(wrld_simpl)
r <- terra::rast("https://raw.github.com/hansronald/Pesticide-data/master/APR_Soybean_Glyphosate_2015_L.tif")
r <- terra::clamp(r, lower=0, values=FALSE)
# area in ha
a <- terra::area(r) * 100
mean_app <- terra::extract(r, wrld_simpl, fun = mean, na.rm = TRUE)
rtot <- r * a
tot_app <- terra::extract(rtot, wrld_simpl, fun = sum, na.rm = TRUE)
rarea <- terra::mask(a, r)
tot_area <- terra::extract(rarea, wrld_simpl, fun = sum, na.rm = TRUE)
但是当我尝试使用掩码时出现此错误:
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘mask’ for signature ‘"numeric", "SpatRaster"’
任何想法如何解决这个问题?