2

So I have a shapely Polygon and two LineStrings. Both Linestrings always intersects the polygon. I want to get the polygon in the middle as seen in the example below. The region shaded in red is the polygon I want to get:

enter image description here

Anyone know how this can be done in shapely or some other Python library?

  • 2
    Can you convert the two lines to a polygon? This way you can run a simple intersection – Thomas Dec 4 '17 at 23:06
6

You can recreate a polygon from the lines. In the example below I did it via a convex hull, but there are other possibilities such as creating the polygon ring from the coordinates. After that you can intersect this polygon with the original polygon.

from shapely.geometry import LineString, Polygon, MultiLineString

a = LineString([(0, 3), (5, 3)])
b = LineString([(0, 5), (5, 5)])
poly = Polygon([(1, -2), (1, 7), (4, 7), (4, -2)])

# Create a polygon from the lines
multi_line = MultiLineString([a, b])
line_poly = multi_line.convex_hull

# get the intersection
intersection = poly.intersection(line_poly)
print(intersection)

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.