[R-sig-Geo] Using the gDifference function

Cam Plouffe cam.plouffe at gmail.com
Tue Jul 26 17:54:46 CEST 2011

Hi everybody,

I would like to use the gDifference function from the rgeos package to find
the difference between 2 groups of polygons (poly1 and poly2).  I've tried
the gDifference function with my given polygons (I made 2 shapefiles of 3
simple polygons that overlap so I could see how the function was working)
and attached the output plot to this email.  I attached the gDifference
between the gUnion and the gIntersection of the 2 shapefiles, because it
shows you all the polygons from both shapefiles(in red), and where they
intersect (in white).  The gDifference function seemed to work for 2 of the
sets of overlapping polygons, but the 3rd (or rather the 1st, in the top
left) set of polygons was not affected (as far as I know).  I believe this
is because in the 2 overlapping polygons in the top left, the polygon from
poly2 is COMPLETELY contained within the polygon from poly1.  Ideally, I
would like the output to have a white hole where the overlapping polygon
from poly2 is currently (like the other polygons).

Here is the code I am using:

poly1 = polygons(spdf1)
poly2 = polygons(spdf2)

p_u = gUnion(poly1, poly2)
p_int = gIntersection(poly1, poly2)

p_diff = gDifference(p_u, p_int)
plot(p_diff, col = "red")

I tried using gSymdifference, and also tried using the original polygons as
opposed to the gUnion and gIntersection for the arguments
(gSymdifference(poly1, poly2) gives the same output plot as above), but the
same problem is always present.  I'm sure this problem can be rectified, but
I imagine the same situation will come up when analysing larger data sets,
so I'm going to look into it now.  There is a good chance I am not
interpreting the data properly, as I'm very new to R, but I would appreciate
any insight that could be provided.  I can provide more information
regarding my problem if it is needed/requested.

Thanks a lot,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20110726/d00cb2b1/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: gDifference.png
Type: image/png
Size: 4234 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20110726/d00cb2b1/attachment.png>

More information about the R-sig-Geo mailing list