I've put together a simple example below that demonstrates how I would go about this kind of problem.
As a general note, rgeos as it is currently implemented does not handle sp DataFrame geometries and will just ignore the data slot and treat the object as if it was just a geometry. With that said it is still possible to merge the geometries but a little additional work is necessary because the user needs to define how the data rows will be merged. The example below shows a couple of simple polygons with color strings as the data. The polygons are merged using gIntersection and I have defined the function merge_col fuction which figures out which polygons from the parent geometries are producing the intersection so that it can identify the relevant data rows which are then merged by averaging the the RGB values. Obviously this approach is inefficient and I will definitely be trying to look into ways to handle all of this in the backend in the future but it should work for the time being. -Colin options(stringsAsFactors = FALSE) x = readWKT("POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))") y = readWKT("GEOMETRYCOLLECTION ( POLYGON((5 5, 14 5, 5 14, 5 5)), POLYGON((15 6, 15 15, 6 15, 15 6)))") x.df = SpatialPolygonsDataFrame(x,data=data.frame(col="green")) y.df = SpatialPolygonsDataFrame(y,data=data.frame(col=c("blue","purple") )) m = gIntersection(x.df,y.df) class(m) merge_col = function(m, x, y) { col = rep(NA, length(m)) for(i in 1:length(m)) { xi = gIntersects(m[i,],x,byid=T) yi = gIntersects(m[i,],y,byid=T) rgb = col2rgb(c(x$col[xi], y$col[yi])) mix = round( apply(rgb,1,mean) ) col[i] = rgb(mix[1],mix[2],mix[3],maxColorValue=255) } return(col) } m.df = SpatialPolygonsDataFrame(m,data=data.frame(col=merge_col(m,x.df,y.df))) plot(x.df,xlim=c(0,15),ylim=c(0,15),col=x.df$col) plot(y.df,add=T,col=y.df$col) plot(m.df,add=T,col=m.df$col) -- View this message in context: http://r-sig-geo.2731867.n2.nabble.com/merging-polygons-from-two-shapefiles-and-generating-new-polygons-for-intersecting-areas-tp5736049p5749808.html Sent from the R-sig-geo mailing list archive at Nabble.com. _______________________________________________ R-sig-Geo mailing list R-sig-Geo@stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo