Skip to content
Prev 23225 / 29559 Next

Union/Overlay single spatialPolygonsDataFrame containing overlapping polygonsto create polygons with all unique combinations of attributes

Thanks for the suggestions.

unfortunately raster::union does not get the desired result,

First it requires two, and only two spatialPolygons objects as inputs. - I
think the function I need must either have a single input containing all
the polygons, or one input for each polygon to work in more complex cases
with many polygons with more than tow overlapping polygons

Second in my example it does indeed produce four polygons, however these
comprise the two original square polygons and two polygons from the
overlapping areas:

#what union::(SPDF2,SPDF2) acheives

par(mfrow=c(2,4))
myUnion<-raster::union(mySPDF2,mySPDF2)
myUnion$Area<-gArea(myUnion, byid=TRUE)
for (i in 1:4){
  plot(myUnion[i,],col=cols[i],axes=T,xlim=c(0,5),ylim=c(0,5))
}
print(myUnion at data)
print("Areas myUnion:")
print(gArea(myUnion, byid=TRUE))


#what is required:

p4<-SpatialPolygons(list(Polygons(list(Polygon(cbind(c(1,4,4,3,3,1,1),c(1,1,3,3,4,4,1)),hole
= F)), "1_ "),

 Polygons(list(Polygon(cbind(c(3,4,4,3,3),c(3,3,4,4,3)),hole = F)), "1_2"),

 Polygons(list(Polygon(cbind(c(3,4,4,3,3),c(3,3,4,4,3)),hole = F)), "2_1"),

 Polygons(list(Polygon(cbind(c(4,4,5,5,3,3,4),c(4,3,3,5,5,4,4)),hole = F)),
"2_")))
for (i in 1:4){
  plot(p4[i,],col=cols[i],axes=T,xlim=c(0,5),ylim=c(0,5))
}
print("Areas p4:")
print(gArea(p4, byid=TRUE))


On Wed, Aug 12, 2015 at 11:02 AM, Robert J. Hijmans <r.hijmans at gmail.com>
wrote: