Ana*_*nas 3 python geometry runtime shapely
尝试计算两个几何对象之间的交集时收到此警告。
>>> shapely.intersection(LineString([(0, 0), (1, 1)], LineString([(2.5, 2.5), (3, 3)]))
.../lib/python3.9/site-packages/shapely/set_operations.py:133: RuntimeWarning: invalid value encountered in intersection
return lib.intersection(a, b, **kwargs)
Run Code Online (Sandbox Code Playgroud)
我认为当两个几何图形之间根本不存在交集时会出现警告,因此我所做的是首先使用 intersects() 检查是否存在交集,然后才计算它们之间的交集。
然后您可以根据您的应用来处理不发生交叉的情况。
def get_iou( polygon1, polygon2):
if polygon1.intersects(polygon2):
intersect = polygon1.intersection(polygon2).area
union = polygon1.union(polygon2).area
return intersect/union
return 0
Run Code Online (Sandbox Code Playgroud)