Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

capital_latlng #18

Merged
merged 4 commits into from
May 27, 2020
Merged

capital_latlng #18

merged 4 commits into from
May 27, 2020

Conversation

DavidBreuer
Copy link

@DavidBreuer DavidBreuer commented May 27, 2020

  • This PR addresses issue/enhancement Add capital coordinates? #17 by adding a method .capital_latlng() and the corresponding data to the .json files.
  • I adjusted the README files accordingly (using only .md would reduce the redundancy here).
  • I also increase the package version number, feel free to revert if unwanted.
  • To find/set the new coordinates, I used geopy, please see script below.
  • Finally, for those countries without a given capital, I didn't add coordinates.
  • If there are any issues with this PR, I'm happy to fix them. Thanks for your super quick replies and best!

@DavidBreuer
Copy link
Author

DavidBreuer commented May 27, 2020

Here is the used Python script (maybe it is helpful for someone) and the list of countries for which no coordinates were added:

# import modules
import os
import json
import io
import fileinput
import geopy
import geopy.geocoders

# capital json/dictionary key
pos = 'capital'
# new capital coordinates json/dictionary key
key = 'capital_latlng'
# path to original json files
path = '/media/Volume/Programs/countryinfo/countryinfo/data_old'

# prepare geocoder
geolocator = geopy.geocoders.Nominatim(user_agent='my-application')

# define auxiliary function
def insert_key_value(dct, pos, key, val):
    # insert after position key
    pos = np.argmax([pos == kei for kei in dct.keys()])
    poz = pos + 1
    # create new dictionary
    new = dict()
    for idx, (ikey, ival) in enumerate(dct.items()):
        if pos >= 0 and idx == poz:
            new[key] = val
        new[ikey] = ival
    return new

# loop over country jsons
files = sorted([os.path.join(path, file) for file in os.listdir(path)])
lenf = len(files)
for fi, file in enumerate(files[:]):

    try:

        # read json
        with io.open(file, 'r', encoding='utf8') as fil:
            dct = json.load(fil)

        # get capital and look up coordinates using geopy
        cap = dct['capital']
        loc = geolocator.geocode(cap)
        lat = np.round(loc.latitude, 6)
        lon = np.round(loc.longitude, 6)
        val = [lat, lon]
        new = insert_key_value(dct, pos, key, val)

        # write json
        dump = file.replace('data_old', 'data')
        with io.open(dump, 'w', encoding='utf8') as fil:
            json.dump(new, fil, ensure_ascii=False)

        # remove blanks
        with fileinput.FileInput(dump, inplace=True, backup=None) as fil:
            for line in fil:
                lin = line.replace(', ', ',').replace(': ', ':')
                print(lin, end='')

    except Exception:

        # print skipped country names
        print(os.path.basename(file))


# list of countries without given capitals
#antarctica.json
#ashmore_and_cartier_island.json
#burma.json
#clipperton_island.json
#europa_island.json
#gaza_strip.json
#glorioso_islands.json
#heard_island_and_mc_donald_islands.json
#holy_see_vatican_city.json
#hungary.json
#index.js
#ireland_northern.json
#jan_mayen.json
#jarvis_island.json
#johnston_atoll.json
#juan_de_nova_island.json
#macau.json
#midway_islands.json
#netherlands_antilles.json
#scotland.json
#virgin_islands.json
#wales.json
#west_bank.json

@porimol porimol merged commit 05cdf75 into porimol:master May 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants