python - 在 Python 中更新 geojson 文件中的值

标签 python json pandas geojson geopandas

我有 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,并且替换每个最后一个点的坐标code>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
          ]
        ]
      }
    }
  ]
} 

获取第一个LineString类型坐标的脚本:

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 - 在 Python 中更新 geojson 文件中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55178112/

相关文章:

python - 无法将 python 类型 str 转换为列表数组

python - 无法切换到帧——不断出现 AttributeError

PHP 在 JSON 数组中传回带有特殊字符的对象

PHP/MySQL - 使用 json 编码数据而不是数据库中的多列

java - 如何阻止 JsonBuilder 排列字段顺序

python - 更改列格式,同时忽略(或保留)NaN

python - 如何消除 ☎ unicode?

python - 将文本编码为 win32api.SendMessage() 的 WPARAM

python - 在 Pandas 中循环 MAPE 函数会抛出错误

python - Pandas 切片因 DatetimeIndex 标签列表而失败