Invalid multipolygon of valid individual polygons

In addition to @15Step's answer, If you want to fix the invalidity, use buffer method. But if polygons are adjacent, you get polygon instead of multipolygon.

mpolygon = mpolygon.buffer(0)
print(mpolygon.is_valid)
print(type(mpolygon))
# OUT:
#   True
#   <class 'shapely.geometry.polygon.Polygon'>

enter image description here

If you need multipolygon, you have to convert polygon to multipolygon.

if isinstance(mpolygon, Polygon):
    mpolygon = MultiPolygon([mpolygon])

It's because polygon1 and polygon3 that you created intersect along an infinite amount of points. Taken here from the Shapely documentation:

enter image description here
...On the left, a valid MultiPolygon with 2 members, and on the right, a MultiPolygon that is invalid because its members touch at an infinite number of points (along a line). Link

To fix this, you need to shift polygon3 so that it will have a maximum of one intersecting point with the other polygons (or none at all).

Your polygons plotted:

enter image description here

Tags:

Python

Shapely