[英]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
method从
make_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.