In [1]:
import pandas as pd, json, numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
flights=json.loads(file('flights_hu.json','r').read())
locations=json.loads(file('locations_hu.json','r').read())
citysave_dest=json.loads(file('citysave_hu_dest.json','r').read())
citysave_arrv=json.loads(file('citysave_hu_arrv.json','r').read())
hnames=json.loads(file('hnames.json','r').read())
#example output format
data1a=json.loads(file('data1a.json','r').read())
data2a=json.loads(file('data2a.json','r').read())
In [3]:
cc={
'DEB':u'Debrecen',
'BUD':u'Budapest'
}
In [4]:
#define locationrenamer 

def namer(s):
    
    if s=='Belgrade': return 'Belgrád'
    elif s=='Budapest': return 'Budapest'
    elif s=='Cluj-Napoca': return u'Marosvásárhely'
    elif s=='Timi\xc8\x99oara': return 'Temesvár'
    elif s=='Targu-Mures': return 'Marosvásárhely'
    elif s=='Vienna': return 'Bécs'
    elif s=='Paris': return 'Párizs'
    elif s=='Nagy-V\xc3\xa1rad': return 'Nagyvárad'
    elif s=='Krak\xc3\xb3w': return 'Krakkó'
    elif s=='Sibiu': return 'Nagyszeben'
    elif s=='Bucharest': return 'Bukarest'
    elif s=='Oradea': return 'Nagyvárad'
    elif s=='Brussels': return 'Brüsszel'
    elif s=='Cologne': return 'Köln'
    elif s=='Athens': return 'Athén'
    elif s=='Brasov': return 'Brassó'
    elif s=='Copenhagen': return 'Koppenhága'
    elif s=='Debreczen': return 'Debrecen'
    elif s=='Leipzig': return 'Lipcse'
    elif s=='Munich': return 'München'
    elif s=='Nuremberg': return 'Nüremberg'
    elif s=='Amsterdam': return 'Amszterdam'
    elif s=='Milan': return 'Milánó'
    elif s=='Gyor': return 'Győr'
    elif s=='K\xc3\xb8benhavn': return 'Koppenhága'
    elif s=='Geneva': return 'Genf'
    elif s=='Prague': return 'Prága'
    elif s=='Turin': return 'Torinó'
    elif s=='Rome': return 'Róma'
    elif s=='Nagy-Sz\xc3\xa9ben': return 'Nagyszeben'
    elif s=='Iasi': return 'Jászvásár'
    elif s=='Florence': return 'Firenze'
    elif s=='Warsaw': return u'Varsó'
    elif s=='Kiev': return u'Kijev'
    elif s=='Malmo': return u'Malmö'
    elif s=='Zurich': return u'Zürich'
    elif s=='Beirut': return 'Bejrút'
    elif s=='Riyadh': return 'Rijád'
    elif s=='Jerusalem': return 'Jeruzsálem'
    elif s=='Naples': return 'Nápoly'
    elif s=='Beijing': return 'Peking'
    elif s=='Constanta': return 'Konstanca'
    elif s=='Istanbul': return 'Isztambul'
    elif s=='Corfu': return 'Korfu'
    elif s=='Bratislava': return 'Pozsony'
    elif s=='City of Brussels': return 'Brüsszel'
    elif s=='Dubai': return 'Dubaj'
    elif s=='Timisoara': return u'Temesvár'
    elif s=='Satu Mare': return u'Szatmár'
    elif s=='Sofia': return u'Szófia'
    elif s=='Lisbon': return u'Lisszabon'
    elif s=='Nice': return u'Nizza'
    elif s=='Karlsruhe/Baden-Baden': return u'Karlsruhe'
    elif s=='Moscow': return 'Moszkva'
    
    else: return s
In [5]:
citysave={}
for i in list(citysave_dest)+list(citysave_arrv):
    if i in citysave_dest:
        citysave[i]=citysave_dest[i]
    else:
        citysave[i]=citysave_arrv[i]
