python - Shapely : "shapely.geos.TopologicalError: The operation ' GEOSIntersection_r' produced a null geometry"中的多边形相交错误

标签 python exception polygon intersection shapely

我一直在尝试调试这个问题,但无法做到。我试图找到两个 Polygon 对象的交集。它大部分时间都有效,但对于以下情况,它会引发以下异常:

P1 area: 13.125721955
P2 area: 1.0
Traceback (most recent call last):
File "geom2d.py", line 235, in <module>
print p1.intersection(p2)
File "/usr/local/lib/python2.7/dist-packages/shapely/geometry/base.py", line 334, in     intersection
return geom_factory(self.impl['intersection'](self, other))
  File "/usr/local/lib/python2.7/dist-packages/shapely/topology.py", line 47, in __call__
    "The operation '%s' produced a null geometry. Likely cause is invalidity of the geometry %s" % (self.fn.__name__, repr(this)))
shapely.geos.TopologicalError: The operation 'GEOSIntersection_r' produced a null     geometry. Likely cause is invalidity of the geometry <shapely.geometry.polygon.Polygon      object at 0x8e5ad6c>

代码如下。

from shapely.geometry import Point,Polygon,MultiPolygon

poly1 = [(35.0041000000000011, -88.1954999999999956), (34.9917999999999978,         -85.6068000000000069), (32.8404000000000025, -85.1756000000000029), (32.2593000000000032, -84.8927000000000049), (32.1535000000000011, -85.0341999999999985), (31.7946999999999989, -85.1358000000000033), (31.5199999999999996, -85.0438000000000045), (31.3384000000000000, -85.0836000000000041), (31.2092999999999989, -85.1069999999999993), (31.0023000000000017, -84.9943999999999988), (30.9953000000000003, -87.6008999999999958), (30.9422999999999995, -87.5926000000000045), (30.8538999999999994, -87.6256000000000057), (30.6744999999999983, -87.4072000000000031), (30.4404000000000003, -87.3687999999999931), (30.1463000000000001, -87.5240000000000009), (30.1545999999999985, -88.3863999999999947), (31.8938999999999986, -88.4742999999999995), (34.8937999999999988, -88.1020999999999930), (34.9478999999999971, -88.1721000000000004), (34.9106999999999985, -88.1461000000000041)]
poly2 = [(34.7998910000000024, -88.2202139999999986), (34.7998910000000024,  -87.2202139999999986), (35.7998910000000024, -87.2202139999999986), (35.7998910000000024, -88.2202139999999986)]

p1 = Polygon(poly1)
p2 = Polygon(poly2)
print 'P1 area:',p1.area
print 'P2 area:',p2.area
print p1.intersection(p2)

由于它打印了两个多边形的面积,我假设多边形的形成是正确的。我还(以某种方式)打印了第一个多边形以确保它确实是一个简单的多边形。

谁能解释一下为什么我会收到此异常?

编辑:我打印了 p1.is_valid 结果是 False。有一些解释here .搜索字符串 is_valid。上面写着

A valid Polygon may not possess any overlapping exterior or interior rings.

有人可以解释一下这意味着什么吗?是否有可能的解决方法? 顺便说一句,我还注意到,如果我从 poly1 中删除最后一个坐标,则整个过程都有效。也许整个坐标列表使多边形变得复杂

最佳答案

如前所述,p1 无效。在绘制它时,我注意到右下角有一个小“领结”。我假设您在多边形中不需要这个;如果没有,您可以尝试使用 Shapely 的 buffer(0) 技巧(记录在 Shapely 手册中)来解决这个问题:

In [382]: p1.is_valid
Out[382]: False

In [383]: p1 = p1.buffer(0)

In [384]: p1.is_valid
Out[384]: True

buffer(0) 具有以下效果:

之前:

enter image description here

之后:

enter image description here

现在您可以这样做:

print p1.intersection(p2)
POLYGON ((34.9396324323625151 -88.1614025927056559, 34.8937999999999988 -88.1020999999999930, 34.7998910000000024 -88.1137513649788247, 34.7998910000000024 -87.2202139999999986, 34.9994660069532983 -87.2202139999999986, 35.0041000000000011 -88.1954999999999956, 34.9396324323625151 -88.1614025927056559))

请注意,我遇到过一些这样的例子(有些区域看起来更像“鸟巢”而不是简单的领结);检查以确保返回单个 Polygon 对象而不是 MultiPolygon 对象。

关于python - Shapely : "shapely.geos.TopologicalError: The operation ' GEOSIntersection_r' produced a null geometry"中的多边形相交错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13062334/

相关文章:

c# - 关闭表单时的 StackOverflow

javascript - svg 折线的 svg 路径

java - 绘制多边形时出现 ArrayIndexOutOfBoundsException 错误

python - 如何在没有 UI 的情况下打开 Maya?

python - 正则表达式不匹配

python - Python xlsxwriter 库中的 UnicodeDecodeError

java - 为什么将字符声明为 ''(空)会抛出异常?

javascript - 如何在多边形下方绘制多段线?

python - 在拟合随机森林分类器时遇到问题

python - 更新 theano 函数中的参数