Shapely 可以很好地解决您的问题。您的边缘案例是两个通用多边形相交的标准案例,您可以使用Polygon.intersection (link)。如果交叉点产生多个多边形,那么Polygon.intersection 只会给你一个MultiPolygon。下面举个例子来说明:
from shapely.geometry import Polygon
from shapely.affinity import scale,translate
import matplotlib.pyplot as plt
import numpy as np
# build squiggly polygon
theta=np.linspace(0,2*np.pi,100)
x=np.cos(theta)+0.1*np.sin(theta*10)
y=np.sin(theta)
A=Polygon(np.column_stack((x,y)))
# build a box
B=Polygon([[-.25,-1.5],[.25,-1.5],[.25,1.25],[-.25,1.25],[-.25,-1.5]])
# Intersection
C=A.intersection(B)
plt.subplot(2,2,1)
plt.plot(A.boundary.xy[0],A.boundary.xy[1],'k',label='A')
plt.plot(B.boundary.xy[0],B.boundary.xy[1],'b',label='B')
plt.plot(C.boundary.xy[0],C.boundary.xy[1],'--r',label='C')
plt.title('standard intersection')
# box in squiggly
B1=scale(B,0.5,0.5)
C=A.intersection(B1)
plt.subplot(2,2,2)
plt.plot(A.boundary.xy[0],A.boundary.xy[1],'k',label='A')
plt.plot(B1.boundary.xy[0],B1.boundary.xy[1],'b',label='B')
plt.plot(C.boundary.xy[0],C.boundary.xy[1],'--r',label='C')
plt.title('box in squiggly')
# squiggly in box
B1=scale(B,5,5)
C=A.intersection(B1)
plt.subplot(2,2,3)
plt.plot(A.boundary.xy[0],A.boundary.xy[1],'k',label='A')
plt.plot(B1.boundary.xy[0],B1.boundary.xy[1],'b',label='B')
plt.plot(C.boundary.xy[0],C.boundary.xy[1],'--r',label='C')
plt.title('squiggly in box')
# multipolygons
B1=translate(B,1.2,0)
C=A.intersection(B1)
plt.subplot(2,2,4)
plt.plot(A.boundary.xy[0],A.boundary.xy[1],'k',label='A')
plt.plot(B1.boundary.xy[0],B1.boundary.xy[1],'b',label='B')
for poly in C:
plt.plot(poly.boundary.xy[0],poly.boundary.xy[1],'--r',label='C')
plt.title('multipolygons')
plt.show()
生成下图