繁体   English   中英

R中的平均聚集(多边形中的多边形)

[英]Mean aggregation in R (Polygon in Polygon)

我有一组代表分析单位的多边形(gadmpolys)。 另外,我还有一组具有各种变量级别的多边形(r3mergepolys)。

我要完成的工作是汇总与分析多边形单元(gadmpolys)相交的多边形(来自r3mergepolys)中一个或多个变量的均值。

我相信over和/或合计函数是我的朋友,但是我似乎无法弄清楚如何编写代码。

# gadmpolys is the spdf containing my units of analysis
# r3mergepoly is the spdf with many smaller polygons which I want to aggregate from
r3mergepoly <- SpatialPolygonsDataFrame(Sr=r3polys, data=r3merge, match.ID=TRUE)

# Overlay GADMpolys and Afrobarometer-GADM matched polygons. Aggregate survey results for intersecting polygons
gadmpoly_r3 <- over(gadmpoly, r3mergepoly[17:21], fn=mean)

快速且丑陋的基于质心的解决方法。

B <- SpatialPointsDataFrame(gCentroid(poly.pr, byid=TRUE),poly.pr@data, match.ID=FALSE)
plot(A)
points(poly_centroids)
# Overlay points and extract just the code column: 
a.data <- over(A, B[,"code"])
# Add that data back to A:
A$bcode <- a.data$code

聚合的sf包实现还提供了使用聚合的工作示例

m1 = cbind(c(0, 0, 1, 0), c(0, 1, 1, 0))
m2 = cbind(c(0, 1, 1, 0), c(0, 0, 1, 0))
pol = st_sfc(st_polygon(list(m1)), st_polygon(list(m2)))
set.seed(1985)
d = data.frame(matrix(runif(15), ncol = 3))
p = st_as_sf(x = d, coords = 1:2)
plot(pol)
plot(p, add = TRUE)
(p_ag1 = aggregate(p, pol, mean))
plot(p_ag1) # geometry same as pol
# works when x overlaps multiple objects in 'by':
p_buff = st_buffer(p, 0.2)
plot(p_buff, add = TRUE)
(p_ag2 = aggregate(p_buff, pol, mean)) # increased mean of second
# with non-matching features
m3 = cbind(c(0, 0, -0.1, 0), c(0, 0.1, 0.1, 0))
pol = st_sfc(st_polygon(list(m3)), st_polygon(list(m1)), st_polygon(list(m2)))
(p_ag3 = aggregate(p, pol, mean))
plot(p_ag3)
# In case we need to pass an argument to the join function:
(p_ag4 = aggregate(p, pol, mean, 
                   join = function(x, y) st_is_within_distance(x, y, dist = 0.3)))

暂无
暂无

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

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