数值错误:无法从空值创建Shapely几何体

5

在使用cascaded_union(我也尝试了unary_union,但是出现了相同的错误)时,我遇到了以下错误:

ValueError: No Shapely geometry can be created from null value

我已经验证了我的多边形是有效的。 最初,polyB 是无效的,但使用 buffer(0) 转换为有效的多边形。

你有任何想法我做错了什么吗?以下是我的代码:

from shapely.geometry import Polygon
from shapely.ops import cascaded_union

def combineBorders(a, b):
    polyA = Polygon(a)
    polyB = Polygon(b)
    pols = [polyA, polyB]

    for p in pols:
        if p.is_valid == False:
            p = p.buffer(0)
        print(p.is_valid)

True
True

    newShape = cascaded_union(pols) # THIS IS WHERE THE ERROR KEEPS SHOWING UP
    return newShape

这里有一个链接,其中包含polyA,polyB和pols的值(在确认它们有效后)。我在我的Ubuntu 14.04服务器上安装了以下版本:

  • python-shapely 1.3.0
  • libgeos 3.4.2
  • python 2.7
2个回答

13
问题在于缓冲多边形没有被放回到列表pols中,因此无效的几何图形被传递给cascaded_union。 你可以使用以下方法使其更简单和通用,该方法可以接受任意数量的多边形几何图形(不仅仅是两个)。
def combineBorders(*geoms):
    return cascaded_union([
        geom if geom.is_valid else geom.buffer(0) for geom in geoms
    ])

polyC = combineBorders(polyA, polyB)

1
发现了问题。不确定为什么这很重要(我看到的示例显示两种方式),但是将多边形直接放入cascaded_union中,像这样:newShape = cascaded_union([polyA, polyB])就可以工作了。这是完全修订后的代码,它可以正常工作:
from shapely.geometry import Polygon
from shapely.ops import cascaded_union

def combineBorders(a, b):
    polyA = Polygon(a)
    polyB = Polygon(b)
    polyBufA = polyA.buffer(0)
    polyBufB = polyB.buffer(0)
    newShape = cascaded_union([polyBufA, polyBufB])
    return newShape

这也适用于 unary_union


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接