forked from ram-nadella/airport-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreformat.py
executable file
·53 lines (45 loc) · 1.82 KB
/
reformat.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
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
# This script reformat the airport-codes data from
# - https://ourairports.com/data/
# - https://github.com/davidmegginson/ourairports-data
import csv
import json
countries = {}
data = {}
with open('airports.json') as jsonfile:
# noinspection PyRedeclaration
data = json.load(jsonfile)
# Download from: https://davidmegginson.github.io/ourairports-data/countries.csv
with open('countries.csv', newline='') as csvfile:
csvreader = csv.DictReader(csvfile)
for row in csvreader:
countries[row['code']] = row
# Download from: https://davidmegginson.github.io/ourairports-data/airports.csv
with open('airports.csv', newline='') as csvfile:
csvreader = csv.DictReader(csvfile)
for row in csvreader:
old_type = ''
if row['iata_code'] in data:
airport = data[row['iata_code']]
if 'type' in airport:
old_type = airport['type']
else:
airport = {}
if not 'airport' in row['type']:
continue
if old_type == '' or old_type == row['type']:
airport['name'] = row['name']
airport['city'] = row['municipality']
if row['iso_country'] in countries:
airport['country'] = countries[row['iso_country']]['name']
airport['country_code'] = row['iso_country']
airport['iata'] = row['iata_code']
airport['latitude'] = "{:.3f}".format(float(row['latitude_deg']))
airport['longitude'] = "{:.3f}".format(float(row['longitude_deg']))
if row['elevation_ft'] != '':
airport['altitude'] = row['elevation_ft']
airport['type'] = row['type']
# airport['extra'] = row
data[row['iata_code']] = airport
#print(json.dumps(data, indent=4))
print(json.dumps(data))