簡體   English   中英

TopologicalError:無法執行操作“GEOSIntersection_r”

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

大家好,我正在嘗試將地區 shapefile 映射到議會選區。 我有兩者的形狀文件。基本上我必須將人口普查數據中在地區級別給出的所有變量映射到議會選區級別。 所以我正在關注 pycon talk 一切正常,但我在 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>

我曾嘗試同時使用 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>

錯誤消息准確地告訴您發生了什么。 您的某些幾何圖形無效,因此您必須在應用之前使它們有效。 在大多數情況下有效的簡單技巧是使用buffer(0)

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

由於問題與幾何有效性有關並且由 GEOS 提出,因此使用 shapely/rtree 后端或 pygeos 都沒有關系。

make_valid 1.8 開始,會有一個make_valid方法

然而,目前勻稱的 1.8 還不是 pypi 上的穩定版本,您需要安裝一個不穩定的版本

pip3 install shapely==1.8a2 

然后你可以使形狀有效

from shapely.validation import make_valid

valid_shape = make_valid(invalid_shape)

請注意,形狀的類型可能會發生變化,例如從多邊形變為多多邊形。

但是,我認為最好 (1) 正確避免無效形狀或 (2) 選擇make_valid ,因為它是由勻稱的團隊建議的,而不是.buffer(0)解決方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM