TopologicalError: The operation 'GEOSIntersection_r' could not be performed
TopologicalError: The operation 'GEOSIntersection_r' could not be performed
Hi Guys,
I am trying to map the district shapefile into assembly constituencies.
I have shape files for Both.Basically I have to map all the variables given at district level in census data to assembly constituency level.
So I am following a pycon talk.
Everything is working fine but I am getting error in get_intersection function.Error for that is
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.But 没有 avail.Please 的帮助
提前致谢。
我试过的代码是
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>
错误消息告诉您到底发生了什么。您的某些几何形状无效,因此您必须在申请之前使它们有效。在大多数情况下都有效的简单技巧是使用 buffer(0)
.
merged['geometry'] = merged.buffer(0)
由于该问题与几何有效性有关并且由 GEOS 提出,因此使用 shapely/rtree 后端或 pygeos 并不重要。
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)
请注意,形状的类型可能会发生变化,例如从多边形变为多边形。
但是,我认为最好 (1) 正确避免无效形状或 (2) 选择 make_valid
,因为它是由 shapely 团队建议的,而不是 .buffer(0)
解决方法.
Hi Guys, I am trying to map the district shapefile into assembly constituencies. I have shape files for Both.Basically I have to map all the variables given at district level in census data to assembly constituency level. So I am following a pycon talk. Everything is working fine but I am getting error in get_intersection function.Error for that is
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.But 没有 avail.Please 的帮助 提前致谢。
我试过的代码是
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>
错误消息告诉您到底发生了什么。您的某些几何形状无效,因此您必须在申请之前使它们有效。在大多数情况下都有效的简单技巧是使用 buffer(0)
.
merged['geometry'] = merged.buffer(0)
由于该问题与几何有效性有关并且由 GEOS 提出,因此使用 shapely/rtree 后端或 pygeos 并不重要。
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)
请注意,形状的类型可能会发生变化,例如从多边形变为多边形。
但是,我认为最好 (1) 正确避免无效形状或 (2) 选择 make_valid
,因为它是由 shapely 团队建议的,而不是 .buffer(0)
解决方法.