简体   繁体   English

了解 raster::extract 和 terra:extract

[英]Understanding raster::extract and terra:extract

I'm having issues fully understanding terra:extract.我在完全理解 terra:extract 时遇到问题。 I wish to extract average raster values for administrative GADM polygons.我希望提取管理 GADM 多边形的平均栅格值。 My raster has one single value per country.我的栅格每个国家/地区都有一个值。 I would expect that each administrative polygon within a particular country has the same value, and some polygons that include some country border be allocated area weighted averages.我希望特定国家/地区内的每个行政多边形具有相同的值,并且包含某些国家边界的某些多边形被分配区域加权平均值。 Unfortunately, this is not the case with my current script.不幸的是,我当前的脚本并非如此。 raster::extract seems to be giving sensible results but not terra:extract (see my sample code below - providing outputs with different values). raster::extract 似乎给出了合理的结果,但不是 terra:extract (请参阅下面的示例代码 - 提供具有不同值的输出)。 Could someone kindly explain me why, in light of my code below?有人可以根据我下面的代码解释我为什么吗? Thank you very much.非常感谢。

## libraries
library(terra)
library(raster)

#===============================================    
## sample example - provides results as expected (1.333, that is (2*0.5+1*1)/1.5)

# sample raster and SpatialPolygons
r <- raster(ncol=2, nrow=3, xmn= 0, ymn= 0, xmx = 30,ymx = 30)
r[] <- c(2, 2, 2, 1, NA, NA)
cds <- rbind(c(7.5,0), c(7.5,20), c(30, 20),c(30,10))
library(sp)
p = Polygon(cds)
ps = Polygons(list(p),1)
sps = SpatialPolygons(list(ps))
plot(r)
plot(sps, add=T)

在此处输入图像描述

# test raster package
test1 <- raster::extract(r , sps, fun=mean, na.rm=T,  weights=TRUE) 
test1 # I get 1.333333 which is what I would expect

# test terra package
sps.spatv <- vect(sps)
r.spatR <-  rast(r) #conversion to SpatRaster class

test2 <- terra::extract(r.spatR, sps.spatv, fun=mean, na.rm=T,  weights=TRUE, exact=TRUE, touches=TRUE) 
test2 # I get 1.333333 which is what I would expect  

#===============================================    
## sample code that leads to different results between raster and terra packages - I wish to understand why such difference.
# sample SpatialPolygonsDataFrame 
ETH <- getData("GADM", country = 'ETH', level = 2)
SOM <- getData("GADM", country = 'SOM', level = 2)
sps <- bind(ETH, SOM)

# sample raster stack
ra <- raster(ncol=31, nrow=24, xmn= 33.3, ymn=  3.67, xmx = 47.5, ymx = 14.65, crs=crs(sps) )
ra[] <- rep(10, 24*31)
ra2 <- raster(ncol=31, nrow=24, xmn= 33.3, ymn= -7.31 , xmx = 47.5, ymx = 3.67, crs=crs(sps) )
ra2[] <- rep(20, 24*31)
ra3 <- merge(ra, ra2)

rb <- raster(ncol=31, nrow=24, xmn= 33.3, ymn=  3.67, xmx = 47.5, ymx = 14.65, crs=crs(sps) )
rb[] <- rep(35, 24*31)
rb2 <- raster(ncol=31, nrow=24, xmn= 33.3, ymn= -7.31 , xmx = 47.5, ymx = 3.67, crs=crs(sps) )
rb2[] <- rep(45, 24*31)
rb3 <- merge(rb, rb2)

stack.r <- stack(ra3, rb3)
names(stack.r) <- c("ra3", "rb3")

plot(stack.r[[1]])
plot(sps, add=T)

# raster::extract
rastR <- raster::extract(stack.r, sps, fun=mean, na.rm=T,  weights=TRUE)

