嗨,伙计们,我正试着把区型文件映射到议会选区。我有两者都有.Basically的形状文件,我必须将人口普查数据中在地区一级给出的所有变量映射到集合选区级别。所以我跟着一只蟒蛇谈话。一切都很好,但我在get_intersection function.Error中遇到了错误,因为这就是
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。有一些链接说,这个问题是在侏儒。因此,我提前使用了rtree.But of no avail.Please help。
我试过的代码是
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.areaError 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>发布于 2020-09-18 21:45:06
错误消息确切地告诉您发生了什么。你的一些几何图形是无效的,所以你必须在申请之前使它们有效。在大多数情况下,简单的技巧是使用buffer(0)。
merged['geometry'] = merged.buffer(0)由于这个问题具有几何有效性,并且是由GEOS提出的,所以使用shapely/rtree后端或pygeos并不重要。
发布于 2021-07-16 10:02:10
从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)注意,形状的类型可能会改变,例如,从多边形到MultiPolygon。
但是,我认为最好是(1)适当地避免无效的形状,或者(2)选择make_valid,因为它是由shapely团队建议的,而不是.buffer(0)在周围工作。
https://stackoverflow.com/questions/63955752
复制相似问题