Cut a polygon with two lines in shapely
<p>I am trying to cut a <code>shapely.geometry.Polygon</code> instance into two parts with two lines. For example, in the code below, <code>polygon</code> is a ring and if we cut it with <code>line1</code> and <code>line2</code> we should get two partial rings, one w/ 270 degrees and one with 90 degrees. Would there be a clean way to do this?</p>

<p>Thank you!</p>

<p>Shawn</p>

<pre><code>from shapely.geometry import Point, LineString, Polygon

polygon = Point(0, 0).buffer(2).difference(Point(0, 0).buffer(1))
line1 = LineString([(0, 0), (3, 3)])
line2 = LineString([(0, 0), (3, -3)])
</code></pre>

<p>Ken Watford answered <a href="http://community-gispython-org-community-projects.955323.n3.nabble.com/Community-Shapely-how-to-split-a-polygon-with-a-line-td1915160.html" rel="nofollow">here</a> about using <code>buffer</code> and <code>difference</code> to do the trick, w/ the drawback of losing a bit of the area. An example code below:</p>

<pre><code>from shapely.geometry import Point, LineString, Polygon

polygon = Point(0, 0).buffer(2).difference(Point(0, 0).buffer(1))
line1 = LineString([(0, 0), (3, 3)])
line2 = LineString([(0, 0), (3, -3)])

line1_pol = line1.buffer(1e-3)
line2_pol = line2.buffer(1e-3)

new_polygon = polygon.difference(line1_pol).difference(line2_pol)
</code></pre>

<p>Works for now, and I'd be interested to see whether there is another (potentially w/o losing area) method!</p>

python shapely