# > head(rastR)
# [,1] [,2]
# [1,]   10   35
# [2,]   10   35
# [3,]   10   35
# [4,]   10   35
# [5,]   10   35
# [6,]   10   35

rastR2 <- rastR %>%
  cbind(sps@data["GID_2"]) # add ID

# terra::extract
sps.spatv <- vect(sps)
stack.r.spatR <-  rast(stack.r) 
rastT <- terra::extract(stack.r.spatR, sps.spatv, fun=mean, na.rm=T,  exact=TRUE)
# > head(rastT)
# ID ra3 rb3
# [1,]  1  10  10
# [2,]  2  10  10
# [3,]  3  10  10
# [4,]  4  10  10
# [5,]  5  10  10
# [6,]  6  10  10
rastT2 <- rastT %>%
  cbind(sps@data["GID_2"]) # add ID

Updated answer更新的答案

Thank you for the expanded question, and for insisting, and apologies for taking so long to get back to you.感谢您提出的扩展问题,以及您的坚持,并且很抱歉花了这么长时间才回复您。 This is a bug in terra that I did not immediately spot.这是terra中的一个错误,我没有立即发现。 The weighted mean results are garbled (the matrix is not filled in the right order).加权平均结果出现乱码(矩阵未按正确顺序填充)。 Fixed now:现在修复:

Your simplified example data您的简化示例数据

library(raster)
library(terra)
#terra version 1.2.17

sp <- getData("GADM", country = 'ETH', level = 2)[1:3,]
sv <- vect(sp)

ra <- raster(ncols=31, nrows=24, xmn= 33.3, ymn=  3.67, xmx = 47.5, ymx = 14.65, crs=crs(sp), vals=rep(10, 24*31))
rb <- raster(ncols=31, nrows=24, xmn= 33.3, ymn=  3.67, xmx = 47.5, ymx = 14.65, crs=crs(sv), vals=rep(35, 24*31))

r_raster <- stack(ra, rb)
names(r_raster) <- c("ra", "rb")
r_terra <-  rast(r_raster) 

Testing without weights and small=FALSE for raster and touches=FALSE for terra (default)没有权重的测试和small=FALSE用于rastertouches=FALSE用于terra (默认)

extract(r_raster, sp, fun=mean, na.rm=T, small=FALSE)
#     [,1] [,2]
#[1,]   NA   NA
#[2,]   10   35
#[3,]   10   35

extract(r_terra, sv, fun=mean, na.rm=T)
#  ID  ra  rb
#1  1 NaN NaN
#2  2  10  35
#3  3  10  35

Testing without weights and small=TRUE for raster (default) and touches=TRUE for terra不使用权重进行测试,对于raster (默认),使用small=TRUE ,对于terra ,使用touches=TRUE

extract(r_raster, sp, fun=mean, na.rm=T)
#      ra rb
# [1,] 10 35
#[2,] 10 35
#[3,] 10 35

extract(r_terra, sv, fun=mean, na.rm=T, touches=TRUE)
#  ID ra rb
#1  1 10 35
#2  2 10 35
#3  3 10 35
 

Testing with weights用权重测试

extract(r_raster, sp, fun=mean, na.rm=T,  weights=TRUE)
#     ra rb
#[1,] 10 35
#[2,] 10 35
#[3,] 10 35

extract(r_terra, sv, fun=mean, na.rm=T,  weights=TRUE)
#     ID ra rb
#[1,]  1 10 35
#[2,]  2 10 35
#[3,]  3 10 35

This was fixed in version 1.2.17.这已在版本 1.2.17 中修复。 You should be able to install that version within an hour like this您应该能够像这样在一小时内安装该版本

install.packages('terra', repos='https://rspatial.r-universe.dev')

I will be further testing it over the coming days;我将在未来几天进一步测试它; and hopefully get it to CRAN next week.希望下周能把它送到克兰。 It once worked and than I made it faster, but clearly without sufficient test cases.它曾经工作过,而且比我做得更快,但显然没有足够的测试用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM