问题描述
我正在使用R,尝试使用多边形图层从栅格图层中提取数据.多边形比栅格像元小 :
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:
现在我从raster
库中调用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
有两个问题-权重是多边形覆盖的 cell 区域的比例,权重四舍五入为1/100.在我的情况下,输出中仅存在左上角的单元格(值209)-其他3个单元格的权重舍入为零,并将它们排除在外.但是,左下角的单元格覆盖了很大一部分的多边形,也应将其包括在内!
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!
PS:请注意:我认为extract()
中的权重设计得不是很好-权重应该是特定单元格覆盖的 polygon 区域的比例,反之亦然.然后,多边形的加权平均值也将更容易计算(只需将每行中的两个数字相乘并求和),并且四舍五入为1/100不会是一个大问题.
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.
可复制的示例-(下载文件-简化版,实际数据要大得多):
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
推荐答案
我认为最简单的解决方案是首先分解RasterLayer,尽管效果不佳.我将看看是否可以更改提取功能以针对非常小的(相对于像元大小)多边形自动执行此操作.
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.
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)
这篇关于来自具有小多边形的栅格的extract()数据-舍入后的权重太小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!