【问题标题】:Delete polygons that have one or more side parts in common删除具有一个或多个共同边部分的多边形
【发布时间】:2021-07-02 16:02:22
【问题描述】:

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

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

本案例的数据在 GeoJson 文件中如下:

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

是否有解决方案可以删除两个 蓝色 多边形(id 2 和 5)?在python中。

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

【问题讨论】:

  • 您可以使用具有 x 或 y 坐标和区间之一作为值的字典。查找相交区间。
  • 间隔作为值”是什么意思?

标签: python geojson shapely


【解决方案1】:

使用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.53328],
            [3.49605, 45.57055],
            [3.45451, 45.57055],
            [3.45451, 45.53328]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "id": 2
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [3.45451, 45.52917],
            [3.49605, 45.52917],
            [3.49605, 45.53891],
            [3.45451, 45.53891],
            [3.45451, 45.52917]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "id": 3
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [3.48455, 45.52980],
            [3.51596, 45.52980],
            [3.51596, 45.54338],
            [3.48455, 45.54338],
            [3.48455, 45.52980]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "id": 4
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [3.465328, 45.54266],
            [3.473567, 45.54266],
            [3.473567, 45.54783],
            [3.465328, 45.54783],
            [3.465328, 45.54266]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "id": 5
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [3.454513, 45.56799],
            [3.458805, 45.56799],
            [3.458805, 45.57055],
            [3.454513, 45.57055],
            [3.454513, 45.56799]
          ]
        ]
      }
    }
  ]
}

【讨论】:

  • 非常感谢@Robin!我没有注意到shared_paths 方法,它运行良好,非常有用。使用 geopandas 代码更短。
猜你喜欢
  • 1970-01-01
  • 2015-01-16
  • 2021-06-18
  • 1970-01-01
  • 1970-01-01
  • 2011-10-02
  • 2023-04-06
  • 2018-08-24
  • 2020-02-08
相关资源
最近更新 更多