I'm working on splitting a polygon given a set of linestrings using the shapely python library. The problem I'm facing is that a polygon cannot be split by a MultiLineString usingshapely.ops.split(geom, splitter). enter image description here

The desired result in the image above is to split polygon (blue) with the linestrings (yellow, brown, red) and recieve 4 seperate polygons (1,2,3,4). The geometries in the image are provided below. It is preferred to do this within shapely.

    polygon = shapely.geometry.polygon.Polygon(
[(172288.6111791851, 442085.8603492165),
 (172300.89001713035, 442131.53561756504),
 (171916.83106749243, 442140.9016841368),
 (171910.779533863, 442014.47234046715),
 (172165.44614826166, 442016.17982713436),
 (172188.0201264064, 442022.13092037174),
 (172189.32585150487, 442047.592559792),
 (172159.87170649506, 442049.9277989104),
 (172169.13733267464, 442085.8603492165),
 (172288.6111791851, 442085.8603492165)])

coords = [((172159.87170649506, 442016.14245166205),
 (172159.87170649506, 442049.9277989104)),((172159.87170649506, 442049.9277989104),
 (172159.87170649506, 442134.97463880514)),((172169.13733267464, 442085.8603492165),
 (172169.13733267464, 442134.74867747363))]

lines = shapely.geometry.MultiLineString(coords)
  • Thanks for the question. This cellular decomposition approach should be in the Robotics network of Stack Exchange – UserK Apr 29 at 12:56

adopted the answer from this post: Splitting Polygon by Linestring in GeoDjango?.

line_split_collection.append(polygon.boundary) # collection of individual linestrings for splitting in a list and add the polygon lines to it.
merged_lines = shapely.ops.linemerge(line_split_collection)
border_lines = shapely.ops.unary_union(merged_lines)
decomposition = shapely.ops.polygonize(border_lines)

The lines for splitting are slightly extended in both directions (0.01) in order to ensure that they cross the input polygon border, therefore making the unary_union work.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.