-
Notifications
You must be signed in to change notification settings - Fork 1
/
osm2apt.py
executable file
·1210 lines (978 loc) · 48.7 KB
/
osm2apt.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# Global variables that are user settable.
shoulderWidth = 1.0
import argparse
import math
import copy
from imposm.parser import OSMParser
from shapely.geometry import *
from shapely.geometry.polygon import orient
from shapely.ops import cascaded_union
overpassQueryFile = open('overpass_query.txt', 'w')
overpassQueryFile.write('data=\n\n[timeout:600];\n\n(\n')
def lookahead(iterable):
it = iter(iterable)
last = it.next()
for val in it:
yield last, False
last = val
yield last, True
def metersToDeg(meters):
return (meters / (6371000 * 2 * 3.1415927)) * 360
deltaM = metersToDeg(0.1)
def computeHeading(coord1, coord2):
lat1 = coord1[1]; lon1 = coord1[0];
lat2 = coord2[1]; lon2 = coord2[0];
lat1 *= math.pi / 180.0
lon1 *= math.pi / 180.0
lat2 *= math.pi / 180.0
lon2 *= math.pi / 180.0
dLon = lon2 - lon1;
y = math.sin(dLon) * math.cos(lat2)
x = math.cos(lat1)*math.sin(lat2) - \
math.sin(lat1)*math.cos(lat2)*math.cos(dLon)
hdg = math.atan2(y, x) * 180.0 / math.pi
hdg = normalizeHeading(hdg)
return hdg
def headingToRunwayInt(heading):
num = round(heading/ 10.0)
if num == 0:
num = 36
return int(num)
def headingToRunwayString(heading):
return '{0:02d}'.format(headingToRunwayInt(heading))
def normalizeHeading(heading):
while heading < 0:
heading += 360
while heading > 360:
heading -= 360
return heading
def headingToDeg(heading):
return (450 - heading) % 360.0
def computeTurnTo(pos1, origHeading, pos2):
origHeading = normalizeHeading(origHeading)
newHeading = computeHeading(pos1, pos2)
return computeHeadingDelta(origHeading, newHeading)
def computeHeadingDelta(origHeading, newHeading):
origHeading = normalizeHeading(origHeading)
newHeading = normalizeHeading(newHeading)
diff = math.fabs(origHeading - newHeading)
if diff <= 180:
amount = diff
if newHeading >= origHeading:
direction = 'right'
else:
direction = 'left'
else:
amount = 360 - diff
if origHeading >= newHeading:
direction = 'right'
else:
direction = 'left'
return (amount, direction)
def computeSegmentHeading(node, nodes, coords):
# Determine which node along the way the node occurs on.
index = nodes.index(node)
# If the node is any but the last one, we use the direction of the
# _following_ segment of the taxiway to get the direction, however
# if the node is the last in the taxiway, then there is no
# following segement so we use the previous segment instead.
if index < (len(nodes) - 1):
p1 = coords[index]
p2 = coords[index+1]
pos = p1
else:
p1 = coords[index-1]
p2 = coords[index]
pos = p2
heading = computeHeading(p1, p2)
return (heading, pos)
def computeHeadingAtPoint(line, point, towardPoint=-1, delta=metersToDeg(0.5)):
distance = line.project(point)
if towardPoint != -1:
distanceToward = line.project(towardPoint)
if distanceToward < distance:
delta *= -1.0
deltaPoint = line.interpolate(distance+delta)
return computeHeading((point.x, point.y), (deltaPoint.x, deltaPoint.y))
def computeNearestObject(obj, otherObjs):
tempDistance = -1
shortestDistance = -1
nearestObject = -1
if isinstance(obj, SpatialObject):
geometry = obj.geometry
else:
geometry = obj
for otherObj in otherObjs:
if isinstance(otherObj, SpatialObject):
otherGeometry = otherObj.geometry
else:
otherGeometry = otherObj
tempDistance = geometry.distance(otherGeometry)
if shortestDistance == -1:
shortestDistance = tempDistance
nearestObject = otherObj
else:
if tempDistance < shortestDistance:
shortestDistance = tempDistance
nearestObject = otherObj
return (nearestObject, shortestDistance)
def computeJunctionSigns(coord, ways, parentAerodrome):
signs = []
junctions = []
wayWidths = []
wayGeoms = []
# Make lists of all the taxiway widths and centerline geometries so we can
# find the maximum width and combine all the geometries into a temporary
# union of all the taxiways/runways that join at this junction.
for way in ways:
wayWidths.append(way.width)
wayGeoms.append(way.geometry)
geometryUnion = cascaded_union(wayGeoms)
# Compute the intersection points of the geometries, first at a large ring
# out to where the signs will be placed, then again at a 1 meter ring right
# around the junction node to see what direction each way enters the
# junction from.
setbackDistance = metersToDeg(max(wayWidths) + 5.0)
setbackRing = Point(coord).buffer(setbackDistance).exterior
setbackPoints = setbackRing.intersection(geometryUnion)
junctionDistance = metersToDeg(1.0)
junctionRing = Point(coord).buffer(junctionDistance).exterior
junctionPoints = junctionRing.intersection(geometryUnion)
# If at the setback distance there is only a single point rather than a
# multipoint then at most one taxiway is long enough to even reach the
# setback ring, so we just return no signs since they are not needed
# anyway.
if not isinstance(setbackPoints, MultiPoint):
return signs
for setbackPoint in setbackPoints:
closestWay, distance = computeNearestObject(setbackPoint, ways)
wayDistanceSetback = closestWay.geometry.project(setbackPoint)
wayDistanceJunction = closestWay.geometry.project(Point(coord))
if wayDistanceSetback > wayDistanceJunction:
junctionPoint = closestWay.geometry.interpolate(wayDistanceJunction + metersToDeg(1.0))
else:
junctionPoint = closestWay.geometry.interpolate(wayDistanceJunction - metersToDeg(1.0))
#junctionPoint, distance = computeNearestObject(closestWay, junctionPoints)
setbackHeading = computeHeadingAtPoint(closestWay.geometry, setbackPoint, junctionPoint)
junctionHeading = computeHeadingAtPoint(closestWay.geometry, junctionPoint, Point(coord))
junctions.append((closestWay, setbackPoint, setbackHeading, junctionHeading))
for (way1, setbackPoint1, setbackHeading1, junctionHeading1) in junctions:
signLoc = travel(setbackPoint1.coords[0], setbackHeading1-90, metersToDeg(way1.width/2.0 + 2.5))
# If the location of this sign would place it on an apron then just skip it.
# TODO: Could try to project to the nearest edge of the apron and maybe place the sign there instead.
nearestApron, distance = computeNearestObject(Point(signLoc), parentAerodrome.listObjectsByType(Apron))
if distance < 1E-14:
continue
subsignParts = []
# Determine the text to place on the sign.
for (way2, setbackPoint2, setbackHeading2, junctionHeading2) in junctions:
if setbackPoint1.distance(setbackPoint2) > metersToDeg(1.0):
headingString = ''
deltaHeading, direction = computeHeadingDelta(junctionHeading1, junctionHeading2+180)
directionLetter = direction[0]
if deltaHeading <= 22.5:
headingString = '{^u}'
elif deltaHeading <= 67.5:
headingString = '{^' + directionLetter + 'u}'
elif deltaHeading <= 112.5:
headingString = '{^' + directionLetter + '}'
elif deltaHeading <= 157.5:
headingString = '{^' + directionLetter + 'd}'
else:
headingString = '{^d}'
# Print leftward arrows to the left of the name and rightward arrows on the right.
if direction == 'left':
deltaHeading *= -1.0
text = headingString + way2.name
else:
text = way2.name + headingString
subsignParts.append((deltaHeading, text, way2))
text = ''
lastTypeString = '-1'
typeString = ''
subsignParts.sort()
for (deltaHeading, partText, way), isLast in lookahead(subsignParts):
if isinstance(way, Taxiway):
newTypeString = '{@Y}'
elif isinstance(way, Runway):
newTypeString = '{@R}'
if newTypeString != lastTypeString:
typeString = newTypeString
lastTypeString = typeString
# When we switch sign types we don't need the | symbol or we
# get an extra blank sign in between. Check to see if the last
# character in the text so far is a | and if so, remove it.
if len(text) > 0 and text[-1] == '|':
text = text[0:-1]
else:
typeString = ''
text += typeString + partText
if not isLast:
text += '|'
signs.append(Sign(signLoc, normalizeHeading(setbackHeading1), 2, text))
return signs
def travel(startPos, heading, distance):
heading = normalizeHeading(heading)
x = startPos[0]
y = startPos[1]
angle = headingToDeg(heading) * math.pi / 180.0
x += distance * math.cos(angle)
y += distance * math.sin(angle)
return (x, y)
def addOverpassQuery(type, id):
if type == 'node':
overpassQueryFile.write(' node({0});\n'.format(id))
elif type == 'way':
overpassQueryFile.write(' way({0}); >;\n'.format(id))
elif type == 'relation':
overpassQueryFile.write(' relation({0}); >;\n'.format(id))
def checkNodesFormClosedWay(nodes):
if nodes[0] != nodes[-1]:
print 'WARNING: Way not closed. First node: %s Last node: %s' % (nodes[0], nodes[-1])
return False
else:
return True
def nodesToCoords(nodes, coordDict):
coords = []
for n in nodes:
coords.append((coordDict[n][0], coordDict[n][1], n))
return coords
def printLine(line, lineType, lineName):
ret = ''
if isinstance(line, MultiLineString) or isinstance(line, GeometryCollection):
for c in line.geoms:
ret += printLine(c, lineType, lineName)
return ret
if isinstance(line, LineString):
ret += printLineSegment(line, lineType, lineName)
else:
print 'ERROR: Should never get here. (printLine())'
return ret
def printLineSegment(line, lineType, lineName):
ret = '120 {0}\n'.format(lineName)
for coord, isLast in lookahead(line.coords):
if not isLast:
ret += '111 {0} {1} {2}\n'.format(coord[1], coord[0], lineType)
else:
ret += '115 {0} {1}\n'.format(coord[1], coord[0])
return ret
def printArea(area, areaType, surface, smoothness, heading, name):
ret = ''
# TODO: Fix this to actually handle these if necessary, for now any time
# this gets passed a GeometryCollection the thing is empty anyway so
# nothing is lost by just exiting as we do now.
if isinstance(area, GeometryCollection) or isinstance(area, MultiPolygon):
for obj in area.geoms:
ret += printArea(obj, areaType, surface, smoothness, heading, name)
return ret
if isinstance(area, Polygon):
ret += printPolygon(area, areaType, surface, smoothness, heading, name)
return ret
def printPolygon(area, areaType, surface, smoothness, heading, name):
ret = '{0} {1} {2} {3} {4}\n'.format(areaType, surface, smoothness, heading, name)
if area.exterior.is_ccw:
coords = area.exterior.coords
else:
coords = reversed(area.exterior.coords)
for coord, isLast in lookahead(coords):
if not isLast:
ret += '111 {0} {1}\n'.format(coord[1], coord[0])
else:
ret += '113 {0} {1}\n'.format(coord[1], coord[0])
for ring in area.interiors:
if ring.is_ccw:
coords = reversed(ring.coords)
else:
coords = ring.coords
for coord, isLast in lookahead(coords):
if not isLast:
ret += '111 {0} {1}\n'.format(coord[1], coord[0])
else:
ret += '113 {0} {1}\n'.format(coord[1], coord[0])
return ret
def cut(line, distance):
# Cuts a line in two at a distance from its starting point
if distance <= 0.0 or distance >= line.length:
return [LineString(line)]
coords = list(line.coords)
for i, p in enumerate(coords):
pd = line.project(Point(p))
if pd == distance:
return [
LineString(coords[:i+1]),
LineString(coords[i:])]
if pd > distance:
cp = line.interpolate(distance)
return [
LineString(coords[:i] + [(cp.x, cp.y)]),
LineString([(cp.x, cp.y)] + coords[i:])]
def isAbandoned(tags):
if 'abandoned' in tags:
if tags['abandoned'] == 'yes':
return True
return False
def convertToUnit(string, unit):
conversionFactors = {
'ft' : { 'ft' : 1.0,
'm' : 3.28084,
'' : 3.28084},
'm' : { 'm' : 1.0,
'ft' : 0.3048,
'' : 1.0}
}
originalString = string
string = string.strip()
stringParts = string.split(None, 1)
length = len(stringParts)
if length == 0:
print 'ERROR: Parsing units on string "%s" failed, returning 0.' % originalString
return 0
elif length == 1:
stringParts.append('')
stringNumber = float(stringParts[0])
stringUnit = stringParts[1]
if unit in conversionFactors:
conversionRatioDict = conversionFactors[unit]
else:
print 'ERROR: Resultant unit of "%s" not found in conversion factors, returning 0.' % unit
return 0
if stringUnit in conversionRatioDict:
conversionRatio = float(conversionRatioDict[stringUnit])
else:
print 'ERROR: Source unit of "%s" not found in conversion factors, returning 0.' % stringUnit
print 'Acceptable source units and corresponding conversion ratios to target unit "%s" are:' % unit
print conversionRatioDict
return 0
return float(stringNumber * conversionRatio)
def surfaceStringToInt(st):
surfaceTypeDict = {'asphalt' : 1,
'paved' : 1,
'concrete' : 2,
'turf' : 3,
'grass' : 3,
'unpaved' : 3,
'dirt' : 4,
'gravel' : 5,
'dry_lakebed' : 12,
'water' : 13,
'snow' : 14,
'transparent' : 15}
if st in surfaceTypeDict:
return surfaceTypeDict[st]
else:
return surfaceTypeDict['unpaved']
# Takes a tags dictionary and a list of keys and returns the value of the first
# key in the dict that matches one from the list. If no key is found in the
# list of keys given, then the value passed to 'default' is returned instead.
def coalesceValue(keys, tags, default):
for key in keys:
if key in tags:
return tags[key]
return default
class SpatialObject(object):
def __init__(self):
self.geometry = null
def buildGeometry(self, coordDict, nodeDict):
return
class AerodromeObject(SpatialObject):
def __init__(self):
self.parentAerodrome = null
class Aerodrome(SpatialObject):
def __init__(self, name, code, ele, nodes):
self.name = name
self.code = code
self.ele = convertToUnit(ele, 'ft')
self.nodes = nodes
self.assosciatedObjects = []
# TODO: set viewpoint location, row code 14, one per aerodrome.
def buildGeometry(self, coordDict, nodeDict):
if checkNodesFormClosedWay(self.nodes):
self.geometry = Polygon(nodesToCoords(self.nodes, coordDict))
else:
print 'Not building geometry for aerodrome since it is not closed.'
def listObjectsByType(self, objType):
ls = []
for obj in self.assosciatedObjects:
if isinstance(obj, objType):
ls.append(obj)
return ls
def cleanGeometries(self):
# Create an apron trim area which is the combined surfaces of all the aprons.
apronGeoms = []
for apron in self.listObjectsByType(Apron):
apronGeoms.append(apron.geometry)
apronUnion = cascaded_union(apronGeoms)
taxiwaySurfaceObjects = {}
for taxiway in self.listObjectsByType(Taxiway):
for runway in self.listObjectsByType(Runway):
# Cut away the parts of the taxiways surface as well as just the taxiway edge lines that are on runways.
taxiway.leftEdgeLine = taxiway.leftEdgeLine.difference(runway.geometryPolygon)
taxiway.rightEdgeLine = taxiway.rightEdgeLine.difference(runway.geometryPolygon)
#taxiway.concreteGeometry = taxiway.concreteGeometry.difference(runway.geometryPolygon)
# Combine all of the taxiways of the same surface type together
# into a list for each type to be unioned together in the loop below.
if taxiway.surfaceInteger in taxiwaySurfaceObjects:
taxiwaySurfaceObjects[taxiway.surfaceInteger].append(taxiway.concreteGeometry)
else:
taxiwaySurfaceObjects[taxiway.surfaceInteger] = [taxiway.concreteGeometry]
# Loop over each pair of taxiways and use a buffer along the center
# of each taxiway to trim out the shoulder markings on the other
# one; this cleans up the intersections.
for taxiway2 in self.listObjectsByType(Taxiway):
if taxiway2 is not taxiway:
center = taxiway.geometry.buffer(metersToDeg(taxiway.width / 2.0 - shoulderWidth))
taxiway2.leftEdgeLine = taxiway2.leftEdgeLine.difference(center)
taxiway2.rightEdgeLine = taxiway2.rightEdgeLine.difference(center)
taxiway2.leftDotted = taxiway2.leftDotted.difference(center)
taxiway2.rightDotted = taxiway2.rightDotted.difference(center)
# Now that we have a list of all taxiways of for each surface type,
# combine each list into a single large object.
self.taxiwaySurfaces = {}
for surfaceInteger, taxiwayList in taxiwaySurfaceObjects.items():
self.taxiwaySurfaces[surfaceInteger] = cascaded_union(taxiwayList)
# Now that the taxiways have been combined as such, loop over and
# remove small islands in the concrete surfaces (generally these
# are small triangular regions formed between a 'Y' connection
# which connects to another taxiway across the top of the 'Y').
newSurfaces = {}
minArea = math.pow(metersToDeg(30.0), 2)
deletedRings = []
for surfaceType, surface in self.taxiwaySurfaces.items():
newPolygons = []
if isinstance(surface, MultiPolygon):
for geom in surface.geoms:
newRingList = []
for ring in geom.interiors:
if Polygon(ring).area >= minArea:
newRingList.append(ring)
else:
deletedRings.append(Polygon(ring))
newPolygons.append(Polygon(geom.exterior, newRingList))
newSurfaces[surfaceType] = MultiPolygon(newPolygons).difference(apronUnion)
else:
newSurfaces[surfaceType] = surface.difference(apronUnion)
self.taxiwaySurfaces = newSurfaces
deletedRingsUnion = cascaded_union(deletedRings)
deletedRingsBuffer = deletedRingsUnion.buffer(metersToDeg(shoulderWidth + deltaM))
apronAndRingsUnion = apronUnion.union(deletedRingsBuffer)
# Strip away taxiway edge lines on aprons and strip away dotted taxiway
# edge lines that are not on aprons.
for taxiway in self.listObjectsByType(Taxiway):
taxiway.leftDotted = apronAndRingsUnion.intersection(taxiway.leftDotted)
taxiway.rightDotted = apronAndRingsUnion.intersection(taxiway.rightDotted)
taxiway.leftEdgeLine = taxiway.leftEdgeLine.difference(apronAndRingsUnion)
taxiway.rightEdgeLine = taxiway.rightEdgeLine.difference(apronAndRingsUnion)
def toString(self):
# Print out the main airport header line
tempString = '1 {0} 0 0 {1} {2}\n'.format(self.ele, self.code, self.name)
# Print out the boundary area of the aerodrome.
tempString += printArea(self.geometry, 130, '', '', '', self.name)
# Print out all of the assosciated objects for this airport to apt.dat.
taxiwayCoords = {}
for obj in self.assosciatedObjects:
tempString += obj.toString()
# Print out the combined taxiway surfaces.
for surf, area in self.taxiwaySurfaces.items():
area = area.buffer(0)
area = area.simplify(metersToDeg(0.5))
tempString += printArea(area, 110, surf, 0.15, 360, 'taxiway surface ' + str(surf))
# If the airport has associated taxiways, also print out a taxiway network.
taxiways = self.listObjectsByType(Taxiway)
runways = self.listObjectsByType(Runway)
taxiwaysAndRunways = taxiways[:] + runways[:]
if len(taxiwaysAndRunways) > 0:
# Start by building a set of the nodes used for taxiways at this specific airport.
for taxiway in taxiwaysAndRunways:
for coord in taxiway.coords:
if coord in taxiwayCoords:
taxiwayCoords[coord].append(taxiway)
else:
taxiwayCoords[coord] = [taxiway]
# Now print out the lines for all of the taxiway/runway nodes for this airport.
tempString += '1200\n'
for coord in taxiwayCoords:
tempString += '1201 {0} {1} both {2}\n'.format(coord[1], coord[0], coord[2])
# Finally, print out the actual edges between the nodes.
for way in taxiwaysAndRunways:
prevCoord = 0
for coord in way.coords:
if prevCoord == 0:
prevCoord = coord
else:
# TODO: Implement oneway taxiways.
if isinstance(way, Runway):
typeString = 'runway'
else:
typeString = 'taxiway'
tempString += '1202 {0} {1} twoway {2} {3}\n'.format(prevCoord[2], coord[2], typeString, way.name)
if typeString == 'runway':
tempString += '1204 arrival {0}\n'.format(way.runwayEndNames[0] + ',' + way.runwayEndNames[1])
prevCoord = coord
# Place signs at the taxiway intersections
for coord, ways in taxiwayCoords.items():
if len(ways) >= 2:
signs = computeJunctionSigns(coord, ways, self)
for sign in signs:
tempString += sign.toString()
return tempString
class Runway(AerodromeObject):
def __init__(self, name, runwayEndNames, runwayEndNodes, surface, width, nodes):
self.name = name
self.runwayEndNames = runwayEndNames
self.runwayEndNodes = runwayEndNodes
self.surface = surface
self.surfaceInteger = surfaceStringToInt(self.surface)
self.width = convertToUnit(width, 'm')
self.nodes = nodes
def buildGeometry(self, coordDict, nodeDict):
self.geometry = LineString(nodesToCoords(self.nodes, coordDict))
self.coords = nodesToCoords(self.nodes, coordDict)
# Check to see if the first end of the runway is drawn with the lower
# numbered end first or if it needs to be reversed so that the
# direction of the line lines up with the name. Note that this assumes
# the ref tag gives the lower number first, like 18/36.
firstEnd = headingToRunwayInt(computeHeading(self.geometry.coords[0], self.geometry.coords[-1]))
if firstEnd > 18:
self.geometry.coords = list(self.geometry.coords)[::-1]
self.runwayEndNodes = list(self.runwayEndNodes)[::-1]
self.heading = computeHeading(self.geometry.coords[0], self.geometry.coords[-1])
self.geometryPolygon = self.geometry.buffer(metersToDeg(self.width/2.0), 1)
def toString(self):
return '100 {0} {1} 0 0.15 0 0 1 {2} {3} {4} 0 0 1 0 0 0 {5} {6} {7} 0 0 1 0 0 0\n'.format(self.width, self.surfaceInteger, self.runwayEndNames[0], self.geometry.coords[0][1], self.geometry.coords[0][0], self.runwayEndNames[1], self.geometry.coords[-1][1], self.geometry.coords[-1][0])
class Taxiway(AerodromeObject):
def __init__(self, name, surface, width, nodes):
self.name = name
self.surface=surface
self.surfaceInteger = surfaceStringToInt(self.surface)
self.width = convertToUnit(width, 'm')
self.nodes = nodes
self.holdingPositions = []
def buildGeometry(self, coordDict, nodeDict):
self.coords = nodesToCoords(self.nodes, coordDict)
self.geometry = LineString(self.coords)
self.leftEdgeLine = self.geometry.parallel_offset(metersToDeg(self.width / 2.0 - shoulderWidth), 'left', 2)
self.rightEdgeLine = self.geometry.parallel_offset(metersToDeg(self.width / 2.0 - shoulderWidth), 'right', 2)
self.leftDotted = copy.deepcopy(self.leftEdgeLine)
self.rightDotted = copy.deepcopy(self.rightEdgeLine)
self.concreteGeometry = orient(self.geometry.buffer(metersToDeg(self.width)/2.0, 2))
# Loop over all the nodes that make up the taxiway and make a list of
# any that are holding positions.
# TODO: Currently only checks normal holds, should check for ILS holds as well.
for node in self.nodes:
if node in nodeDict:
nodeProps = nodeDict[node]
if 'aeroway' in nodeProps[0]:
if nodeProps[0]['aeroway'] == 'holding_position':
self.holdingPositions.append((node, nodeProps[0], nodeProps[1]))
def toString(self):
ret = ''
# Print out holding position lines on top of the concrete (these lines
# are purely visual, X-plane does not respect these in taxi routing)
self.signs = []
for node, tags, coord in self.holdingPositions:
headingAndPosition = computeSegmentHeading(node, self.nodes, self.coords)
hdg = headingAndPosition[0]
pos = headingAndPosition[1]
# TODO: Need to properly set runway.
ret += Sign(travel(pos, hdg+90, metersToDeg(self.width/2.0 + 2.5)), hdg, 3, '{@R}18-36').toString()
ret += Sign(travel(pos, hdg-90, metersToDeg(self.width/2.0 + 2.5)), hdg, 3, '{@R}18-36').toString()
# TODO: The dashed yellow lines should be on the left side of this line, which should be closer to the runway. Currently they are drawn arbitrarily on one side, might need to reverse start and end if they are on the wrong side for a particular runway.
lineStart = travel(pos, hdg-90, metersToDeg(self.width/2.0 - shoulderWidth))
lineEnd = travel(pos, hdg+90, metersToDeg(self.width/2.0 - shoulderWidth))
ret += printLine(LineString((lineStart, lineEnd)), 4, 'hold line {0}\n'.format(self.name))
# Print out taxiway centerlines and shoulder lines on top of the concrete.
ret += printLine(self.geometry, 1, 'taxiway {0} centerline'.format(self.name))
ret += printLine(self.leftEdgeLine, 3, 'taxiway {0} left line'.format(self.name))
ret += printLine(self.rightEdgeLine, 3, 'taxiway {0} right line'.format(self.name))
ret += printLine(self.leftDotted, 2, 'taxiway {0} dotted line'.format(self.name))
ret += printLine(self.rightDotted, 2, 'taxiway {0} dotted line'.format(self.name))
return ret
class Windsock(AerodromeObject):
def __init__(self, coord, lit):
self.coord = coord
if lit in ('yes', 'true', '1'):
self.lit = 1
else:
self.lit = 0
def buildGeometry(self, coordDict, nodeDict):
self.geometry = Point(self.coord)
def toString(self):
return '19 {0} {1} {2} WS\n'.format(self.coord[1], self.coord[0], self.lit)
class Apron(AerodromeObject):
def __init__(self, name, nodes, surface):
self.name = name
self.nodes = nodes
self.surface = surface
self.surfaceInteger = surfaceStringToInt(self.surface)
def buildGeometry(self, coordDict, nodeDict):
if checkNodesFormClosedWay(self.nodes):
# TODO: Need to make sure the coords in the resulting geometry for a counter clockwise ring.
self.geometry = orient(Polygon(nodesToCoords(self.nodes, coordDict)))
else:
print 'Not building geometry for apron since it is not closed.'
def toString(self):
ret = printArea(self.geometry, 110, self.surfaceInteger, 0.15, 360, self.name)
# If this apron is named, also create a start location for it.
# TODO: If an airport has no named aprons we should choose the largest one and put a start location there. An alternative is for the aerodrome class to make sure that it has at least one named apron by naming the largest on 'Ramp' or something if none are named.
if len(self.name) > 0:
startupLoc = self.geometry.representative_point()
# TODO: Set the startup heading to be something reasonable, toward the nearest taxiway would be a good one.
ret += '15 {0} {1} 360 {2}\n'.format(startupLoc.y, startupLoc.x, self.name)
return ret
class Beacon(AerodromeObject):
def __init__(self, coord, color):
self.coord = coord
self.color = color
def buildGeometry(self, coordDict, nodeDict):
self.geometry = Point(self.coord)
def toString(self):
return '18 {0} {1} {2} BCN\n'.format(self.coord[1], self.coord[0], self.color)
class ParkingPosition(SpatialObject):
def __init__(self, coord, positionTypeTag, width, heading):
self.coord = coord
self.positionTypeTag = positionTypeTag
self.typeString = 'tie-down'
self.aircraftTypeString = 'props'
self.width = width
self.heading = float(heading)
if self.positionTypeTag == 'parking_position':
self.typeString = 'tie_down'
self.aircraftTypeString = 'props'
elif self.positionTypeTag == 'hangar':
self.typeString = 'hangar'
self.aircraftTypeString = 'all'
elif self.positionTypeTag == 'gate':
self.typeString = 'gate'
self.aircraftTypeString = 'jets|heavy'
else:
print 'ERROR: Parking position type "%s" unknown, may be exported incorrectly.'
def buildGeometry(self, coordDict, nodeDict):
self.geometry = Point(self.coord)
self.leftWingTip = travel(self.coord, self.heading-90, metersToDeg(self.width/2.0))
self.rightWingTip = travel(self.coord, self.heading+90, metersToDeg(self.width/2.0))
self.tail = travel(self.coord, self.heading+180, metersToDeg(self.width/2.0))
self.wingLine = LineString((self.leftWingTip, self.rightWingTip))
self.tailLine = LineString((self.coord, self.tail))
def toString(self):
ret = '1300 {0} {1} {2} {3} {4}\n'.format(self.coord[1], self.coord[0], round(self.heading), self.typeString, self.aircraftTypeString)
ret += printLine(self.wingLine, 1, self.typeString)
ret += printLine(self.tailLine, 1, self.typeString)
return ret
class LightedObject(AerodromeObject):
def __init__(self, coord, typeName):
self.coord = coord
self.typeName = typeName
self.heading = 360
# TODO: Need to figure out tag for glideslope and read it if present.
self.glideslope = 3.0
self.runway = '01'
def buildGeometry(self, coordDict, nodeDict):
self.geometry = Point(self.coord)
def toString(self):
# Determine the nearest runway to this object.
nearestRunway, shortestDistance = computeNearestObject(self.geometry, self.parentAerodrome.listObjectsByType(Runway))
turn = -1
if nearestRunway != -1:
# Find out what distance along the runway this object is at.
distance = nearestRunway.geometry.project(self.geometry)
runwayFraction = distance / nearestRunway.geometry.length
runwayLoc = nearestRunway.geometry.interpolate(distance)
# Determine the true heading of the runway and also work out which
# end of the runway the light is on.
if runwayFraction < 0.5:
self.heading = computeHeading(nearestRunway.geometry.coords[0], nearestRunway.geometry.coords[-1])
self.runway = nearestRunway.runwayEndNames[0]
else:
self.heading = computeHeading(nearestRunway.geometry.coords[-1], nearestRunway.geometry.coords[0])
self.runway = nearestRunway.runwayEndNames[1]
self.heading = int(round(self.heading))
if self.heading == 0:
self.heading = 360
# If we are moving down the runway and are at the location with the
# light directly beside us, compute which way (left or right) we
# would need to turn to face the light, i.e. determine which side
# of the runway it is on.
turn = computeTurnTo(runwayLoc.coords[:][0], self.heading, self.geometry.coords[:][0])
# Determine the appropriate code to use for X-plane to indicate the type of the light.
if self.typeName == 'vasi':
self.typeCode = 1
elif self.typeName == 'papi':
if turn != -1 and turn[1] == 'left':
self.typeCode = 2
else:
self.typeCode = 3
else:
print 'ERROR: Lighted object of type "%s" is unknown, skipping output of this object.' % self.typeName
return '21 {0} {1} {2} {3} {4} {5} {6}\n'.format(self.coord[1], self.coord[0], self.typeCode, round(self.heading), self.glideslope, self.runway, self.typeName)
class Sign(AerodromeObject):
def __init__(self, coord, heading, size, text):
self.coord = coord
self.heading = heading
self.size = size
self.text = text
def buildGeometry(self):
self.geometry = Point(self.coord)
def toString(self):
return '20 {0} {1} {2} 0 {3} {4}\n'.format(self.coord[1], self.coord[0], round(self.heading), self.size, self.text)
class Osm2apt_class(object):
aerodromes = []
# Every one of the lists in this next section should be added to
# objectLists like 'runways' is.
objectLists = []
runways = []; objectLists.append(runways)
taxiways = []; objectLists.append(taxiways)
windsocks = []; objectLists.append(windsocks)
aprons = []; objectLists.append(aprons)
beacons = []; objectLists.append(beacons)
lightedObjects = []; objectLists.append(lightedObjects)
parkingPositions = []; objectLists.append(parkingPositions)
coords = []
coordDict = {}
nodes = []
nodeDict = {}
# Callback method to simply read in all node ID's and coordinates
def coordsCallback(self, coords):
for c in coords:
self.coords.append(c)
# Callback method to read all nodes which have tags on them
def nodesCallback(self, nodes):
for osmid, tags, coord in nodes:
self.nodes.append((osmid, tags, coord))
if 'aeroway' in tags:
#node: aeroway=windsock
if tags['aeroway'] == 'windsock':
lit = coalesceValue(('lit'), tags, 'no')
self.windsocks.append(Windsock(coord, lit))
#node: aeroway=vasi|papi
if tags['aeroway'] == 'vasi' or tags['aeroway'] == 'papi':
self.lightedObjects.append(LightedObject(coord, tags['aeroway']))
#node: aeroway=parking_position
if tags['aeroway'] == 'parking_position' \
or tags['aeroway'] == 'hangar' \
or tags['aeroway'] == 'gate':
width = convertToUnit(coalesceValue(('width'), tags, '10 m'), 'm')
heading = coalesceValue(('heading', 'direction'), tags, 360)
self.parkingPositions.append(ParkingPosition(coord, tags['aeroway'], width, heading))
if 'man_made' in tags:
#node: man_made=beacon
if tags['man_made'] == 'beacon':
# TODO: Need to work out the tagging (if it even exists) for the beacon color type.
color = 1
self.beacons.append(Beacon(coord, color))
# Callback method to process ways
def waysCallback(self, ways):
for osmid, tags, refs in ways:
if 'aeroway' in tags:
# way: aeroway=aerodrome
if tags['aeroway'] == 'aerodrome':
# Get the airport name, if no name is listed use the
# default name listed below, no need to skip it in this case.
if 'name' in tags:
aerodromeName = tags['name']
else:
aerodromeName = 'Unnamed Airport'
# Get the ICAO code first, if that does not exist fall back
# on the FAA code, if neither exist we cannot import the
# airport so just skip it.
aerodromeCode = coalesceValue(('icao', 'faa', 'ref'), tags, '')
if aerodromeCode == '':
print '\nFound an aerodrome. Tags:\n', tags
print 'ERROR: Aerodrome does not have an ICAO or FAA code, skipping. Way ID: ', osmid
addOverpassQuery('way', osmid)
continue
# Get the airport elevation, if there is no 'ele' tag skip
# the airport.
if 'ele' in tags:
aerodromeELE = tags['ele']
else:
print '\nFound an aerodrome. Tags:\n', tags
print 'ERROR: Aerodrome does not have an elevation (ele), skipping. Way ID: ', osmid
addOverpassQuery('way', osmid)
continue
# We have successfully read all the data for this aerodrome
# so add it to the list of completed aerodromes to be put
# in the output file.
self.aerodromes.append(Aerodrome(aerodromeName, aerodromeCode, aerodromeELE, refs))
# way: aeroway=runway
elif tags['aeroway'] == 'runway':
# Check to make sure the runway is not a closed loop, that
# is, make sure the first and last nodes are not the same node.
if (refs[0] != refs[-1]):
runwayEndNodes = [refs[0], refs[-1]]
else:
print '\nFound a runway. Tags:\n', tags
print 'ERROR: Runway is a closed loop, skipping. Way ID: ', osmid
addOverpassQuery('way', osmid)
continue
# Check to see if the runway is abandonded, if so skip it.
if isAbandoned(tags):
print '\nFound a runway. Tags:\n', tags
print 'NOTICE: Runway is abandoned, skipping. Way ID: ', osmid