map_lines.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. # -----------------------------------------------------------------------------------------
  12. # To execute this script, make sure that the taipy-gui package is installed in your
  13. # Python environment and run:
  14. # python <script>
  15. # -----------------------------------------------------------------------------------------
  16. from typing import Any, Dict, List
  17. from taipy.gui import Gui
  18. # Busiest US airports
  19. # Source: https://en.wikipedia.org/wiki/List_of_busiest_airports_by_passenger_traffic
  20. airports: Dict[str, Dict[str, float]] = {
  21. "AMS": {"lat": 52.31047296675518, "lon": 4.76819929439927},
  22. "ATL": {"lat": 33.64086185344307, "lon": -84.43600501711686},
  23. "AYT": {"lat": 36.90419539293911, "lon": 30.801855337974292},
  24. "BOS": {"lat": 42.36556559649881, "lon": -71.00960311751096},
  25. "CAN": {"lat": 23.38848323741897, "lon": 113.30277713668413},
  26. "CDG": {"lat": 49.008029034119915, "lon": 2.550879924581871},
  27. "CJU": {"lat": 33.51035978854847, "lon": 126.4913319405336},
  28. "CKG": {"lat": 29.71931573810283, "lon": 106.64211731662628},
  29. "CLT": {"lat": 35.214730980190616, "lon": -80.9474735034797},
  30. "CSX": {"lat": 28.196638298182446, "lon": 113.22083329905352},
  31. "CTU": {"lat": 30.567492917634063, "lon": 103.94912193845805},
  32. "CUN": {"lat": 21.04160313837335, "lon": -86.87407057500725},
  33. "DEL": {"lat": 28.556426221725868, "lon": 77.10031185913002},
  34. "DEN": {"lat": 39.85589532386815, "lon": -104.67329901305273},
  35. "DFW": {"lat": 32.89998507111719, "lon": -97.04044513206443},
  36. "DME": {"lat": 55.41032513421412, "lon": 37.902386927376234},
  37. "DTW": {"lat": 42.216145762248594, "lon": -83.35541784824225},
  38. "DXB": {"lat": 25.253155060720765, "lon": 55.365672799304534},
  39. "EWR": {"lat": 40.68951508829295, "lon": -74.17446240095387},
  40. "FLL": {"lat": 26.072469069499288, "lon": -80.1502073285754},
  41. "FRA": {"lat": 50.037870541116, "lon": 8.562119610188235},
  42. "GMP": {"lat": 37.558628944763534, "lon": 126.79445244110332},
  43. "GRU": {"lat": -23.430691200492866, "lon": -46.473107371367846},
  44. "HGH": {"lat": 30.2359856421667, "lon": 120.43880486944619},
  45. "HND": {"lat": 35.54938443207139, "lon": 139.77979568388005},
  46. "IAH": {"lat": 29.98997826322153, "lon": -95.33684707873988},
  47. "IST": {"lat": 41.27696594578831, "lon": 28.73004303446375},
  48. "JFK": {"lat": 40.64129497654287, "lon": -73.77813830094803},
  49. "KMG": {"lat": 24.99723271310971, "lon": 102.74030761670535},
  50. "LAS": {"lat": 36.08256046166282, "lon": -115.15700045025673},
  51. "LAX": {"lat": 33.94157995977848, "lon": -118.40848708486908},
  52. "MAD": {"lat": 40.49832400063489, "lon": -3.5676196584173754},
  53. "MCO": {"lat": 28.419119921670067, "lon": -81.30451008534465},
  54. "MEX": {"lat": 19.436096410278736, "lon": -99.07204777544095},
  55. "MIA": {"lat": 25.795823878101675, "lon": -80.28701871639629},
  56. "MSP": {"lat": 44.88471735079015, "lon": -93.22233824616785},
  57. "ORD": {"lat": 41.98024003208415, "lon": -87.9089657513565},
  58. "PEK": {"lat": 40.079816213451416, "lon": 116.60309064055198},
  59. "PHX": {"lat": 33.43614430802288, "lon": -112.01128270596944},
  60. "PKX": {"lat": 39.50978840400886, "lon": 116.41050689906415},
  61. "PVG": {"lat": 31.144398958515847, "lon": 121.80823008537978},
  62. "SAW": {"lat": 40.9053709590178, "lon": 29.316838841845318},
  63. "SEA": {"lat": 47.448024349661814, "lon": -122.30897973141963},
  64. "SFO": {"lat": 37.62122788155908, "lon": -122.37901977603573},
  65. "SHA": {"lat": 31.192227319334787, "lon": 121.33425408454256},
  66. "SLC": {"lat": 40.78985913031307, "lon": -111.97911351851535},
  67. "SVO": {"lat": 55.97381026156798, "lon": 37.412288430689664},
  68. "SZX": {"lat": 22.636827890877626, "lon": 113.81454162446936},
  69. "WUH": {"lat": 30.776589409566686, "lon": 114.21244949898504},
  70. "XIY": {"lat": 34.437119809208546, "lon": 108.7573508575816},
  71. }
  72. # Inter US airports flights
  73. # Source: https://www.faa.gov/air_traffic/by_the_numbers
  74. flights: List[Dict[str, Any]] = [
  75. {"from": "ATL", "to": "DFW", "traffic": 580},
  76. {"from": "ATL", "to": "MIA", "traffic": 224},
  77. {"from": "BOS", "to": "LAX", "traffic": 168},
  78. {"from": "DEN", "to": "DFW", "traffic": 558},
  79. {"from": "DFW", "to": "BOS", "traffic": 422},
  80. {"from": "DFW", "to": "CLT", "traffic": 360},
  81. {"from": "DFW", "to": "JFK", "traffic": 56},
  82. {"from": "DFW", "to": "LAS", "traffic": 569},
  83. {"from": "DFW", "to": "SEA", "traffic": 392},
  84. {"from": "DTW", "to": "DFW", "traffic": 260},
  85. {"from": "EWR", "to": "DFW", "traffic": 310},
  86. {"from": "EWR", "to": "ORD", "traffic": 168},
  87. {"from": "FLL", "to": "DFW", "traffic": 336},
  88. {"from": "FLL", "to": "ORD", "traffic": 168},
  89. {"from": "IAH", "to": "DFW", "traffic": 324},
  90. {"from": "JFK", "to": "FLL", "traffic": 112},
  91. {"from": "JFK", "to": "LAS", "traffic": 112},
  92. {"from": "JFK", "to": "LAX", "traffic": 548},
  93. {"from": "JFK", "to": "ORD", "traffic": 56},
  94. {"from": "LAS", "to": "MIA", "traffic": 168},
  95. {"from": "LAX", "to": "DFW", "traffic": 914},
  96. {"from": "LAX", "to": "EWR", "traffic": 54},
  97. {"from": "LAX", "to": "LAS", "traffic": 222},
  98. {"from": "LAX", "to": "MCO", "traffic": 56},
  99. {"from": "LAX", "to": "MIA", "traffic": 392},
  100. {"from": "LAX", "to": "SFO", "traffic": 336},
  101. {"from": "MCO", "to": "DFW", "traffic": 500},
  102. {"from": "MCO", "to": "JFK", "traffic": 224},
  103. {"from": "MCO", "to": "ORD", "traffic": 224},
  104. {"from": "MIA", "to": "BOS", "traffic": 392},
  105. {"from": "MIA", "to": "DEN", "traffic": 112},
  106. {"from": "MIA", "to": "DFW", "traffic": 560},
  107. {"from": "MIA", "to": "DTW", "traffic": 112},
  108. {"from": "MIA", "to": "EWR", "traffic": 168},
  109. {"from": "MIA", "to": "IAH", "traffic": 168},
  110. {"from": "MIA", "to": "JFK", "traffic": 392},
  111. {"from": "MIA", "to": "MCO", "traffic": 448},
  112. {"from": "MSP", "to": "DFW", "traffic": 326},
  113. {"from": "MSP", "to": "MIA", "traffic": 56},
  114. {"from": "ORD", "to": "BOS", "traffic": 430},
  115. {"from": "ORD", "to": "DEN", "traffic": 112},
  116. {"from": "ORD", "to": "DFW", "traffic": 825},
  117. {"from": "ORD", "to": "LAS", "traffic": 280},
  118. {"from": "ORD", "to": "LAX", "traffic": 496},
  119. {"from": "ORD", "to": "MIA", "traffic": 505},
  120. {"from": "ORD", "to": "MSP", "traffic": 160},
  121. {"from": "ORD", "to": "PHX", "traffic": 280},
  122. {"from": "ORD", "to": "SEA", "traffic": 214},
  123. {"from": "ORD", "to": "SFO", "traffic": 326},
  124. {"from": "PHX", "to": "DFW", "traffic": 550},
  125. {"from": "PHX", "to": "MIA", "traffic": 56},
  126. {"from": "SEA", "to": "JFK", "traffic": 56},
  127. {"from": "SFO", "to": "DFW", "traffic": 526},
  128. {"from": "SFO", "to": "JFK", "traffic": 278},
  129. {"from": "SFO", "to": "MIA", "traffic": 168},
  130. {"from": "SLC", "to": "DFW", "traffic": 280},
  131. ]
  132. data = []
  133. max_traffic = 0
  134. for flight in flights:
  135. airport_from = airports[flight["from"]]
  136. airport_to = airports[flight["to"]]
  137. # Define data source to plot this flight
  138. data.append({"lat": [airport_from["lat"], airport_to["lat"]], "lon": [airport_from["lon"], airport_to["lon"]]})
  139. # Store the maximum traffic
  140. if flight["traffic"] > max_traffic:
  141. max_traffic = flight["traffic"]
  142. properties = {
  143. # Chart data
  144. "data": data,
  145. # Chart type
  146. "type": "scattergeo",
  147. # Keep lines only
  148. "mode": "lines",
  149. # Flights display as redish lines
  150. "line": {"width": 2, "color": "E22"},
  151. "layout": {
  152. # Focus on the USA region
  153. "geo": {"scope": "usa"}
  154. },
  155. }
  156. # Set the proper data source and opacity for each trace
  157. for i, flight in enumerate(flights):
  158. # lat[trace_index] = "[index_in_data]/lat"
  159. properties[f"lat[{i+1}]"] = f"{i}/lat"
  160. # lon[trace_index] = "[index_in_data]/lon"
  161. properties[f"lon[{i+1}]"] = f"{i}/lon"
  162. # Set flight opacity (max traffic -> max opacity)
  163. # Hide legend for all flights
  164. properties[f"options[{i+1}]"] = {"opacity": flight["traffic"] / max_traffic, "showlegend": False}
  165. page = """
  166. <|chart|properties={properties}|>
  167. """
  168. if __name__ == "__main__":
  169. Gui(page).run(title="Chart - Maps - Multiple Lines")