汇总来自sf :: st_intersection()的属性,其中几何重叠

时间:2018-01-16 10:53:59

标签: r summarization sf

我想总结一组几何的属性,将它们重叠的值相加。

library(devtools)
install_github("r-spatial/sf")
library(sf)
m = rbind(c(0,0), c(1,0), c(1,1), c(0,1), c(0,0))
p = st_polygon(list(m))
n = rbind(c(0.5,0.5), c(1.5,0.5), c(1.5,1.5), c(0.5,1.5), c(0.5,0.5))
q = st_polygon(list(n))
s = st_sfc(list(p, q))
sf = st_sf(s, att=c(1,1))
d = st_intersection(sf)
d$id <- 1:nrow(d)

plot(d['att'])
plot(st_centroid(d['att']), add = TRUE, col = 'red')

enter image description here

> d
#Simple feature collection with 3 features and 4 fields
#geometry type:  POLYGON
#dimension:      XY
#bbox:           xmin: 0 ymin: 0 xmax: 1.5 ymax: 1.5
#epsg (SRID):    NA
#proj4string:    NA
#  att                       geometry n.overlaps origins id
#1   1 POLYGON ((1 0.5, 1 0, 0 0, ...          1       1  1
#2   1 POLYGON ((0.5 1, 1 1, 1 0.5...          2    1, 2  2
#3   1 POLYGON ((0.5 1, 0.5 1.5, 1...          1       2  3

在上面提供的最小例子中,我想总结d $ att并得到几何#2的att = 2(对应于重叠的那个)。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

您可以使用origins列表列来检索&#34;起源的多边形&#34;相交,然后求和att列。这样的事情可以起作用(至少在这个非常简单的用例中......):

for (int in seq_along(d$id)) {
     d$att[int] = sum(sf[d$origins[[int]], ]$att, na.rm = TRUE)
}
> d
Simple feature collection with 3 features and 4 fields
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: 0 ymin: 0 xmax: 1.5 ymax: 1.5
epsg (SRID):    NA
proj4string:    NA
  att                       geometry n.overlaps origins id
1   1 POLYGON ((1 0.5, 1 0, 0 0, ...          1       1  1
2   2 POLYGON ((0.5 1, 1 1, 1 0.5...          2    1, 2  2
3   1 POLYGON ((0.5 1, 0.5 1.5, 1...          1       2  3