Percentage of overlap between polygons

I'm new to R and this is my first post here, thought to let you guys know that I am still a newbie to the R language, hoping this will simplify your answers. I am currently doing my master's thesis which involves a lot of work with shapefiles containing distributions of different kinds in South Africa. My research includes 4 animal families; 1 family (consisting of 29 species) and 3 predator families (each of which contains more species). For each species, I have a shapefile with their distribution polygon, so I have a lot of shaping files. My problem is that I have to calculate the percentage of overlap of each predator with each prey species (100% overlap is when the predator distribution / polygon is within the prey distribution / polygon,therefore the percentage must be calculated relative to the mining polygon).

I know that maybe I could do this one for each species separately, but it would literally take me weeks and I don't have time for it. Even my promoter at uni hasn't done anything like this before and is trying to figure it out himself. He originally gave me this code:

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

      

But the result was not what we expected as it does not calculate percentages relative to any polygon. Does anyone know a way to get percentages of overlap using relatively simple code, not too much fluff? Maybe the result is a matrix containing all the views / shapefiles / polygons?

Thanks everyone in advance!

+3


source to share


1 answer


For fun, here's an example. Some people here or at gis.stackexchange.com may have better ways in 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)

      



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

      

+4


source







All Articles