In [6]:
citysave['Cluj-Napoca']['coords']=[46.5385862, 24.5514392]
In [7]:
newdata={}
apconv={}
for g in citysave:
    k=namer(g)+'('+str(citysave[g]['coords'][0])+', '+str(citysave[g]['coords'][1])+')'
    apconv[g]=k
    if k not in newdata: newdata[k]={}
    newdata[k]['coords']=citysave[g]['coords']
    newdata[k]['country']=citysave[g]['country']
    newdata[k]['count']=0
C:\Anaconda2\lib\site-packages\ipykernel\__main__.py:8: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
C:\Anaconda2\lib\site-packages\ipykernel\__main__.py:12: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
C:\Anaconda2\lib\site-packages\ipykernel\__main__.py:13: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
C:\Anaconda2\lib\site-packages\ipykernel\__main__.py:29: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
C:\Anaconda2\lib\site-packages\ipykernel\__main__.py:34: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
In [8]:
for c in flights:
    for airport in flights[c]:
        k=apconv[airport]
        newdata[k]['count']+=flights[c][airport]['7freq']
        if c not in newdata[k]:
            newdata[k][c]={"count":0}
        newdata[k][c]['count']+=flights[c][airport]['7freq']
        newdata[k][c]['airports']=flights[c][airport]['airports']
In [9]:
#clean up
for i in list(newdata.keys()):
    if newdata[i]['count']==0: newdata.pop(i);
In [10]:
hnames['Czechia']=u'Csehország'
In [11]:
F=[]
for j in newdata:
    for i in newdata[j]:
        if i not in {'country','count','coords'}:
            for k in newdata[j][i]['airports']:
                for m in newdata[j][i]['airports'][k]['airlines']:
                    if 'cargo' not in m.lower():
                        F.append({'to':j[:j.find('(')],'from':cc[i],'country':newdata[j]['country'],'hcountry':hnames[newdata[j]['country']],'ap':j[:j.find('(')]+' '+str(k),'al':m,u'heti járatszám':np.round(newdata[j][i]['airports'][k]['airlines'][m]['7freq'],1)})
                        #tests
                        F.append({'to':j[:j.find('(')],'from':cc[i],'country':newdata[j]['country'],'hcountry':hnames[newdata[j]['country']],'ap':j[:j.find('(')]+' '+str(k),'al':u'Összesen',u'heti járatszám':0})
                        F.append({'to':j[:j.find('(')],'from':cc[i],'country':newdata[j]['country'],'hcountry':hnames[newdata[j]['country']],'ap':u'Összesen','al':m,u'heti járatszám':0})
                        F.append({'to':j[:j.find('(')],'from':cc[i],'country':u'Összesen','hcountry':u'Összesen','ap':j[:j.find('(')]+' '+str(k),'al':m,u'heti járatszám':0})
                        F.append({'to':j[:j.find('(')],'from':u'Összesen','country':newdata[j]['country'],'hcountry':hnames[newdata[j]['country']],'ap':j[:j.find('(')]+' '+str(k),'al':m,u'heti járatszám':0})
                        F.append({'to':u'Összesen','from':cc[i],'country':newdata[j]['country'],'hcountry':hnames[newdata[j]['country']],'ap':j[:j.find('(')]+' '+str(k),'al':m,u'heti járatszám':0})
