Skip to content

Instantly share code, notes, and snippets.

@alexprengere
Last active December 12, 2015 00:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexprengere/4682971 to your computer and use it in GitHub Desktop.
Save alexprengere/4682971 to your computer and use it in GitHub Desktop.
Map cities to points of reference using GeoBases ori_por data source
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Map cities to points of reference sorted
by pageranks.
"""
from GeoBases import GeoBase
def to_float(string):
"""String to float conversion, 0 if failure.
"""
try:
val = float(string)
except ValueError:
val = 0
return val
def main():
g = GeoBase('ori_por', verbose=False)
# location_type for cities are ('C',) or ('C','A')
# note that in earlier version of GeoBases
# the location_type is not splitted, so we should
# look for 'C' or 'CA'
# This lambda function works in both version :)
is_city = lambda lt: 'C' in lt
for key in g:
if not is_city(g.get(key, 'location_type')):
continue
# Associated por for the city
tvl_list = g.get(key, 'tvl_por_list')
# (page_rank, code)
pr = [
(to_float(g.get(k, 'page_rank')), k)
for k in tvl_list
]
pr.sort(reverse=True)
print '%s: %s' % (g.get(key, 'iata_code'),
'-'.join('%s[%.2f]' % (k, p) for p, k in pr))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment