I have a spatialpolygons-dataframe in R that contains overlapping polygon features. I would like to dissolve only the overlapping features into separate polygon features (one feature per par of overlapping polygons) and preserve the other features as they are. The length of my dataset should therefore reduce e.g. from 4000 to 3900 if say 200 polygons overlap pairwise.
I tried the unionSpatialPolygons()
from the maptools
package as indicated here. However this will dissolve all polygons into a single polygon.
A workaround would be to use an id-vector that defines which polygons should be dissolved as indicated by the help manual.
if the id argument is used, it should be a character vector defining the memberships of the output Polygons objects, equal in length to the length of the polygons slot of spgeom
In my case it would be those polygons that overlap together (can be two or more).
Edit
A possible but not efficient solution is given in the code below, assuming you already have an intersection matrix, that you can create e.g. with gIntersects
setting byid=TRUE
. IF anyone knows an easier and more efficient solution for this, please contribute.
### example data (this should later be your intersection matrix)
mx<-matrix(c(TRUE,TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,
TRUE,TRUE,TRUE,FALSE,FALSE,FALSE,FALSE,
FALSE,TRUE,TRUE,FALSE,FALSE,FALSE,FALSE,
FALSE,FALSE,FALSE,TRUE,TRUE,FALSE,FALSE,
FALSE,FALSE,FALSE,TRUE,TRUE,FALSE,FALSE,
FALSE,FALSE,FALSE,FALSE,FALSE,TRUE,FALSE,
TRUE,FALSE,FALSE,FALSE,FALSE,FALSE,TRUE),7)
### groupings
# create a list for the results
results.list<-as.list(vector(length=ncol(mx)))
# group
for(i in 1:ncol(mx)) {
tmp <- which(mx[,i]) # get TRUE FALSE values for the i-th column
ifelse(length(tmp)>1, # if there is more than 1 TRUE value,
tmp.expand<-which(apply(mx[,tmp],1,any)), # get the row-number of the other TRUE Values and create a vector called expand
tmp.expand<-tmp) # otherwise define tmp as expand
while(length(tmp.expand)>length(tmp)){ # while tmp.expand has more items than tmp
tmp<-tmp.expand # reset tmp to tmp.expand
tmp.expand<-as.vector(which(apply(mx[,tmp],1,any))) # get all new row-numbers of new TRUE values
}
results.list[[i]]<-tmp.expand # store results in the list
print(paste("nr", i, "out of", ncol(mx),"done", sep=" "))
}
# create unique ids from the results
results.list<-
llply(.data = results.list,
.fun = function(x)paste(x,collapse=""))
Now you can use this list as an ID vector in the unionSpatialPolygons
function. This will create new polygons from the overlapping ones and leave the others as they are.
Note
The amount of computational power required by this approach increases exponentially with the size of your matrix/nr of polygons and the nr. of overlappings. If you have a very big data-set you might rather subset it first and than process the subsets separately. After you can join them again and apply the function again to get the same result.
I also tried the code with the lapply
function instead of a for
loop but it is not really faster, at least if applied on a single core.
The code was partly developed with help of this question on SO.