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

时间:2014-02-18 09:14:55

标签: r gis aggregate geospatial spatial

我有一组代表分析单元的多边形(gadmpolys)。 另外,我有一组具有各种变量水平的多边形(r3mergepolys)。

我想要实现的是聚合多边形(来自r3mergepolys)的一个或多个变量的平均值,这些变量与分析多边形(gadmpolys)的单位相交。

我相信过度和/或聚合功能是我的朋友,但我似乎无法弄清楚如何编写代码。

# 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)

1 个答案:

答案 0 :(得分:0)

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

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)))