#!/usr/bin/env python # coding: utf-8 # In[54]: import pandas as pd, json, numpy as np import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') # In[55]: flights=json.loads(file('flights_ro.json','r').read()) locations=json.loads(file('locations_ro.json','r').read()) citysave_dest=json.loads(file('citysave_ro_dest.json','r').read()) citysave_arrv=json.loads(file('citysave_ro_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[56]: cc={ 'BCM':u'Bákó', 'SUJ':u'Szatmárnémeti', 'CND':u'Konstanca', 'CRA':u'Craiova', 'TSR':u'Temesvár', 'IAS':u'Jászvásár', 'CLJ':u'Kolozsvár', 'SBZ':u'Szeben', 'OMR':u'Nagyvárad', 'OTP':u'Bukarest', 'SCV':u'Suceava', 'TGM':u'Marosvásárhely' } file("cities_ro.json",'w').write(json.dumps(cc)) # In[57]: #define locationrenamer def namer(s): if s=='Belgrade': return 'Belgrád' elif s=='Budapest': return 'Budapest' elif s=='Cluj-Napoca': return 'Kolozsvár' 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[66]: 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[67]: 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 # In[68]: 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[69]: #clean up for i in list(newdata.keys()): if newdata[i]['count']==0: newdata.pop(i); # In[70]: hnames['Czechia']=u'Csehország' # In[73]: 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[74]: file("f_ro.json",'w').write(json.dumps(F)) # In[75]: F=[] typ=u'Romániai 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[76]: file("g_ro.json",'w').write(json.dumps(F)) # In[38]: s=0 for j in ["CND","TGM","BCM","CRA"]: 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'Romania'} # In[39]: file("newdata1a_ro.json",'w').write(json.dumps(newdata)) # data2a # In[40]: gountrygeo=json.load(file('gountrygeo.json','r')) # In[41]: 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[42]: from pygeocoder import Geocoder apik='AIzaSyDybC2OroTE_XDJTuxjKruxFpby5VDhEGk' # In[43]: 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[44]: file("gountrygeo.json",'w').write(json.dumps(gountrygeo)) # In[45]: 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[46]: 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[47]: file("newdata2a_ro.json",'w').write(json.dumps(newdatar)) # colors # In[48]: order=['OTP','CLJ','TSR','IAS','SBZ','CRA','TGM','SCV','BCM','OMR','SUJ','CND'] # In[49]: ss=[ '#dd1c77', '#f768a1', '#fecc5c', '#fd8d3c', '#f03b20', '#2c7fb8', '#41b6c4', '#a1dab4', '#78c679', '#31a354', '#006837', '#1c9099', '#016c59', '#993404'] # In[50]: file("colors_ro.json",'w').write(json.dumps({order[i]:ss[i] for i in range(len(order))})) # In[51]: file("citycoords_ro.json",'w').write(json.dumps({i:locations[i] for i in locations if i in cc})) # In[52]: file("cityorder_ro.json",'w').write(json.dumps(order))