In [12]:
file("f_hu.json",'w').write(json.dumps(F))
In [16]:
F=[]
typ=u'Magyarországi járatok'
for j in newdata:
    for i in newdata[j]:
        if i not in {'country','count','coords'}:
            for k in newdata[j][i]['airports']:
                for m in newdata[j][i]['airports'][k]['airlines']:
                    if 'cargo' not in m.lower():
                        F.append({'type':typ,'to':j[:j.find('(')],'from':cc[i],'country':newdata[j]['country'],'hcountry':hnames[newdata[j]['country']],'ap':j[:j.find('(')]+' '+str(k),'al':m,u'heti járatszám':np.round(newdata[j][i]['airports'][k]['airlines'][m]['7freq'],1)})
                        #tests
                        F.append({'type':typ,'to':j[:j.find('(')],'from':cc[i],'country':newdata[j]['country'],'hcountry':hnames[newdata[j]['country']],'ap':j[:j.find('(')]+' '+str(k),'al':u'Összesen',u'heti járatszám':0})
                        F.append({'type':typ,'to':j[:j.find('(')],'from':cc[i],'country':newdata[j]['country'],'hcountry':hnames[newdata[j]['country']],'ap':u'Összesen','al':m,u'heti járatszám':0})
                        F.append({'type':typ,'to':j[:j.find('(')],'from':cc[i],'country':u'Összesen','hcountry':u'Összesen','ap':j[:j.find('(')]+' '+str(k),'al':m,u'heti járatszám':0})
                        F.append({'type':typ,'to':j[:j.find('(')],'from':u'Összesen','country':newdata[j]['country'],'hcountry':hnames[newdata[j]['country']],'ap':j[:j.find('(')]+' '+str(k),'al':m,u'heti járatszám':0})
                        F.append({'type':typ,'to':u'Összesen','from':cc[i],'country':newdata[j]['country'],'hcountry':hnames[newdata[j]['country']],'ap':j[:j.find('(')]+' '+str(k),'al':m,u'heti járatszám':0})
In [17]:
file("g_hu.json",'w').write(json.dumps(F))
In [49]:
s=0
for j in ["DEB","BUD"]:
    newdata[cc[j]+'('+str(locations[j][0])+', '+str(locations[j][1])+')']={j:{'airports': {j: {u'7freq': s,
     u'airlines': {u'Airport': {u'7freq': s}}}},
   'count': s},
  'coords': locations[j],
  'count': s,
  'country': u'Hungary'}
In [50]:
file("newdata1a_hu.json",'w').write(json.dumps(newdata))

data2a

In [51]:
gountrygeo=json.load(file('gountrygeo.json','r'))
In [52]:
gountrygeo["Canada"]=[61, -29]
gountrygeo["United States"]=[57, -29]
gountrygeo["Romania"]=[46.052612, 24.954499]
gountrygeo["Other"]=[58, 44]
gountrygeo["None"]=[0, 0]
gountrygeo[None]=[0, 0]
In [53]:
from pygeocoder import Geocoder
apik='AIzaSyDybC2OroTE_XDJTuxjKruxFpby5VDhEGk'
In [54]:
for c in list(set(citysave[g]['country'] for g in citysave)):
    if c not in gountrygeo:
        print c
        gountrygeo[c]=Geocoder(apik).geocode(c)[0].coordinates
In [55]:
file("gountrygeo.json",'w').write(json.dumps(gountrygeo))
In [56]:
newdatar={}
for g in citysave:
    k=citysave[g]['country']
    if k not in newdatar: newdatar[k]={}
    newdatar[k]['coords']=gountrygeo[k]
    newdatar[k]['count']=0
In [57]:
for c in flights:
    for airport in flights[c]:
        k=citysave[airport]['country']
        newdatar[k]['count']+=flights[c][airport]['7freq']
        if c not in newdatar[k]:
            newdatar[k][c]={"count":0,'cities':{}}
        newdatar[k][c]['count']+=flights[c][airport]['7freq']
        #if airport not in newdatar['cities'][k][c]['cities']:
        newdatar[k][c]['cities'][airport]=flights[c][airport]
In [58]:
file("newdata2a_hu.json",'w').write(json.dumps(newdatar))

colors

In [59]:
file("cities_hu.json",'w').write(json.dumps(cc))
In [60]:
order=['BUD','DEB']
In [61]:
ss={
'DEB':'#1c9099',
'BUD':'#fd8d3c'
}
In [62]:
file("colors_hu.json",'w').write(json.dumps(ss))
In [63]:
file("citycoords_hu.json",'w').write(json.dumps({i:locations[i] for i in locations if i in cc}))
In [64]:
file("cityorder_hu.json",'w').write(json.dumps(order))