Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

add iso 3611-1-alpha codes #30

Open
jerCarre opened this issue Apr 8, 2020 · 2 comments
Open

add iso 3611-1-alpha codes #30

jerCarre opened this issue Apr 8, 2020 · 2 comments

Comments

@jerCarre
Copy link

jerCarre commented Apr 8, 2020

Hello,

Could you add 2 letters and 3 letters codes in properties of countries ?

Thx

@Falenos
Copy link

Falenos commented Dec 19, 2020

Hi

Also in need for it..

@elibroftw
Copy link

elibroftw commented Mar 2, 2023

I added the alpha 3 codes and cleaned it up to the best of my abilities.

https://github.com/elibroftw/world-atlas/blob/master/countries-50m.json

Pythons script to help others convert the other files. Requires manually googling the countries that are printed. Some places definitely do not have an alpha 3 code.

import requests
import json

url = 'https://restcountries.com/v2/name/'

// will be overwritten
FILE = 'input.json'

with open(FILE, encoding='utf-8') as f:
    b = json.load(f)
for geo in b['objects']['countries']['geometries']:
    name = geo['properties']['name']
    if 'Is.' in name:
        name = geo['properties']['name'] = name.replace('Is.', 'Islands')
    if 'St.' in name:
        name = geo['properties']['name'] = name.replace('St.', 'Saint')
    if 'St-' in name:
        name = geo['properties']['name'] = name.replace('St-', 'Saint ')
    if 'N.' in name:
        name = geo['properties']['name'] = name.replace('"N. ', '"Northern ')
        name = geo['properties']['name'] = name.replace(' N. ', ' Northern ')
    if 'W.' in name:
        name = geo['properties']['name'] = name.replace('"W. ', '"Western ')
        name = geo['properties']['name'] = name.replace(' W. ', ' Western ')
    if 'Ter.' in name:
        name = geo['properties']['name'] = name.replace('Ter.', 'Territory')
    if 'Fr.' in name:
        name = geo['properties']['name'] = name.replace('Fr.', 'French')
    if 'Eq.' in name:
        name = geo['properties']['name'] = name.replace('Eq.', 'Equatorial ')
    if 'Rep.' in name:
        name = geo['properties']['name'] = name.replace('Rep.', 'Republic')
    if 'I.' in name:
        name = geo['properties']['name'] = name.replace('I.', 'Island')
    _id = geo['properties'].pop('id', None)
    try:
        if _id is None:
            _id = geo['id']
        if _id.isnumeric():
            r = requests.get(url + name)
            if r.status_code == 404:
                print(name, f'({_id}) could not be found')
            else:
                alpha3Code = r.json()[0]['alpha3Code']
                geo['id'] = alpha3Code
        else:
            geo['id'] = _id
    except (KeyError, AttributeError):
        if _id is not None:
            geo['id'] = _id

with open(FILE, 'w', encoding='utf-8') as f:
    json.dump(b, f, indent=4)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants