【问题标题】:ValueError: No Shapely geometry can be created from null valueValueError:无法从空值创建形状优美的几何图形
【发布时间】:2019-06-04 18:27:55
【问题描述】:

我在使用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

Here is a link 到 polyA、polyB 和 pols 的值(在确认它们有效之后)。我的 Ubuntu 14.04 服务器上安装了以下版本:

  • python-shapely 1.3.0
  • libgeos 3.4.2
  • python 2.7

【问题讨论】:

    标签: python shapely


    【解决方案1】:

    问题的问题是缓冲的多边形没有放回列表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)
    

    【讨论】:

      【解决方案2】:

      发现问题。不知道为什么这很重要(我已经看到了两种方式的示例),但是在将多边形直接放入 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

      【讨论】:

        猜你喜欢
        • 2018-12-28
        • 2019-06-04
        • 1970-01-01
        • 1970-01-01
        • 2020-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-03
        相关资源
        最近更新 更多