Python Shapely – How to Add Points to a LineString in Shapely

pythonshapely

I have two Polygons which intersect along one edge as shown below.

enter image description here

I need to add the red vertices at each end of the intersection to the blue polygon. I thought I could do this with by calling union on the Polygon and the LineString produced by intersection of the two polygons but it appears not.

Is there a way of doing what I need to do?

Best Answer

You can use the properties of union (and unary_union) witch split all the lines at intersections (Planar graph)

enter image description here

# LinearRing of the polygons
ringgreen = LineString(list(polygreen.exterior.coords))
ringblue  = LineString(list(polyblue.exterior.coords))
# Union of the rings
union = ringgreen.union(ringblue)
# now polygonize the result
from shapely.ops import polygonize
result = [geom for geom in polygonize(union]

enter image description here

and

enter image description here