Create new shapely polygon by subtracting the intersection with another polygon
<p>I have two shapely MultiPolygon instances (made of lon,lat points) that intersect at various parts. I'm trying to loop through, determine if there's an intersection between two polygons, and then create a new polygon that excludes that intersection. From the attached image, I basically don't want the red circle to overlap with the yellow contour, I want the edge to be exactly where the yellow contour starts.</p>

<p>I've tried following the instructions <a href="http://gis.stackexchange.com/questions/11987/polygon-overlay-with-shapely">here</a> but it doesn't change my output at all, plus I don't want to merge them into one cascading union. I'm not getting any error messages, but when I add these MultiPolygons to a KML file (just raw text manipulation in python, no fancy program) they're still showing up as circles without any modifications. </p>

<pre><code># multipol1 and multipol2 are my shapely MultiPolygons
from shapely.ops import cascaded_union
from itertools import combinations
from shapely.geometry import Polygon,MultiPolygon

outmulti = []
for pol in multipoly1:
    for pol2 in multipoly2:
        if pol.intersects(pol2)==True:
            # If they intersect, create a new polygon that is
            # essentially pol minus the intersection
            intersection = pol.intersection(pol2)
            nonoverlap = pol.difference(intersection)
            outmulti.append(nonoverlap)

        else:
            # Otherwise, just keep the initial polygon as it is.
            outmulti.append(pol)

finalpol = MultiPolygon(outmulti)
</code></pre>

<p><a href="http://i.stack.imgur.com/Z1NyF.png" rel="nofollow"><img src="http://i.stack.imgur.com/Z1NyF.png" alt="Polygon Overlap"></a></p>

<p>I guess you can use the <a href="http://toblerity.org/shapely/manual.html#object.symmetric_difference" rel="nofollow"><code>symmetric_difference</code></a> between theses two polygons, combined by the difference with the second polygon to achieve what you want to do (the <em>symmetric difference</em> will brings you the non-overlapping parts from the two polygons, on which are removed parts of the polygon 2 by the <em>difference</em>). I haven't tested but it might look like :</p>

<pre><code># multipol1 and multipol2 are my shapely MultiPolygons
from shapely.ops import cascaded_union
from itertools import combinations
from shapely.geometry import Polygon,MultiPolygon

outmulti = []
for pol in multipoly1:
    for pol2 in multipoly2:
        if pol.intersects(pol2)==True:
            # If they intersect, create a new polygon that is
            # essentially pol minus the intersection
            nonoverlap = (pol.symmetric_difference(pol2)).difference(pol2)
            outmulti.append(nonoverlap)

        else:
            # Otherwise, just keep the initial polygon as it is.
            outmulti.append(pol)

finalpol = MultiPolygon(outmulti)
</code></pre>

python python-2.7 polygon shapely