【问题标题】:Percentage of overlap between polygons多边形之间的重叠百分比
【发布时间】:2017-09-27 10:01:51
【问题描述】:

我是 R 新手,这是我第一次在这里发帖,想让你们知道我还是 R 语言的新手,希望它能简化你的答案。我目前正在做我的硕士论文,其中包括大量使用包含南非不同物种分布的 shapefile 的工作。我的研究包括4个动物科; 1 个猎物科(由 29 个物种组成)和 3 个掠食性科(每个包含更多物种)。对于每个物种,我都有一个带有多边形分布的 shapefile,所以我有很多 shapefile。我现在的问题是,我必须计算每个捕食者与每个猎物物种的重叠百分比(100% 重叠是当捕食者的分布/多边形在猎物的分布/多边形内时,所以百分比应该相对于猎物的多边形)。

我知道我可能可以分别为每个物种一个一个地做这件事,但这确实需要我几周时间,而且我没有时间。甚至我在 uni 的推广者以前也没有做过这样的事情,并且正试图自己解决这个问题。他最初给了我这个代码:

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

但是结果不是我们预期的,因为它没有计算相对于任何多边形的百分比。有没有人知道一种使用相对简单的代码来获得重叠百分比的方法,而不需要太多的模糊?也许导致包含所有物种/形状文件/多边形的矩阵形式?

提前谢谢大家!

【问题讨论】:

    标签: r


    【解决方案1】:

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

    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)
    

    ## 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
    

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-12
      • 1970-01-01
      • 2012-06-21
      • 2023-03-03
      • 1970-01-01
      相关资源
      最近更新 更多