【问题标题】:summarise attributes from sf::st_intersection() where geometries overlaps汇总来自 sf::st_intersection() 几何重叠的属性
【发布时间】:2018-06-25 01:28:07
【问题描述】:

我想总结一组几何图形的属性,对它们重叠的值求和。

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

> 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(对应于重叠的那个)。

任何帮助将不胜感激。

【问题讨论】:

    标签: r summarization sf


    【解决方案1】:

    您可以使用origins 列表列来检索“起源”相交的多边形,然后对它们的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
    

    【讨论】:

    • @Ibusett 它似乎运作良好。我只是想知道如何避免循环,但我会接受它!谢谢!。
    • 当然可以使用 *apply/purrr 来完成,尽管有时我想知道为什么要为此烦恼(除非存在性能问题),因为 for 循环语法更容易阅读和理解(至少对我来说......);-)
    猜你喜欢
    • 2021-06-09
    • 1970-01-01
    • 2022-01-22
    • 2020-12-29
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多