【问题标题】:TopologicalError: The operation 'GEOSIntersection_r' could not be performed拓扑错误:无法执行操作“GEOSIntersection_r”
【发布时间】:2021-01-05 09:25:43
【问题描述】:

大家好, 我正在尝试将地区 shapefile 映射到议会选区。 我有Both 的形状文件。基本上,我必须将人口普查数据中地区级别的所有变量映射到议会选区级别。 所以我正在关注一个 pycon talk。 一切正常,但我在 get_intersection 函数中遇到错误。错误是 TopologicalError: The operation 'GEOSIntersection_r' could not be performed. Likely cause is invalidity of the geometry <shapely.geometry.polygon.Polygon object at 0x7f460250ce10>.

我尝试过同时使用 pygeos 和 rtree。有一些链接说问题出在pygeos中。 所以,我用了rtree。但没有用。请帮忙 提前致谢。

我尝试的代码是

constituencies=gpd.GeoDataFrame.from_file('/content/AC_All_Final.shp')
districts=gpd.GeoDataFrame.from_file('/content/2001_Dist.shp')
districts['AREA'] = districts.geometry.area
constituencies['AREA'] = constituencies.geometry.area
merged = gpd.sjoin(districts, constituencies).reset_index().rename(
    columns={'index': 'index_left'})

def get_intersection(row):
    left_geom = districts['geometry'][row['index_left']]
    right_geom = constituencies['geometry'][row['index_right']]
    return left_geom.intersection(right_geom)

***Error is at this point***
merged['geometry'] = merged.apply(get_intersection, axis=1)
merged['AREA'] = merged.geometry.area
Error trace is given below:
TopologyException: Input geom 1 is invalid: Ring Self-intersection at or near point 77.852561819157373 14.546596140487276 at 77.852561819157373 14.546596140487276
---------------------------------------------------------------------------
TopologicalError                          Traceback (most recent call last)
<ipython-input-17-8123669e025c> in <module>()
      4     return left_geom.intersection(right_geom)
      5 
----> 6 merged['geometry'] = merged.apply(get_intersection, axis=1)
      7 merged['AREA'] = merged.geometry.area

7 frames
/usr/local/lib/python3.6/dist-packages/shapely/topology.py in _check_topology(self, err, *geoms)
     36                     "The operation '%s' could not be performed. "
     37                     "Likely cause is invalidity of the geometry %s" % (
---> 38                         self.fn.__name__, repr(geom)))
     39         raise err
     40 

TopologicalError: The operation 'GEOSIntersection_r' could not be performed. Likely cause is invalidity of the geometry <shapely.geometry.polygon.Polygon object at 0x7f460250ce10>

【问题讨论】:

    标签: python python-3.x geopandas topology r-tree


    【解决方案1】:

    错误消息会告诉您到底发生了什么。您的某些几何图形无效,因此您必须在应用之前使它们有效。在大多数情况下都有效的简单技巧是使用buffer(0)

    merged['geometry'] = merged.buffer(0)
    

    由于该问题与几何有效性有关并且由 GEOS 提出,因此使用 shapely/rtree 后端还是 pygeos 并不重要。

    【讨论】:

    • @martinfleis。使用您的代码后我仍然遇到同样的错误。错误是 TopologicalError:无法执行操作“GEOSIntersection_r”。可能的原因是几何 无效
    • 然后您必须检查该特定多边形的情况。您可以通过merged[~merged.is_valid]过滤有问题的。
    • 非常感谢@martinflies.Was 能够使用此解决
    【解决方案2】:

    从shapely 1.8开始,会有make_valid方法

    但是,目前 shapely 1.8 还不是 pypi 上的稳定版本,您需要安装一个不稳定的版本

    pip3 install shapely==1.8a2 
    

    然后你可以使一个形状有效

    from shapely.validation import make_valid
    
    valid_shape = make_valid(invalid_shape)
    

    请注意,形状的类型可能会发生变化,例如从 Polygon 变为 MultiPolygon。

    但是,我认为最好 (1) 适当地避免无效形状或 (2) 选择 make_valid,因为这是由 shapely 团队建议的,而不是 .buffer(0) 解决方法。

    【讨论】:

    • 这个对我有用,谢谢! (buffer(0) 方法没有)
    猜你喜欢
    • 2020-01-08
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-20
    • 2016-04-16
    相关资源
    最近更新 更多