opentraveldata / geobases

Data services and visualization
http://opentraveldata.github.com/geobases/
Other
193 stars 41 forks source link

Include currency in ori_por_public.csv #20

Closed tadhgpearson closed 10 years ago

tadhgpearson commented 10 years ago

Helpful to determine which officeID should be used for a given origin when making faring queries.

Currency data taken from https://raw.githubusercontent.com/datasets/country-codes/master/data/country-codes.csv

Code for currency merge below:

import csv

def add_currency_to_points(por_reader, currencies):
    por_with_currency = []
    for point in por_reader:
        country = point['country_code']

        if country in currencies:
            currency = currencies[country]
            point['currency_code'] = currency
        else:
            print "Could not parse " + point[
                'iata_code'] + "   Country code is " + country + "   Currency code not found"

        por_with_currency.append(point)

    return por_with_currency

def read_currencies(currency_file_path):
    currency_file = open(currency_file_path, 'rb')
    currency_reader = csv.DictReader(currency_file)
    currencies = {}
    for c in currency_reader:
        currency_code = c['currency_alphabetic_code']
        country_code = c['ISO3166-1-Alpha-2']
        currencies[country_code] = currency_code
    currency_file.close()
    return currencies

currency_file_path = 'country-codes.csv'
locations_file_path = 'ori_por_public.csv'
output_file_path = 'ori_por_public_out.csv'

locations_file = open(locations_file_path, 'rb')
por_reader = csv.DictReader(locations_file, delimiter='^', )
fieldnames = por_reader.fieldnames

currencies = read_currencies(currency_file_path)

por_with_currency = add_currency_to_points(por_reader, currencies)

locations_file.close()

fieldnames.append('currency_code')
output_file = open(output_file_path, 'wb')
out = csv.DictWriter(output_file, delimiter='^', fieldnames=fieldnames)
out.writeheader()
out.writerows(por_with_currency)
output_file.close()

Signed-off-by: tpearson <tadhg.pearson@amadeus.com>
alexprengere commented 10 years ago

I slightly edited your PR for the Python code to display nicely.

I would agree with the rationale, e.g. having a main currency code associated with each point of reference could be useful. With the GeoBases library this is fairly easy though.

from GeoBases import GeoBase
g = GeoBase('ori_por', verbose=False)
c = GeoBase('countries', verbose=False)

country = g.get('ORY', 'country_code')     # 'FR'
currency = c.get(country, 'currency_code') # 'EUR'

Still, direct access to the currency may be useful to people using the raw ori_por_public.csv.

Regarding the process, the ori_por_public.csv file is generated with optd, and then just copied in this project. So any change to that file should be included in the optd project, and discussed with @denisarnaud :wink:.

So I am going to close this PR here, and suggest you reopen it here.