3

I currently possess a shapefile that consists of >5000 polygons. I would like to know, for each polygon, how many other polygons it intersects with and the names of each polygon it intersects with?

My output should be something like this:

enter image description here

Based on this previous question and @JeffreyEvans suggestion on using gIntersects, I have tried the following below. But, the process has been running continuously without stopping for the last 5 hours. Any suggestions?

Here's the code I ran in R:

require(rgdal)
library(maptools)
library(rgeos)
library(GISTools)
Poly <- readOGR("C:\\Users\\rameshv\\Terrestrial_dissolved_multipart.shp")
Poly_join <- gIntersects(Poly)

EDIT

I was able to solve part of my own question. However, I am still unclear as to write a list of comma separated names on each row. As of now, I have a loop that read in every shapefile, checks for an Intersection with a large Spatial Polygons Data Frame and prints out a dataframe of and number of polygons it intersects with.

library(rgdal)
library(GISTools)
library(sp)
library(raster)

##List all the shapefiles
shapes <- list.files(path =    "C:\\Users\\rameshv\\Desktop\\Output_All\\", pattern=".shp$", full.names=TRUE)
head(shapes)

##Read in the big spatial polygons dataframe - this contains ALL MAMMALS
Big <-readOGR("C:\\Users\\rameshv\\Desktop\\Ranges_Overlap\\multipart.shp")

for (i in 1:length(shapes)){
    a <- readOGR(shapes[i])
    o <- over(a, Big,returnList = T)
    s <- o[[1]]
    count <- length(s$binomial)-1
    a$binomial <- as.character(a$binomial)
    results <- data.frame(a$binomial,count)
 write.table(results,file="C:\\Users\\rameshv\\Desktop\\overlapfile_Final.csv", row.names=F,append = TRUE, col.names = F, sep = ",")
}

Any thoughts?

1 Answer 1

0

Now with sf you could do

st_intersection(shapes, shapes)

For example

library(sf)
example(st_read)
x <- st_buffer(nc[1:10, ], dist = 0.1) ## make sure some overlap
st_intersects(x, x)
 Sparse geometry binary predicate list of length 10, where the predicate was `intersects'
 1: 1, 2
 2: 1, 2, 3
 3: 2, 3, 10
 4: 4, 7, 8
 5: 5, 6, 8, 9
 6: 5, 6, 8
 7: 4, 7, 8
 8: 4, 5, 6, 7, 8
 9: 5, 9
 10: 3, 10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.