简体   繁体   English

TopologicalError:无法执行操作“GEOSIntersection_r”

[英]TopologicalError: The operation 'GEOSIntersection_r' could not be performed

Hi Guys, I am trying to map the district shapefile into assembly constituencies.大家好,我正在尝试将地区 shapefile 映射到议会选区。 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 .所以我正在关注 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>一切正常,但我在 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> TopologicalError: The operation 'GEOSIntersection_r' could not be performed. Likely cause is invalidity of the geometry <shapely.geometry.polygon.Polygon object at 0x7f460250ce10> . TopologicalError: The operation 'GEOSIntersection_r' could not be performed. Likely cause is invalidity of the geometry <shapely.geometry.polygon.Polygon object at 0x7f460250ce10>

I have tried to use both pygeos and rtree.我曾尝试同时使用 pygeos 和 rtree。 There were some links which said that problem is in pygeos.有一些链接说这个问题出在 pygeos 中。 So,I used rtree.But of no avail.Please help Thanks in advance.所以,我使用了 rtree。但无济于事。请帮助提前致谢。

Code tried by me is我尝试的代码是

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>

The error message tells you exactly what is going on.错误消息准确地告诉您发生了什么。 Some of your geometries are not valid, so you have to make them valid before doing your apply.您的某些几何图形无效,因此您必须在应用之前使它们有效。 The simple trick, which works in most of the cases is using buffer(0) .在大多数情况下有效的简单技巧是使用buffer(0)

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

Since the issue is with geometry validity and is raised by GEOS, it does not matter if you use shapely/rtree backend or pygeos.由于问题与几何有效性有关并且由 GEOS 提出,因此使用 shapely/rtree 后端或 pygeos 都没有关系。

Starting from shapely 1.8, there will be a make_valid methodmake_valid 1.8 开始,会有一个make_valid方法

However, currently shapely 1.8 isn't a stable release on pypi yet and you'd need to install an unstable one然而,目前匀称的 1.8 还不是 pypi 上的稳定版本,您需要安装一个不稳定的版本

pip3 install shapely==1.8a2 

Then you can make a shape valid as per然后你可以使形状有效

from shapely.validation import make_valid

valid_shape = make_valid(invalid_shape)

Note that the type of shape might change, for example from a Polygon to a MultiPolygon.请注意,形状的类型可能会发生变化,例如从多边形变为多多边形。

However, I'd argue it better to (1) properly avoid invalid shapes or (2) choose make_valid , since it's suggested by the shapely team, rather than the .buffer(0) work around.但是,我认为最好 (1) 正确避免无效形状或 (2) 选择make_valid ,因为它是由匀称的团队建议的,而不是.buffer(0)解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Shapely 中的多边形相交错误:“shapely.geos.TopologicalError:操作‘GEOSIntersection_r’产生了 null 几何图形” - Polygon intersection error in Shapely: "shapely.geos.TopologicalError: The operation 'GEOSIntersection_r' produced a null geometry" 这里正在执行什么减法运算? - What subtraction operation is being performed here? 执行操作时模拟500响应 - Mocking a 500 response when an operation is performed Scapy sniff()“对不是套接字的对象执行了操作” - Scapy sniff() “an operation was performed on something that is not a socket” 在列表和字符串中执行操作时分配的差异 - Difference of assignment when an operation is performed in lists and strings SyntaxError:语法无效,无法对Pandas数据框的列执行操作 - SyntaxError: invalid syntax , operation can't be performed on a column of a Pandas dataframe 确保两次不在字典中的相同值上执行操作的最佳方法? - Best way to ensure that an operation is not performed on the same value in a dictionary twice? 测试操作是否已在python 3 numpy中正确执行? - Testing if an operation has been performed correctly in python 3 numpy? 使用 R 进行切片操作 - SLICING operation with R 执行除法运算时,如何使该算法返回整数? - How can I make this algorithm to return whole numbers when division operation is performed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM