I am trying to cut a shapely.geometry.Polygon
instance in two parts with two lines. For example, in the code below, polygon
is a ring and if we cut it with line1
and line2
we should get two partial rings, one w/ 270 degrees and one with 90 degrees. Would there be a clean way to do this?
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)])
buffer(0.2) , shapely will buffer the coordinates in polygon by 0.2 units. In your case, these are not meters, but degrees.
geometry. Polygon to simply convert to line string to a polygon. It will connect the first and last coordinates. Try Polygon([(0, 0), (1, 1), (1, 2), (0, 1)]) or Polygon(s1) to produce POLYGON ((0 0, 1 1, 1 2, 0 1, 0 0)).
There is a function to split one geometry by another in Shapely as of version 1.6.0 (Aug 2017), so there is no need to roll your own anymore. See the docs for: shapely.ops.split(geom, splitter)
Note that the older answers on this thread were written before the splitting function was in Shapely - they are now effectively obsolete.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With