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:
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?