Last active
August 29, 2015 14:21
-
-
Save alexprengere/d4ed1527f4c89a697755 to your computer and use it in GitHub Desktop.
Generate timezone file from optd_por_best_known_so_far.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
from GeoBases import GeoBase | |
import pytz | |
def load_best_known(filename): | |
with open(filename) as f: | |
f.next() # skipping header | |
for row in f: | |
_, iata, lat, lng, _, _ = row.rstrip().split('^') | |
geocode = float(lat), float(lng) | |
yield iata, geocode | |
def main(filename): | |
db_oripor = GeoBase('ori_por', verbose=False) | |
db_geonames = GeoBase('cities', verbose=False) | |
print 'iata_code^tz' | |
for iata, geocode in load_best_known(filename): | |
try: | |
tz = db_oripor.get(iata, 'timezone') | |
pytz.timezone(tz) | |
except pytz.exceptions.UnknownTimeZoneError: | |
# timezone in ori_por was not valid, we compute it from GeoNames | |
_, id_ = db_geonames.findClosestFromPoint(geocode).next() | |
tz = db_geonames.get(id_, 'timezone') | |
print '{0}^{1}'.format(iata, tz) | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
print 'Need one argument: optd_por_best_known_so_far.csv file' | |
exit(1) | |
else: | |
main(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment