如何根据 python 中更新版本的 geoJSON 文件制作新文件?

How to make a new file based on the updated version of geoJSON file in python?

我一直在尝试更新一个大的 geoJSON 文件 我想将所有 NAME_1 更新为小写并将其保存在新文件中,有什么办法吗?

这是我的 geoJSON 文件的一部分 坐标比较多,直接截下来做成.... 在这里简化 post 对于 NAME_1 变量,实际上大约有 32 个,我只是为了简化目的而减为两个,但我需要更新所有 32

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          [
            [
              [
                ......
             ]
            ]
          ]
        ]
      },
      "properties": {
        "GID_0": "IND",
        "NAME_0": "India",
        "GID_1": "IND.1_1",
        "NAME_1": "Andaman and Nicobar",
        "VARNAME_1": "Andaman & Nicobar Islands|Andaman et Nicobar|Iihas de Andama e Nicobar|Inseln Andamanen und Nikobare",
        "NL_NAME_1": "",
        "TYPE_1": "Union Territor",
        "ENGTYPE_1": "Union Territory",
        "CC_1": "",
        "HASC_1": "IN.AN"
      }
   },
      {
      "type": "Feature",
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          [
            [
              [
                ......
              ]
            ]
          ]
        ]
      },
      "properties": {
        "GID_0": "IND",
        "NAME_0": "India",
        "GID_1": "IND.2_1",
        "NAME_1": "Andhra Pradesh",
        "VARNAME_1": "",
        "NL_NAME_1": "",
        "TYPE_1": "State",
        "ENGTYPE_1": "State",
        "CC_1": "",
        "HASC_1": "IN.AP"
      }

还有我的代码:

import sys
from geojson import Feature, Point, FeatureCollection, Polygon
import json
import geojson
import pprint
import pandas as pd
import re
import os
#print(sys.version)

with open('gadm36_IND_1.json', 'r') as data_file:
    data = json.load(data_file)

for feature in data['features']:
    #change all the Name_1 into lowercase
    name1 = feature['properties']['NAME_1']
    for f in re.findall("([A-Z]+)", name1):
        name1 = name1.replace(f, f.lower())

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

快帮忙嘿嘿嘿

您需要在原来的data['features']中设置值。

因此,在行 name1 = name1.replace(f, f.lower()) 中替换为 feature['properties']['NAME_1'] = name1.replace(f, f.lower())

另一种方法是添加以下行: feature['properties']['NAME_1'] = name1 在第二个循环中。

假设您要为要素数组中的所有对象转换键 NAME_1 的值。

import sys
import json
import re
import os

with open('gadm36_IND_1.json', 'r') as data_file:
    data = json.load(data_file)

for feature in data['features']:
    # change all the Name_1 into lowercase
    feature['properties']['NAME_1'] = feature['properties']['NAME_1'].lower()

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

此代码将键 NAME_1 的值变为小写并将新数据写入文件 lower.json

下面是我的 lower.json 的样子:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "GID_0": "IND",
        "NAME_0": "India",
        "GID_1": "IND.1_1",
        "NAME_1": "andaman and nicobar",
        "VARNAME_1": "Andaman & Nicobar Islands|Andaman et Nicobar|Iihas de Andama e Nicobar|Inseln Andamanen und Nikobare",
        "NL_NAME_1": "",
        "TYPE_1": "Union Territory",
        "ENGTYPE_1": "Union Territory",
        "CC_1": "",
        "HASC_1": "IN.AN"
      }
    },
    {
      "type": "Feature",
      "properties": {
        "GID_0": "IND",
        "NAME_0": "India",
        "GID_1": "IND.2_1",
        "NAME_1": "andhra pradesh",
        "VARNAME_1": "",
        "NL_NAME_1": "",
        "TYPE_1": "State",
        "ENGTYPE_1": "State",
        "CC_1": "",
        "HASC_1": "IN.AP"
      }
    }
  ]
}

希望对您有所帮助!

使用正则表达式

with open('gadm36_IND_1.json') as data_file:
    Json_Str = data_file.read()
NAME_REGEX = re.compile(r'\"NAME_1\":\s*\"(.*?)\"')
for name in NAME_REGEX.findall(Json_Str):
    Json_Str = Json_Str.replace(name , name.lower())

现在您有一个修改后的 Json 字符串,您可以轻松地将其转换为 json obj