在R

时间:2019-01-17 11:24:59

标签: r matrix polygon overlap

我正在尝试计算多对多边形之间的重叠面积和百分比。例如,我有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))

谢谢!

1 个答案:

答案 0 :(得分:0)

没有示例数据,我认为可能的解决方案是:

创建一些示例数据

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)

enter image description here

计算重叠区域

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