拓扑错误:无法执行操作“Geoscrossion\r”

2024-09-30 08:21:36 发布

您现在位置:Python中文网/ 问答频道 /正文

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。但是没有用。请帮忙 提前谢谢

我尝试的代码是

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>

Tags: theingetindexisareamergedleft
2条回答

错误消息准确地告诉您发生了什么。某些几何图形无效,因此在执行应用之前,必须使其有效。在大多数情况下有效的简单技巧是使用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)解决方法

相关问题 更多 >

    热门问题