计算 R 中矩阵格式的多边形对的面积重叠

Sti*_*ijn 2 r polygon matrix overlap

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

overlap    poly 1     poly 2    poly 3     poly 4 poly 5

poly 1

poly 2

poly 3

poly 4

poly 5
Run Code Online (Sandbox Code Playgroud)

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

AreaOverlap/(SQRT(AreaPolyA*AreaPolyB))
Run Code Online (Sandbox Code Playgroud)

谢谢!

Wim*_*pel 5

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

创建一些示例数据

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)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

计算重叠面积

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
Run Code Online (Sandbox Code Playgroud)

计算重叠百分比

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
Run Code Online (Sandbox Code Playgroud)