【发布时间】: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