forked from cquest/aixmParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaixm2json.py
executable file
·592 lines (507 loc) · 19.6 KB
/
aixm2json.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#!/usr/bin/env python3
import sys
import json
import math
from shapely.geometry import LineString, Point
from shapely.ops import split, nearest_points, snap
from bs4 import BeautifulSoup
from pyproj import Proj, transform
def substring(geom, start_dist, end_dist, normalized=False):
"""Return a line segment between specified distances along a linear geometry.
Negative distance values are taken as measured in the reverse
direction from the end of the geometry. Out-of-range index
values are handled by clamping them to the valid range of values.
If the start distances equals the end distance, a point is being returned.
If the normalized arg is True, the distance will be interpreted as a
fraction of the geometry's length.
from shapely 1.7
"""
assert(isinstance(geom, LineString))
# Filter out cases in which to return a point
if start_dist == end_dist:
return geom.interpolate(start_dist, normalized)
elif not normalized and start_dist >= geom.length and end_dist >= geom.length:
return geom.interpolate(geom.length, normalized)
elif not normalized and -start_dist >= geom.length and -end_dist >= geom.length:
return geom.interpolate(0, normalized)
elif normalized and start_dist >= 1 and end_dist >= 1:
return geom.interpolate(1, normalized)
elif normalized and -start_dist >= 1 and -end_dist >= 1:
return geom.interpolate(0, normalized)
start_point = geom.interpolate(start_dist, normalized)
end_point = geom.interpolate(end_dist, normalized)
min_dist = min(start_dist, end_dist)
max_dist = max(start_dist, end_dist)
if normalized:
min_dist *= geom.length
max_dist *= geom.length
if start_dist < end_dist:
vertex_list = [(start_point.x, start_point.y)]
else:
vertex_list = [(end_point.x, end_point.y)]
coords = list(geom.coords)
for i, p in enumerate(coords):
pd = geom.project(Point(p))
if min_dist < pd < max_dist:
vertex_list.append(p)
elif pd >= max_dist:
break
if start_dist < end_dist:
vertex_list.append((end_point.x, end_point.y))
else:
vertex_list.append((start_point.x, start_point.y))
# reverse direction result
vertex_list = reversed(vertex_list)
return LineString(vertex_list)
def geo2coordinates(o, latitude=None, longitude=None, recurse=True):
if latitude:
s = latitude
else:
s = o.find('geolat', recursive=recurse).string
lat = s[:-1]
if len(lat)==2 or lat[2]=='.': # DD[.dddd]
lat = float(lat)
elif len(lat)==4 or lat[4]=='.': # DDMM[.mmmm]
lat = int(lat[0:2])+float(lat[2:])/60
else: # DDMMSS[.sss]
lat = int(lat[0:2])+int(lat[2:4])/60+float(lat[4:-1])/3600
if s[-1] == 'S':
lat = -lat
if longitude:
s = longitude
else:
s = o.find('geolong', recursive=recurse).string
lon = s[:-1]
if len(lon) == 3 or lon[3] == '.':
lon = float(lon)
elif len(lon) == 5 or lon[5] == '.':
lon = int(lon[0:3])+float(lon[3:])/60
else:
lon = int(lon[0:3])+int(lon[3:5])/60+float(lon[5:-1])/3600
if s[-1] == 'W':
lon = -lon
return([lon, lat])
def getfield(o, inputname, outputname=None):
if outputname is None:
outputname = inputname
value = o.find(inputname.lower(), recursive=False)
if value:
return {outputname: value.string.replace('#','\n')}
else:
return None
def addfield(prop, field):
if field:
prop.update(field)
return prop
def ahp2json(ahp):
"Aerodrome / Heliport"
# geometry
geom = {"type": "Point", "coordinates": geo2coordinates(ahp)}
# properties
prop = dict()
prop = addfield(prop, getfield(ahp, 'txtname', 'name'))
prop = addfield(prop, getfield(ahp, 'codetype'))
prop = addfield(prop, getfield(ahp, 'codeicao'))
prop = addfield(prop, getfield(ahp, 'codeiata'))
prop = addfield(prop, getfield(ahp, 'valelev','elevation'))
prop = addfield(prop, getfield(ahp, 'uomdistver','vertical_unit'))
prop = addfield(prop, getfield(ahp, 'txtdescrrefpt', 'description'))
return {"type": "Feature", "geometry": geom, "properties": prop}
def obs2json(obs):
"Obstacle"
# geometry
geom = {"type": "Point", "coordinates": geo2coordinates(obs)}
# properties
prop = dict()
prop = addfield(prop, getfield(obs, 'txtname', 'name'))
prop = addfield(prop, getfield(obs, 'txtdescrtype', 'description'))
prop = addfield(prop, getfield(obs, 'txtDescrMarking', 'marked'))
prop = addfield(prop, getfield(obs, 'codeLgt', 'light'))
prop = addfield(prop, getfield(obs, 'valElev','elevation'))
prop = addfield(prop, getfield(obs, 'valHgt','height'))
prop = addfield(prop, getfield(obs, 'uomdistver','vertical_unit'))
return {"type": "Feature", "geometry": geom, "properties": prop}
# <Obs>
# <ObsUid mid="1577950">
# <geoLat>482936.00N</geoLat>
# <geoLong>0015526.00W</geoLong>
# </ObsUid>
# <txtName>22033</txtName>
# <txtDescrType>Pylône</txtDescrType>
# <codeGroup>N</codeGroup>
# <codeLgt>N</codeLgt>
# <txtDescrMarking>non balisé</txtDescrMarking>
# <codeDatum>U</codeDatum>
# <valElev>318</valElev>
# <valHgt>167</valHgt>
# <uomDistVer>FT</uomDistVer>
# </Obs>
def rcp2json(o):
"Runway Center line Position"
# geometry
geom = {"type": "Point", "coordinates": geo2coordinates(o.rcpuid)}
# properties
prop = dict()
if o.ahpuid:
prop = addfield(prop, getfield(o.ahpuid, 'codeid', 'codeicao'))
if o.rwyuid:
prop = addfield(prop, getfield(o.rwyuid, 'txtDesig', 'name'))
prop = addfield(prop, getfield(o, 'valElev','elevation'))
prop = addfield(prop, getfield(o, 'uomdistver','vertical_unit'))
return {"type": "Feature", "geometry": geom, "properties": prop}
# <Rcp>
# <RcpUid mid="1532169">
# <RwyUid mid="1528969">
# <AhpUid mid="1521170">
# <codeId>LFMD</codeId>
# </AhpUid>
# <txtDesig>04/22</txtDesig>
# </RwyUid>
# <geoLat>433253.80N</geoLat>
# <geoLong>0065736.42E</geoLong>
# </RcpUid>
# <codeDatum>WGE</codeDatum>
# <valElev>10</valElev>
# <uomDistVer>FT</uomDistVer>
# </Rcp>
def frange(start, stop, step):
"Float range"
if step > 0:
while start < stop:
yield start
start += step
else:
while start > stop:
yield start
start += step
def xy2angle(x,y):
if x == 0:
if y>0:
angle = pi/2
else:
angle = -pi/2
else:
angle = math.atan(y/x)
if x<0:
angle = angle + pi
elif y<0:
angle = (angle + 2 * pi)
return angle % (2*pi)
def make_circle(lon, lat, radius, srs):
g = []
step = pi*2/120 if radius > 100 else pi*2/8
center_x, center_y = transform(pWGS, srs, lon, lat)
for a in frange(0, pi*2, step):
x = center_x + math.cos(a) * radius
y = center_y + math.sin(a) * radius
lon, lat = transform(srs, pWGS, x, y)
g.append([round(lon,6), round(lat,6)])
return g
def abd2json(o):
"Airspace Border"
# properties
prop = dict()
prop.update({"uid": o.abduid['mid']})
if o.aseuid:
prop = addfield(prop, getfield(o.aseuid, 'codetype'))
prop = addfield(prop, getfield(o.aseuid, 'codeid'))
if o.aseuid["mid"] in ase:
a = ase[o.aseuid["mid"]]
prop = addfield(prop, getfield(a, 'txtname', 'name'))
prop = addfield(prop, getfield(a, 'codeclass', 'class'))
prop = addfield(prop, getfield(a, 'codedistverupper', 'upper_type'))
prop = addfield(prop, getfield(a, 'valdistverupper', 'upper_value'))
prop = addfield(prop, getfield(a, 'uomdistverupper', 'upper_unit'))
prop = addfield(prop, getfield(a, 'codedistverlower', 'lower_type'))
prop = addfield(prop, getfield(a, 'valdistverlower', 'lower_value'))
prop = addfield(prop, getfield(a, 'uomdistverlower', 'lower_unit'))
prop = addfield(prop, getfield(a, 'txtrmk', 'remark'))
# approximate altitudes in meters
if a.uomdistverupper is not None:
up = None
if a.uomdistverupper.string == 'FL':
up = float(a.valdistverupper.string) * ft * 100
elif a.uomdistverupper.string == 'FT':
up = float(a.valdistverupper.string) * ft
elif a.uomdistverupper.string == 'M':
up = float(a.valdistverupper.string)
if up is not None:
prop.update({"upper_m": int(up)})
if a.uomdistverlower is not None:
low = None
if a.uomdistverlower.string == 'FL':
low = float(a.valdistverlower.string) * ft * 100
elif a.uomdistverlower.string == 'FT':
low = float(a.valdistverlower.string) * ft
elif a.uomdistverlower.string == 'M':
low = float(a.valdistverlower.string)
if low is not None:
prop.update({"lower_m": int(low)})
# geometry
g = []
if o.circle:
lon_c, lat_c = geo2coordinates(o.circle,
latitude=o.geolatcen.string,
longitude=o.geolongcen.string)
radius = float(o.valradius.string)
if o.uomradius.string == 'NM':
radius = radius * nm
if o.uomradius.string == 'KM':
radius = radius * 1000
g = make_circle(lon_c, lat_c, radius, Proj(proj='ortho', lat_0=lat_c, lon_0=lon_c))
geom = {"type": "Polygon", "coordinates": [g]}
prop = addfield(prop, getfield(o.valradius, 'radius'))
prop = addfield(prop, getfield(o.uomradius, 'radius_unit'))
else:
avx_list = o.find_all('avx')
for avx_cur in range(0,len(avx_list)):
avx = avx_list[avx_cur]
codetype = avx.codetype.string
if codetype in ['GRC', 'RHL']:
# great-circle segment
g.append(geo2coordinates(avx))
elif codetype in ['CCA', 'CWA']:
# arcs
start = geo2coordinates(avx, recurse=False)
if avx_cur+1 == len(avx_list):
stop = g[0]
else:
stop = geo2coordinates(avx_list[avx_cur+1], recurse=False)
center = geo2coordinates(avx,
latitude=avx.geolatarc.string,
longitude=avx.geolongarc.string)
g.append(start)
# convert to local meters
srs = Proj(proj='ortho', lat_0=center[1], lon_0=center[0])
start_x, start_y = transform(pWGS, srs, start[0], start[1])
stop_x, stop_y = transform(pWGS, srs, stop[0], stop[1])
center_x, center_y = transform(pWGS, srs, center[0], center[1])
# recompute radius from center/start coordinates in local projection
radius = math.sqrt(start_x**2+start_y**2)
# start / stop angles
start_angle = round(xy2angle(start_x-center_x, start_y-center_y),6)
stop_angle = round(xy2angle(stop_x-center_x, stop_y-center_y),6)
step = -0.025 if codetype == 'CWA' else 0.025
if codetype == 'CWA' and stop_angle > start_angle:
stop_angle = stop_angle - 2*pi
if codetype == 'CCA' and stop_angle < start_angle:
start_angle = start_angle - 2*pi
for a in frange(start_angle+step/2, stop_angle-step/2, step):
x = center_x + math.cos(a) * radius
y = center_y + math.sin(a) * radius
lon, lat = transform(srs, pWGS, x, y)
g.append([lon, lat])
elif codetype == 'FNT':
# geographic borders
start = geo2coordinates(avx)
if avx_cur+1 == len(avx_list):
stop = g[0]
else:
stop = geo2coordinates(avx_list[avx_cur+1])
if avx.gbruid["mid"] in gbr:
fnt = gbr[avx.gbruid["mid"]]
start_d = fnt.project(Point(start[0], start[1]), normalized=True)
stop_d = fnt.project(Point(stop[0], stop[1]), normalized=True)
geom = substring(fnt, start_d, stop_d, normalized=True)
for c in geom.coords:
lon, lat = c
g.append([lon, lat])
else:
print('!!! missing GBR', avx.gbruid["mid"])
g.append(start)
else:
g.append(geo2coordinates(avx))
if (len(g)==0):
print(o.prettify())
geom = None
elif len(g) == 1:
geom = {"type": "Point", "coordinates": g[0]}
elif len(g) == 2:
geom = {"type": "LineString", "coordinates": g}
else:
g.append(g[0])
geom = {"type": "Polygon", "coordinates": [g]}
return {"type": "Feature", "geometry": geom, "properties": prop}
# <Abd>
# <AbdUid mid="1570252">
# <AseUid mid="1562867">
# <codeType>SECTOR</codeType>
# <codeId>LFRRVU</codeId>
# </AseUid>
# </AbdUid>
# <Avx>
# <codeType>GRC</codeType>
# <geoLat>493400.00N</geoLat>
# <geoLong>0042700.00W</geoLong>
# <codeDatum>WGE</codeDatum>
# </Avx>
# <Avx>
# <codeType>GRC</codeType>
# <geoLat>500000.00N</geoLat>
# <geoLong>0020000.00W</geoLong>
# <codeDatum>WGE</codeDatum>
# </Avx>
# <Avx>
# <codeType>GRC</codeType>
# <geoLat>494343.00N</geoLat>
# <geoLong>0015825.00W</geoLong>
# <codeDatum>WGE</codeDatum>
# </Avx>
# <Avx>
# <codeType>GRC</codeType>
# <geoLat>485404.00N</geoLat>
# <geoLong>0024822.00W</geoLong>
# <codeDatum>WGE</codeDatum>
# </Avx>
# <Avx>
# <codeType>GRC</codeType>
# <geoLat>484607.00N</geoLat>
# <geoLong>0025950.00W</geoLong>
# <codeDatum>WGE</codeDatum>
# </Avx>
# <Avx>
# <codeType>GRC</codeType>
# <geoLat>484746.00N</geoLat>
# <geoLong>0040157.00W</geoLong>
# <codeDatum>WGE</codeDatum>
# </Avx>
# </Abd>
def gbr2json(o):
"Geographic borders"
# geometry
g = []
l = []
for gbv in o.find_all('gbv'):
if gbv.codetype.string not in ['GRC', 'END']:
print(gbv)
g.append(geo2coordinates(gbv))
l.append((g[-1][0], g[-1][1]))
geom = { "type":"LineString", "coordinates": g }
# properties
prop = dict()
prop = addfield(prop, getfield(o.gbruid, 'txtname', 'name'))
prop = addfield(prop, getfield(o, 'codetype', 'type'))
prop = addfield(prop, getfield(o, 'txtrmk', 'remark'))
return ({"type": "Feature", "geometry": geom, "properties": prop} , l)
# <Gbr>
# <GbrUid mid="1544998">
# <txtName>ZR:LE</txtName>
# </GbrUid>
# <codeType>OTHER</codeType>
# <Gbv>
# <codeType>GRC</codeType>
# <geoLat>424729.76N</geoLat>
# <geoLong>0000031.32W</geoLong>
# <codeDatum>WGE</codeDatum>
# </Gbv>
# ...
def tower2json(o):
"Control towers"
if o.codetype.string == 'TWR' and o.find('geolat'):
# geometry
geom = { "type":"Point", "coordinates": geo2coordinates(o) }
# properties
prop = dict()
prop = addfield(prop, getfield(o.uniuid, 'txtname', 'name'))
return {"type": "Feature", "geometry": geom, "properties": prop}
else:
print("!!! missing TWR coordinates", o)
# <Uni>
# <UniUid mid="1524684">
# <txtName>LFBR MURET</txtName>
# </UniUid>
# <OrgUid mid="1520800">
# <txtName>FRANCE</txtName>
# </OrgUid>
# <AhpUid mid="1521106">
# <codeId>LFBR</codeId>
# </AhpUid>
# <codeType>TWR</codeType>
# <codeClass>OTHER</codeClass>
# <geoLat>432656.96N</geoLat>
# <geoLong>0011549.30E</geoLong>
# <codeDatum>WGE</codeDatum>
# </Uni>
def gsd2json(o):
"Gate stands"
# geometry
geom = { "type":"Point", "coordinates": geo2coordinates(o) }
# properties
prop = dict()
prop = addfield(prop, getfield(o.gsduid, 'txtdesig', 'ref'))
prop = addfield(prop, getfield(o.gsduid.apnuid.ahpuid, 'codeid', 'airport_ref'))
return {"type": "Feature", "geometry": geom, "properties": prop}
# <Gsd>
# <GsdUid mid="1583952">
# <ApnUid mid="1574122">
# <AhpUid mid="1520960">
# <codeId>LFMN</codeId>
# </AhpUid>
# <txtName>LFMN-APRON</txtName>
# </ApnUid>
# <txtDesig>Y2</txtDesig>
# </GsdUid>
# <codeType>OTHER</codeType>
# <geoLat>433923.85N</geoLat>
# <geoLong>0071214.03E</geoLong>
# <codeDatum>WGE</codeDatum>
# </Gsd>
pLocal = Proj(init='epsg:2154')
pWGS = Proj(init='epsg:4326')
nm = 1852 # Nautic Mile to meters
ft = 0.3048 # foot in meter
pi = 3.1415926
print("parsing xml")
aixm = BeautifulSoup(open(sys.argv[1]), 'lxml')
print("extract ahp - aerodromes/heliports")
out = []
for o in aixm.find_all('ahp'):
out.append(ahp2json(o))
with open('aerodromes.geojson','w') as output:
output.write(json.dumps({"type":"FeatureCollection", "features": out}, sort_keys=True, ensure_ascii=False))
print("extract obs - obstacles")
out = []
for o in aixm.find_all('obs'):
out.append(obs2json(o))
with open('obstacles.geojson','w') as output:
output.write(json.dumps({"type":"FeatureCollection", "features": out}, sort_keys=True, ensure_ascii=False))
print("extract rcp - runway centers")
out = []
for o in aixm.find_all('rcp'):
out.append(rcp2json(o))
with open('runway_center.geojson','w') as output:
output.write(json.dumps({"type":"FeatureCollection", "features": out}, sort_keys=True, ensure_ascii=False))
print("extract ase - airspace")
ase = dict()
for o in aixm.find_all('ase'):
ase[o.aseuid['mid']] = o
print("extract gbr - geographic borders")
gbr = dict()
out = []
for o in aixm.find_all('gbr'):
j,l = gbr2json(o)
out.append(j)
gbr[o.gbruid['mid']] = LineString(l)
with open('border.geojson','w') as output:
output.write(json.dumps({"type":"FeatureCollection", "features": out}, sort_keys=True, ensure_ascii=False))
print("extract abd - airspace boundaries")
out = []
for o in aixm.find_all('abd'):
out.append(abd2json(o))
with open('airspace.geojson','w') as output:
output.write(json.dumps({"type":"FeatureCollection", "features": out}, sort_keys=True, ensure_ascii=False))
print("extract uni - control towers")
out = []
for o in aixm.find_all('uni'):
twr = tower2json(o)
if twr:
out.append(twr)
with open('tower.geojson','w') as output:
output.write(json.dumps({"type":"FeatureCollection", "features": out}, sort_keys=True, ensure_ascii=False))
print("extract gsd - gate stands")
out = []
for o in aixm.find_all('gsd'):
out.append(gsd2json(o))
with open('gate_stand.geojson','w') as output:
output.write(json.dumps({"type":"FeatureCollection", "features": out}, sort_keys=True, ensure_ascii=False))
print("done")