在 Python 中更新 geojson 文件中的值

Update values in geojson file in Python

我有如下 geojson 文件:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            57.45849609375,
            57.36801461845934
          ],
          [
            57.10693359375,
            56.31044317134597
          ],
          [
            59.205322265625,
            56.20059291588374
          ],
          [
            59.4140625,
            57.29091812634045
          ],
          [
            57.55737304687501,
            57.36801461845934
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            59.40307617187499,
            57.29685437021898
          ],
          [
            60.8203125,
            57.314657355733274
          ],
          [
            60.74340820312499,
            56.26776108757582
          ],
          [
            59.227294921875,
            56.21281407174654
          ],
          [
            59.447021484375,
            57.29091812634045
          ]
        ]
      }
    }
  ]
}

我想用 Polygon 替换 "type": "LineString" 中的 LineString,并且用第一个点的坐标替换每个 linestring 的最后一个点的坐标以使其关闭如果超过 3 分。

如何在 Python 中使用 geopandas 或 pandas 进行操作?谢谢。

这是预期的输出:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            57.45849609375,
            57.36801461845934
          ],
          [
            57.10693359375,
            56.31044317134597
          ],
          [
            59.205322265625,
            56.20059291588374
          ],
          [
            59.4140625,
            57.29091812634045
          ],
          [
            57.45849609375,
            57.36801461845934
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            59.40307617187499,
            57.29685437021898
          ],
          [
            60.8203125,
            57.314657355733274
          ],
          [
            60.74340820312499,
            56.26776108757582
          ],
          [
            59.227294921875,
            56.21281407174654
          ],
          [
            59.40307617187499,
            57.29685437021898
          ]
        ]
      }
    }
  ]
} 

获取第一个 LineStringtypecoordinates 的脚本:

import json
from pprint import pprint

with open('data.geojson') as f:
    data = json.load(f)

pprint(data)

data["features"][0]["geometry"]['type']
data["features"][0]["geometry"]['coordinates']

您可以使用 json 模块实现:

file_line = 'file.json'
file_poly = 'file_poly.json'

import json
with open(file_line, 'r') as f:
    data = json.load(f)

for feature in data['features']:
    if (feature['geometry']['type'] == 'LineString') & (len(feature['geometry']['coordinates']) >= 3):
        feature['geometry']['type'] = 'Polygon'
        feature['geometry']['coordinates'].append(feature['geometry']['coordinates'][0])

with open(file_poly, 'w+') as f:
    json.dump(data, f, indent=2)