用Python更新geojson文件中的值

用Python更新geojson文件中的值,python,json,pandas,geojson,geopandas,Python,Json,Pandas,Geojson,Geopandas,我有如下geojson文件: { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": {}, "geometry": { "type": "LineString", "coordinates": [ [ 57.45849609375, 57.3

我有如下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
          ]
        ]
      }
    }
  ]
}
我想用多边形替换
中的
LineString
“type”:“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
          ]
        ]
      }
    }
  ]
} 
获取第一行字符串的
类型
坐标
的脚本:

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)


这可以使用python中的json模块完成。请参考到目前为止您尝试了什么?看看如何将json读入python字典(使用json模块),并使用这些字典。感谢您的评论。我将尝试更新我的问题。