我有一个高分辨率栅格 (highRes) 和一个较低分辨率栅格 (lowRes),我想为每个低分辨率像元返回高分辨率像元在每个低分辨率像元中的值和覆盖百分比.
我可以使用以下代码来完成此操作,并且它有效。在这里,我逐个单元格地将每个单元格转换为多边形,以确定哪些高分辨率单元格与该多边形相交。但对于大栅格来说,速度非常慢。对于我正在从事的项目,高分辨率栅格的全局分辨率为 1x1km,低分辨率栅格的分辨率为 25x25km。
我想知道是否有更快的方法来解决这个问题。
library(terra)
highRes <- rast()
res(highRes) <- c(0.1, 0.1)
highRes[] <- sample(1:10, ncell(highRes), replace = TRUE)
lowRes <- rast(res = c(1, 1), xmin = -170, xmax = -50, ymin = 0, ymax = 80)
lowRes[] <- 1
lowRes <- shift(lowRes, dx = 0.02, dy = 0.03) # not aligned
for (i in 1:ncell(lowRes)) {
xx <- rast(lowRes, vals = NA)
xx[i] <- 1
p <- as.polygons(xx)
p <- project(p, crs(highRes))
e <- extract(highRes, p, weights = TRUE, exact = TRUE)
}
这为我提供了与低分辨率像元重叠的像元值,以及这些像元的覆盖百分比。
使用
exactextractr::exact_extract()
:
library(terra)
library(sf)
library(exactextractr)
# Your data
set.seed(1)
highRes <- rast()
res(highRes) <- c(0.1, 0.1)
highRes[] <- sample(1:10, ncell(highRes), replace = TRUE)
lowRes <- rast(res = c(1, 1), xmin = -170, xmax = -50, ymin = 0, ymax = 80)
lowRes <- shift(lowRes, dx = 0.02, dy = 0.03)
# Add individual value to each cell
lowRes[] <- 1:ncell(lowRes)
# Covert lowres to sf polygon layer and project to the CRS of highRes
sf_poly <- as.polygons(lowRes) |>
st_as_sf() |>
st_transform(st_crs(highRes))
# Extract values from highRes using sf_poly (non-aggregated list object)
e1 <- exact_extract(highRes, sf_poly, weights = "area")
head(e1[1][[1]])
# value weight coverage_fraction
# 1 10 21412024 0.24
# 2 6 21412024 0.30
# 3 10 21412024 0.30
# 4 1 21412024 0.30
# 5 2 21412024 0.30
# 6 9 21412024 0.30
# For comparison
e <- extract(highRes, sf_poly[1,], weights = TRUE, exact = TRUE)
head(e)
# ID lyr.1 weight
# 1 1 10 0.2408351
# 2 1 6 0.3010438
# 3 1 10 0.3010438
# 4 1 1 0.3010438
# 5 1 2 0.3010438
# 6 1 9 0.3010438