我一直在尝试使用shapely来寻找给定两个点的两个矩形的交点,然后我可以将其扩展到四个点,所以我现在有类似这样的东西……
from shapely.geometry import Polygon
polygon = Polygon([(-3, 2), (1, 2), (-3, -2), (1, -2)])
other_polygon = Polygon([(2, 1), (-1, 1), (2, 4), (-1, 4)])
if polygon.overlaps(other_polygon):
print( polygon.intersection(other_polygon).area )
else:
print(0)这些点通常存储为变量,但为了减少我在这里抛出的代码量,我去掉了所有的逻辑和多余的信息,但本质上这就是传递的内容。但它一直在抛出这个错误,我不明白shapely为什么生气……
TopologyException: Input geom 0 is invalid: Self-intersection at or near point -1 0 at -1 0
Traceback (most recent call last):
File "main.py", line 66, in <module>
print( polygon.intersection(other_polygon).area )
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/shapely/geometry/base.py", line 676, in intersection
return geom_factory(self.impl['intersection'](self, other))
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/shapely/topology.py", line 70, in __call__
self._check_topology(err, this, other)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/shapely/topology.py", line 35, in _check_topology
raise TopologicalError(
shapely.errors.TopologicalError: The operation 'GEOSIntersection_r' could not be performed. Likely cause is invalidity of the geometry <shapely.geometry.polygon.Polygon object at 0x7f49b40b2ee0>如果你真的想看到一些非常丑陋的代码,我已经尝试了一段时间了……
class Point:
def __init__(self, x, y):
self.x = int(x)
self.y = int(y)
ax1,ay1,ax2,ay2, = input().split()
bx1,by1,bx2,by2 = input().split()
ATL = Point(ax2, ay2)
ATR = Point(ax1, ay2)
ABL = Point(ax2, ay1)
ABR = Point(ax1, ay1)
if ax1 < ax2:
if ay1 > ay2:
ATL = Point(ax1, ay1)
ATR = Point(ax2, ay1)
ABL = Point(ax1, ay2)
ABR = Point(ax2, ay2)
else:
ATL = Point(ax1, ay2)
ATR = Point(ax2, ay2)
ABL = Point(ax1, ay1)
ABR = Point(ax2, ay1)
else:
if ay1 > ay2:
ATL = Point(ax2, ay1)
ATR = Point(ax1, ay1)
ABL = Point(ax2, ay2)
ABR = Point(ax1, ay2)
BTL = Point(bx2, by2)
BTR = Point(bx1, by2)
BBL = Point(bx2, by1)
BBR = Point(bx1, by1)
if bx1 < bx2:
if by1 > by2:
BTL = Point(bx1, by1)
BTR = Point(bx2, by1)
BBL = Point(bx1, by2)
BBR = Point(bx2, by2)
else:
BTL = Point(bx1, by2)
BTR = Point(bx2, by2)
BBL = Point(bx1, by1)
BBR = Point(bx2, by1)
else:
if by1 > by2:
BTL = Point(bx2, by1)
BTR = Point(bx1, by1)
BBL = Point(bx2, by2)
BBR = Point(bx1, by2)
# polygon = Polygon([(ATL.x, ATL.y), (ATR.x, ATR.y), (ABL.x, ABL.y), (ABR.x, ABR.y)])
# other_polygon = Polygon([(BTL.x, BTL.y), (BTR.x, BTR.y), (BBL.x, BBL.y), (BBR.x, BBR.y)])
# -3 2 1 -2
# 2 1 -1 4
from shapely.geometry import Polygon
polygon = Polygon([(-3, 2), (1, 2), (-3, -2), (1, -2)])
other_polygon = Polygon([(2, 1), (-1, 1), (2, 4), (-1, 4)])
if polygon.overlaps(other_polygon):
print( polygon.intersection(other_polygon).area )
else:
print(0)发布于 2021-10-05 02:21:54
您的多边形无效:
>>> polygon.is_valid
False
>>> other_polygon.is_valid
False这是因为您没有按正确的顺序指定顶点。如果你自己跟踪它们,你会看到一个Z字形的模式。要解决此问题,请切换最后两个顶点:
>>> polygon = Polygon([(-3, 2), (1, 2), (1, -2), (-3, -2)])
>>> polygon.is_valid
True
>>> other_polygon = Polygon([(2, 1), (-1, 1), (-1, 4), (2, 4)])
>>> other_polygon.is_valid
True你的交集计算也会起作用:
>>> polygon.overlaps(other_polygon)
True
>>> polygon.intersection(other_polygon).area
2.0https://stackoverflow.com/questions/69443026
复制相似问题