多边形之间重叠的百分比

4
我是一名新手,第一次使用R并在此发帖。我想让大家知道我对R语言还很陌生,希望这能简化你们的回答。我正在撰写我的硕士论文,其中涉及到南非不同物种的分布情况。我的研究包括4个动物家族:1个猎物家族(包含29个物种)和3个掠食家族(每个家族都包含更多物种)。对于每个物种,我都有一个形状文件,其中包含它们的分布多边形,因此我有很多形状文件。现在我的问题是,我必须计算每个掠食者与每个猎物物种之间的重叠百分比(当掠食者的分布/多边形在猎物的分布/多边形内时,100%的重叠是指相对于猎物物种的多边形计算百分比)。
我知道我可能可以为每个物种单独进行计算,但这需要花费我数周的时间,而我没有那么多时间。即使我的导师也没有做过这样的事情,并且正在尝试自己解决。他最初给了我这段代码:
my_rangemaps <- list.files(path = "imagine_rangemaps", pattern = ".shp", full.names = TRUE) 
my_rangemaps 
rangemap_matrix <- pairwiseRangemaps(my_rangemaps, projection = 3035, 
             Ncpu = 1, nchunks = 1, filename = "rangemap_matrix.csv") 

但结果并不如我们所预期的那样,因为它没有相对于任何多边形计算百分比。有没有人知道一种相对简单的方法来获取重叠百分比,而不需要太多复杂的代码呢?也许可以得到一个包含所有物种/形状文件/多边形的矩阵形式的结果?谢谢大家提前!
1个回答

6

为了好玩,这里有一个例子。这里或者在gis.stackexchange.com上的一些人可能有更好的方法:

library(raster)
library(sp)
## example data:
p1 <- structure(c(0, 0, 0.4, 0.4, 0, 0.6, 0.6, 0), .Dim = c(4L, 2L), .Dimnames = list(NULL, c("x", "y")))
p2 <- structure(c(0.2, 0.2, 0.6, 0.6, 0, 0.4, 0.4, 0), .Dim = c(4L, 2L), .Dimnames = list(NULL, c("x", "y")))
p3 <- structure(c(0, 0, 0.8, 0.8, 0, 0.8, 0.8, 0), .Dim = c(4L, 2L), .Dimnames = list(NULL, c("x", "y")))
poly <- SpatialPolygons(list(Polygons(list(Polygon(p1)), "a"),Polygons(list(Polygon(p2)), "b"),Polygons(list(Polygon(p3)), "c")),1L:3L)
plot(poly)

enter image description here

## areas for the original shapes:
areas_poly <- vector(length = length(poly)) 
for (x in seq_along(poly)) areas_poly[x]<-area(poly[x])

## areas for the overlapping regions:
idx <- combn(seq_along(poly),2)
areas_intersect <- sapply(1:ncol(idx), function(x) {
  area(intersect(poly[idx[1,x]], poly[idx[2,x]])) 
})

## get overlaps in percentages:
overlap_perc <- 
  round(do.call(cbind, lapply(seq_len(ncol(idx)), function(x)  
  rbind(
    areas_intersect[x] / areas_poly[idx[1,x]] * 100, 
    areas_intersect[x] / areas_poly[idx[2,x]] * 100
  )
)), 2)

## into matrix form:
m <- matrix(100, ncol=length(poly), nrow=length(poly))
m[rbind(t(idx),t(idx)[,2:1])] <- as.vector(t(overlap_perc))
m
#       [,1]   [,2] [,3]
# [1,] 100.0  33.33  100
# [2,]  50.0 100.00  100
# [3,]  37.5  25.00  100

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接