-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransform_to_graphml.py
41 lines (34 loc) · 1.12 KB
/
transform_to_graphml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os.path
import networkx as nx
import pandas as pd
if os.path.exists('data/air_traffic.graphml'):
print('data/air_traffic.graphml file already exists.')
exit(0)
df_airports = pd.read_csv('data/airports.csv')
df_flights = pd.read_csv('data/anac.csv')
# Create graph
G = nx.Graph()
# Add nodes
for index, row in df_airports.iterrows(): # noqa: B007
G.add_node(row['code'],
name=row['name'],
country=row['country'],
latitude=row['lat_geo_point'],
longitude=row['lon_geo_point']
)
# Add edges
df_edges = df_flights[[
'origin_airport_abbreviation',
'destination_airport_abbreviation',
]].dropna()
df_edges = df_edges.groupby(df_edges.columns.tolist(), as_index=False).size()
for index, row in df_edges.iterrows(): # noqa: B007
if row['origin_airport_abbreviation'] == row['destination_airport_abbreviation']:
continue
G.add_edge(
row['origin_airport_abbreviation'],
row['destination_airport_abbreviation'],
flight_count=row['size']
)
# Export to graphml
nx.write_graphml(G, 'data/air_traffic.graphml')