forked from wiedehopf/tar1090-db
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoJson.py
executable file
·245 lines (194 loc) · 8.29 KB
/
toJson.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env python3
import sqlite3, json, sys, csv, traceback
import glob
from contextlib import closing
def writedb(blocks, todir, blocklimit, debug):
block_count = 0
files = []
sys.stderr.write('Writing blocks: ')
queue = sorted(blocks.keys())
while queue:
bkey = queue[0]
del queue[0]
blockdata = blocks[bkey]
if len(blockdata) > blocklimit:
if debug: sys.stderr.write('Splitting block' + bkey + 'with' + len(blockdata) + 'entries..\n')
# split all children out
children = {}
for dkey in blockdata.keys():
new_bkey = bkey + dkey[0]
new_dkey = dkey[1:]
if new_bkey not in children: children[new_bkey] = {}
children[new_bkey][new_dkey] = blockdata[dkey]
# look for small children we can retain in the parent, to
# reduce the total number of files needed. This reduces the
# number of blocks needed from 150 to 61
blockdata = {}
children = sorted(children.items(), key=lambda x: len(x[1]))
retained = 1
while len(children[0][1]) + retained < blocklimit:
# move this child back to the parent
c_bkey, c_entries = children[0]
for c_dkey, entry in c_entries.items():
blockdata[c_bkey[-1] + c_dkey] = entry
retained += 1
del children[0]
if debug: sys.stderr.write(len(children) + 'children created,' + len(blockdata) + 'entries retained in parent\n')
children = sorted(children, key=lambda x: x[0])
blockdata['children'] = [x[0] for x in children]
blocks[bkey] = blockdata
for c_bkey, c_entries in children:
blocks[c_bkey] = c_entries
queue.append(c_bkey)
path = todir + '/' + bkey + '.js'
files.append(bkey);
if debug: sys.stderr.write('Writing' + len(blockdata) + 'entries to' + path + '\n')
else:
sys.stderr.write(bkey + ' ')
sys.stderr.flush()
block_count += 1
with closing(open(path, 'w', encoding='utf-8')) as f:
json.dump(obj=blockdata, fp=f, check_circular=False, separators=(',',':'), sort_keys=True)
path = todir + '/files.js'
with closing(open(path, 'w', encoding='utf-8')) as f:
json.dump(obj=files, fp=f, check_circular=False, separators=(',',':'), sort_keys=True)
sys.stderr.write('done.\n')
sys.stderr.write('Wrote ' + str(block_count) + ' blocks\n')
if __name__ == '__main__':
if len(sys.argv) < 3:
sys.stderr.write('Reads the aircrafts.json like this one: https://github.com/Mictronics/readsb/blob/master/webapp/src/db/aircrafts.json with aircraft information and produces a directory of JSON files\n')
sys.stderr.write('Syntax: ' + sys.argv[0] + ' <path to aircraft.json> <path to DB dir>\n')
sys.exit(1)
blocks = {}
for i in range(16):
blocks['%01X' % i] = {}
mil_long = []
for file in glob.glob("./longnames/individual-types/*.csv"):
with open(file, 'rt', encoding='utf-8-sig', errors='backslashreplace') as f:
mil_long.extend(csv.reader(f, delimiter=',', quotechar='|'))
with open(sys.argv[1], 'rt', encoding='utf-8', errors='backslashreplace') as jsonFile:
text = jsonFile.read()
with open('decodedJSON', 'wt', encoding='utf-8') as out:
out.write(text)
with open(sys.argv[1], 'rt', encoding='utf-8', errors='replace') as jsonFile:
noblocks = json.load(jsonFile)
types = {}
if len(sys.argv) >= 4:
with open(sys.argv[3], 'rt', encoding='utf-8', errors='replace') as jsonFile:
newTypes = json.load(jsonFile)
fixKey = {}
delKey = []
useLongName = set([ "ZZZZ","SHIP","BALL","GLID","ULAC","GYRO","UHEL","TWR","GND","PARA","DRON","EMER","SERV" ])
for k,v in noblocks.items():
v['f'] = v['f'] + '00'
tc = v.get('t')
if tc and tc not in useLongName:
nT = newTypes.get(tc)
if nT and nT[0]:
v['d'] = nT[0]
#print(v['d'])
if k != k.upper():
fixKey[k] = v
if len(k) < 6 or len(k) > 6:
delKey.append(k)
for k,v in fixKey.items():
del noblocks[k]
noblocks[k.upper()] = v
for k in delKey:
del noblocks[k]
print('ignoring bad addr: ' + str(k))
if len(sys.argv) >= 5:
with open(sys.argv[4], 'rt', encoding='utf-8', errors='replace_errors') as f:
lines = f.readlines()
for line in lines:
try:
a = json.loads(line)
except ValueError:
traceback.print_exc()
continue
addr = a['icao'].upper()
if len(addr) < 6 or len(addr) > 6:
print('ignoring bad addr: ' + str(addr))
continue
e = noblocks.setdefault(addr, {})
e.setdefault('f', '0000')
f = e['f']
ladd = a.get('faa_ladd')
if ladd:
e['f'] = f[:3] + '1'
mil = a.get('mil')
if mil:
e['f'] = '1' + f[1:]
ownop = a.get('ownop')
if ownop:
e['ownop'] = ownop
reg = a.get('reg')
#if reg and not e.get('r'):
if reg:
e['r'] = reg
icaotype = a.get('icaotype')
#if icaotype and not e.get('t'):
# revert to this as soon as the E170 situation is fixed
#if icaotype:
if icaotype and (icaotype != 'E170' or not e.get('t')):
if e.get('t') and e.get('t') != icaotype:
nT = newTypes.get(icaotype)
if nT: e['d'] = nT[0]
e['t'] = icaotype
year = a.get('year')
if year:
e['year'] = year
pia = a.get('faa_pia')
if pia:
e = noblocks[addr] = {'f': '0010'}
for k, v in noblocks.items():
if 'f' in v and v['f'][2:] == '00':
v['f'] = v['f'][:2]
for row in mil_long:
if len(row) < 5:
print(row)
continue
icao = row[0]
reg = row[1]
tc = row[2]
flags = row[3]
longtype = row[4]
entry = noblocks.get(icao)
if entry and entry.get('t') == tc and entry.get('r') == reg:
entry['d'] = longtype
with open('aircraft.csv', 'w', newline='', encoding='utf-8') as csvfile:
spamwriter = csv.writer(csvfile,
delimiter=';', escapechar='\\',
quoting=csv.QUOTE_NONE, quotechar=None,
lineterminator='\n')
for k,v in sorted(noblocks.items()): # readsb expects a trailing ; which is easily added with , None
spamwriter.writerow([k, v.get('r'), v.get('t'), v.get('f'), v.get('d'), v.get('year'), v.get('ownop'), None ])
regIcao = {}
for k,v in sorted(noblocks.items()):
# if the long type matches the one in newTypes, delete it as it doesn't need to be in this db
# we still need it for the aircraft.csv, but that's already written out
typeCode = v.get('t')
typeLong = v.get('d')
# disable this for now due to openadsb
if False and typeCode and typeLong:
nt = newTypes.get(v.get('t'))
if nt and nt[0] == typeLong:
#print(typeLong)
del v['d']
if "r" in v:
regIcao[v["r"].replace("-", "")] = k
bkey = k[0:1].upper()
dkey = k[1:].upper()
if v and bkey and dkey and bkey in blocks:
blocks[bkey][dkey] = [ v.get('r'), v.get('t'), v.get('f'), v.get('d') ]
else:
print(k)
print(bkey)
print(dkey)
print(v)
with open((sys.argv[2] + '/regIcao.js'), 'w') as out:
json.dump(obj=regIcao, fp=out, check_circular=False, separators=(',',':'), sort_keys=True)
#with open('blocks.json', 'w') as out:
#json.dump(blocks, out)
writedb(blocks, sys.argv[2], 24576, False)
sys.exit(0)