【问题标题】:Calculate area overlap for pairs of polygons in matrix format in R计算R中矩阵格式的多边形对的区域重叠
【发布时间】:2019-01-17 11:24:58
【问题描述】:

我正在尝试计算多对多边形之间重叠的面积和百分比。例如,我有 5 个多边形,我想计算每对组合的面积和重叠百分比。有没有办法可以运行包含所有多边形(shapefile)的函数并获得显示每对值的矩阵输出?我想得到这样的输出:

overlap    poly 1     poly 2    poly 3     poly 4 poly 5

poly 1

poly 2

poly 3

poly 4

poly 5

我用来计算一对多边形的重叠百分比的公式如下:

AreaOverlap/(SQRT(AreaPolyA*AreaPolyB))

谢谢!

【问题讨论】:

  • 请提供一些示例数据..(提示:使用dput()
  • 多边形的格式是什么? x.y 坐标?还是直接给定面积?需要更多信息
  • @Mandar 不同的多边形是我加载到 R 中的不同 shapefile,因此在坐标系中。
  • @Wimpel 如果我使用的数据是加载到 R 中的 shapefile,我可以使用 dput() 提供示例数据吗?
  • @Stijn 您必须创建一个最小的可重现示例,因此加载整个 shapefile 可能太多了。如果有必要,您也可以链接到您的数据...如果我在下面的回答将有效您将 shapefile 作为单独的多边形加载到 sf 对象中。如果您以前没有使用过它,您可能必须遵循一些关于该包的教程。例如:gis.stackexchange.com/a/230161/129211

标签: r matrix polygon overlap


【解决方案1】:

如果没有样本数据,我认为可能的解决方案是:

创建一些示例数据

library( sf)
#square of 2 x 2
pol = st_polygon(list(rbind(c(0,0), c(2,0), c(2,2), c(0,2), c(0,0))))
#add two more squares of 2 x 2
b = st_sfc(pol, pol + c(.8, .2), pol + c(4, .8))

plot(b)

计算重叠面积

l <- lapply( b, function(x) { 
       lapply(b, function(y) st_intersection( x, y ) %>% st_area() ) 
     })

matrix(unlist(l), ncol = length(b), byrow = TRUE)
#      [,1] [,2] [,3]
# [1,] 4.00 2.16    0
# [2,] 2.16 4.00    0
# [3,] 0.00 0.00    4

计算重叠百分比

l2 <- lapply( b, function(x) { 
  lapply(b, function(y) st_intersection( x, y ) %>% st_area() * 100 /sqrt( st_area(x) * st_area(y) ) ) 
})

matrix(unlist(l2), ncol = length(b), byrow = TRUE)
#      [,1] [,2] [,3]
# [1,]  100   54    0
# [2,]   54  100    0
# [3,]    0    0  100

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 2012-04-16
    • 1970-01-01
    • 2017-07-07
    • 2018-04-21
    相关资源
    最近更新 更多