在python中选择与目标多边形共享边界的多边形最好使用GeoPandas

问题描述

我有一个 GeoDataFrame 作为 gdf。我想选择与 id 为 4 的目标几何体相接触的多边形。但是当我使用 GeoPandas 的 touches() 时,它会吐出共享边界的几何体以及仅接触目标几何体(ID 为 4)一角的几何体。我对 GeoPandas intersects() 的运气也不好,以至于它产生了 touches() 产生的所有几何图形,包括目标几何图形。

我只想选择那些实际上与目标(id 4)共享边界的几何图形,这样输出将仅是 ID 为(3,7,5,1)的几何图形。 code:

import geopandas as gpd
gdf =,Id,geometry
        0,"polyGON ((-2247824.100899419 -4996167.43201861,-2247824.100899419 -4996067.43201861,-2247724.100899419 -4996067.43201861,-2247724.100899419 -4996167.43201861,-2247824.100899419 -4996167.43201861))"
        1,"polyGON ((-2247724.100899419 -4996167.43201861,-2247624.100899419 -4996067.43201861,-2247624.100899419 -4996167.43201861,-2247724.100899419 -4996167.43201861))"
        2,"polyGON ((-2247624.100899419 -4996167.43201861,-2247524.100899419 -4996067.43201861,-2247524.100899419 -4996167.43201861,-2247624.100899419 -4996167.43201861))"
        3,"polyGON ((-2247824.100899419 -4996067.43201861,-2247824.100899419 -4995967.43201861,-2247724.100899419 -4995967.43201861,-2247824.100899419 -4996067.43201861))"
        4,"polyGON ((-2247724.100899419 -4996067.43201861,-2247624.100899419 -4995967.43201861,-2247724.100899419 -4996067.43201861))"
        5,"polyGON ((-2247624.100899419 -4996067.43201861,-2247524.100899419 -4995967.43201861,-2247624.100899419 -4996067.43201861))"
        6,"polyGON ((-2247824.100899419 -4995967.43201861,-2247824.100899419 -4995867.43201861,-2247724.100899419 -4995867.43201861,-2247824.100899419 -4995967.43201861))"
        7,"polyGON ((-2247724.100899419 -4995967.43201861,-2247624.100899419 -4995867.43201861,-2247724.100899419 -4995967.43201861))"
        8,"polyGON ((-2247624.100899419 -4995967.43201861,-2247524.100899419 -4995867.43201861,-2247624.100899419 -4995967.43201861))"

shares_boundary = gdf[gdf.geometry.touches(gdf['geometry'][4])]

gdf

解决方法

您可以编写一个函数,使用匀称的方法,实现您的逻辑,如下所示:

{
  "owner" : {
    "name" : "topic_z_owner"
  }
}

然后使用 def share_boundary(geom1,geom2): if geom1.touches(geom2): if not isinstance(geom1.intersection(geom2),Point): return True return False 将其应用于几何列:

apply