删除具有一个或多个共同边部分的多边形

问题描述

我正在尝试解决将多边形与其他多边形进行比较的特殊情况。我有 五个 多边形分布,如下图所示。黑色多边形是面积最大的那个。

可能还有其他类似的情况,主要规则是删除所有具有一个或多个共同边部分的多边形中最小的多边形

这个案例的数据在一个 GeoJson 文件中,如下所示:

{"type":"FeatureCollection","features":[
    {"type":"Feature","properties":{"id":1},"geometry":{"type":"polygon","coordinates":[[[3.4545135498046875,45.533288879467456],[3.4960556030273433,45.57055337226086],[3.4545135498046875,45.533288879467456]]]}},{"type":"Feature","properties":{"id":2},45.52917023833511],45.53891018749409],45.52917023833511]]]}},"properties":{"id":3},"coordinates":[[[3.4845542907714844,45.5298015824607],[3.5159683227539062,45.543388795387294],[3.4845542907714844,45.5298015824607]]]}},"properties":{"id":4},"coordinates":[[[3.465328216552734,45.542667432984864],[3.4735679626464844,45.5478369923404],[3.465328216552734,45.542667432984864]]]}},"properties":{"id":5},"coordinates":[[[3.4545138850808144,45.56799974017372],[3.4588050842285156,45.57055290285386],[3.4545138850808144,45.56799974017372]]]}}]}

有没有办法删除两个蓝色多边形(id 2和5)?在python中。

通过将多边形转换为 Linestring 可以查看 Linestring 是否是另一个 Linestring 的一部分?但我不知道该怎么做。或者可能想看看黑色和蓝色多边形的 Linestring 是否有两个以上的共同点?但是我们不能把一个 Linestring 转换成两个以上的点。

enter image description here

解决方法

以下方法可能适用于使用 shared_paths 的您,它可以正确调用多边形 1、2 和 5 之间的路径重叠:

import json
import shapely as sh
import shapely.ops as ops
import shapely.geometry as geo

with open('./test.json') as f:
  features = json.load(f)['features']

for f1 in features:
  for f2 in features:
    id1 = f1['properties']['id']
    id2 = f2['properties']['id']
    if int(id1) > int(id2):
      s1 = geo.shape(f1['geometry'])
      s2 = geo.shape(f2['geometry'])
      coll = ops.shared_paths(s1.boundary,s2.boundary)
      if not coll.is_empty:
        print(f"{id1} and {id2} have shared path")
        # update your feature collection etc

我必须将要素几何中的精度降低到 5 个小数位才能使其工作,因为最初它只检测多边形 1 和 2 之间的重叠。多边形 1 和 5 之间的共享角在您的输入 FeatureCollection 中略有出入:

{
  "type": "FeatureCollection","features": [{
      "type": "Feature","properties": {
        "id": 1
      },"geometry": {
        "type": "Polygon","coordinates": [
          [
            [3.45451,45.53328],[3.49605,45.57055],[3.45451,45.53328]
          ]
        ]
      }
    },{
      "type": "Feature","properties": {
        "id": 2
      },45.52917],45.53891],45.52917]
          ]
        ]
      }
    },"properties": {
        "id": 3
      },"coordinates": [
          [
            [3.48455,45.52980],[3.51596,45.54338],[3.48455,45.52980]
          ]
        ]
      }
    },"properties": {
        "id": 4
      },"coordinates": [
          [
            [3.465328,45.54266],[3.473567,45.54783],[3.465328,45.54266]
          ]
        ]
      }
    },"properties": {
        "id": 5
      },"coordinates": [
          [
            [3.454513,45.56799],[3.458805,[3.454513,45.56799]
          ]
        ]
      }
    }
  ]
}