Shapely can "handle" invalid polygons, you can inspect its validity via the is_valid
property. In some cases, in order to get the outer boundary without intersection, one can employ a "trick" using zero-sized buffer as shown below:
import math
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
from shapely.geometry import Polygon
fig = plt.figure(figsize=(3, 6))
pnts = [ (math.cos(math.pi/2 - 2*math.pi*i/5), math.sin(math.pi/2 - 2*math.pi*i/5)) for i in range(5) ]
pnts = [ pnts[0], pnts[2], pnts[4], pnts[1], pnts[3] ]
P = Polygon(pnts)
print(P.is_valid) #False
Q = P.buffer(0)
ax = plt.subplot(211, aspect = 'equal')
ax.plot(*P.exterior.xy)
ax = plt.subplot(212, aspect = 'equal')
ax.plot(*Q.exterior.xy)
fig.savefig('fig.png')
This gives:
