从具有小多边形的栅格中提取()数据 - 圆角权重太小

时间:2022-09-17 20:49:59

Using R, I am trying to extract data from a raster layer using a polygon layer. The polygons are much smaller than the raster cells:

使用R,我试图使用多边形图层从栅格图层中提取数据。多边形比栅格单元小得多:

从具有小多边形的栅格中提取()数据 - 圆角权重太小

Now I call extract() from raster library:

现在我从栅格库中调用extract():

a <- extract(raster, polygons, weights = TRUE, small = TRUE)
a
# ...
# [[1551]]
# value weight
#   209   0.03 # top left cell - more than 50% of the polygon area

There are two problems - the weight is the proportion of the cell area covered by the polygon, and the weights are rounded to 1/100. In my case, only the top left cell is present in the output (value 209) - the weight of 3 other cells was rounded to zero and they were excluded. However, the bottom left cell covers significant proportion of the polygon and should be included also!

有两个问题 - 权重是多边形覆盖的单元格区域的比例,权重四舍五入为1/100。在我的例子中,只有左上角的单元格出现在输出中(值209) - 其他3个单元格的权重被舍入为零,它们被排除在外。但是,左下角的单元格覆盖了多边形的很大比例,也应该包括在内!

I need a proper weighted mean. Can this be done somehow else using extract()? Or any other way?

PS: note aside: I think the weights in extract() are not designed very well - the weight should be the proportion of polygon area covered by the particular cell, not vice versa. Then, the weighted mean for the polygon would be also easier to compute (just multiply the two numbers in each row and sum up), and rounding to 1/100 wouldn't be a big problem.

PS:请注意:我认为extract()中的权重设计得不是很好 - 权重应该是特定单元格覆盖的多边形区域的比例,反之亦然。然后,多边形的加权平均值也将更容易计算(只需将每行中的两个数相乘并求和),并且舍入到1/100不会是一个大问题。

Reproducible example - (download the files - simplified version, actual data are much bigger):

可重复的示例 - (下载文件 - 简化版,实际数据要大得多):

require(raster)
rast <- raster("my.tif")
poly <- readOGR(".", "socc_buff_Rx")
a <- extract(rast, poly, weights = TRUE, small = TRUE)
a

Related: Extract in R fails for small polygons and raster

相关:对于小多边形和栅格,R中的提取失败

1 个解决方案

#1


4  

I think the easiest, albeit inelegant, solution is to disaggregate the RasterLayer first. I will have a look to see if I can change the extract function to do this automatically for very small (relative to the cells size) polygons.

我认为最简单但最不优雅的解决方案是首先分解RasterLayer。我将看看是否可以更改提取功能,以自动执行非常小(相对于单元格大小)多边形。

library(raster)
r <- raster("my.tif")
pu <- shapefile("socc_buff_Rx.shp")
p <- spTransform(pu, crs(r))

extract(r, p, weights = TRUE, small = TRUE)
#[[1]]
# value weight
#   209   0.03

rr <- disaggregate(r, 10)
e <- extract(rr, p, weights = TRUE, small = TRUE)
lapply(e, function(x) { aggregate(x[,2,drop=F], list(value=x[,1]), sum ) } )

#[[1]]
#  value weight
#1   197   0.95
#2   209   3.44
#3   256   0.31
#4   293   0.04

plot(r, legend=F)
plot(p, add=T)
text(r)

从具有小多边形的栅格中提取()数据 - 圆角权重太小

#1


4  

I think the easiest, albeit inelegant, solution is to disaggregate the RasterLayer first. I will have a look to see if I can change the extract function to do this automatically for very small (relative to the cells size) polygons.

我认为最简单但最不优雅的解决方案是首先分解RasterLayer。我将看看是否可以更改提取功能,以自动执行非常小(相对于单元格大小)多边形。

library(raster)
r <- raster("my.tif")
pu <- shapefile("socc_buff_Rx.shp")
p <- spTransform(pu, crs(r))

extract(r, p, weights = TRUE, small = TRUE)
#[[1]]
# value weight
#   209   0.03

rr <- disaggregate(r, 10)
e <- extract(rr, p, weights = TRUE, small = TRUE)
lapply(e, function(x) { aggregate(x[,2,drop=F], list(value=x[,1]), sum ) } )

#[[1]]
#  value weight
#1   197   0.95
#2   209   3.44
#3   256   0.31
#4   293   0.04

plot(r, legend=F)
plot(p, add=T)
text(r)

从具有小多边形的栅格中提取()数据 - 圆角权重太小