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

R中的平均聚合(多边形中的多边形),r,gis,aggregate,geospatial,spatial,R,Gis,Aggregate,Geospatial,Spatial,我有一组表示分析单位(gadmpolys)的多边形。 此外,我还有一组具有各种变量级别的多边形(R3MergePoly) 我想要完成的是聚合与分析单元多边形(gadmpolys)相交的多边形(来自r3mergepolys)中一个或多个变量的平均值 我相信over和/或aggregate函数是我的朋友,但我似乎不知道如何编写代码 # gadmpolys is the spdf containing my units of analysis # r3mergepoly is the spdf wit

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

我想要完成的是聚合与分析单元多边形(gadmpolys)相交的多边形(来自r3mergepolys)中一个或多个变量的平均值

我相信over和/或aggregate函数是我的朋友,但我似乎不知道如何编写代码

# 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)
#gadmpolys是包含我的分析单位的spdf
#r3mergepoly是包含许多较小多边形的spdf,我想从这些多边形中进行聚合

r3mergepoly快速且丑陋的基于质心的解决方案

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

在PostGIS中,我会这样写:选择b.gid,AVG(a.var1)作为meanvar1,AVG(a.var2)作为meanvar2,从gadmpolys b,r3a中,ST_通过b.gid相交(a.geom,b.geom)组;我通过创建一个for循环,将多边形转换为光栅,然后在每个gadmpolys多边形中聚合平均光栅像素值,从而创建了一种方法。你找到什么了吗?我需要类似的东西!
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)))