ConvertRegions



In [8]:
import json
import codecs
from operator import itemgetter

json_data = open("configs/regions_m.json").read()
regm = json.loads(json_data)

minId = 5972

def getRegion(id):
    for reg in regm:
        if reg["id"] == id:
            return reg
    return None

def getLevel(region):
    reg = region
    level = 1
    while reg != None and reg.get("parentId", None) != None:
        regParentId = reg["parentId"]
        if(regParentId >= minId):
            regParentId -= minId
        reg = getRegion(regParentId)
        level += 1
    return level

newRegions = []

for reg in regm:
    nreg = {}
    nreg['id'] = reg["id"] + minId
    nreg["name"] = reg["cannonicalName"]
    nreg["dativeCaseName"] = reg["dativeName"]
    nreg["administrativeTypeString"] = reg["localityType"]
    nreg["nameTranslit"] = reg["translitedCannonicalName"]
    nreg["dativeTranslit"] = reg["translitedDativeName"]
    if reg["parentId"] != None:
        nreg["parentId"] = reg["parentId"] + minId
    nreg["lat"] = reg["lat"]
    nreg["lon"] = reg["lon"]
    nreg["level"] = getLevel(nreg)
    newRegions.append(nreg)
    
sortedRegs = sorted(newRegions, key=itemgetter('id')) 
    
jsonString = json.dumps(sortedRegs, ensure_ascii=False)

f = codecs.open('configs/regions_new.json', 'w', "utf-8")
f.write(jsonString)
f.close()

print('Done')


Done

In [ ]: