【问题标题】:An empty polygon generated with OpenTripPlanner generates error with .is_empty Shapely instruction使用 OpenTripPlanner 生成的空多边形使用 .is_empty Shapely 指令生成错误
【发布时间】:2017-04-14 09:08:31
【问题描述】:

我有一个用 OpenTripPlanner 生成的等时多边形:

{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"MultiPolygon","coordinates":[]},"properties":{"time":-47},"id":"fid--576b228b_15b66d32d71_-7cbd"}]}

这个多边形被翻译成一个 Shapely 对象,指令如下:

isochrone = shapely.geometry.asShape(isochroneJSON['features'][0]['geometry'])

这就是它在 Spyder 中的样子:

{u'type': u'FeatureCollection', u'features': [{u'geometry': {u'type': u'MultiPolygon', u'coordinates': []}, u'type': u'Feature', u'properties': {u'time': -47}, u'id': u'fid--576b228b_15b66d32d71_-7a54'}]}

在我看来,它真的像一个空的多边形。我的问题是我想将它排除在我的其余治疗之外,并检查它是否有效和/或为空。以及以下指令:

if not isochrone.is_empty:

使用 .is_empty shapely 指令生成错误:

return (self._geom is None) or bool(self.impl['is_empty'](self))
self.__geom__, n = self.factory(self.context)

我完全迷失了,因为the only similar question 似乎没有我自己的问题。

【问题讨论】:

    标签: python polygon shapely opentripplanner


    【解决方案1】:

    空几何有点棘手,在您的特定情况下(MultiPolygons),您可以通过使用shape 而不是asShape 来部分修复它:

    import json
    from shapely.geometry import MultiPolygon, shape, mapping, asShape
    
    Q = shape({'type': 'MultiPolygon', 'coordinates': []})
    print(Q.is_empty)
    print(Q.geom_type)
    print(json.dumps(mapping(Q)))
    

    这个输出(geom_type 在空多面的情况下确实不等于MultiPolygon):

    True
    GeometryCollection
    {"type": "MultiPolygon", "coordinates": []}
    

    【讨论】:

    • 在我的情况下,仅使用 'shape' 而不是 'asShape' 已经消除了错误。但是不要问我为什么!!
    猜你喜欢
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 2018-04-23
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    相关资源
    最近更新 更多