Advertisement
UniQuet0p1

Untitled

Apr 26th, 2021
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. import cartopy
  2. import cartopy.crs as ccrs
  3. import matplotlib.pyplot as plt
  4. import csv
  5.  
  6.  
  7. class Flight:
  8. def __init__(self, name, city, country, IATA, latitude, longitude):
  9. self.longitude = longitude
  10. self.latitude = latitude
  11. self.IATA = IATA
  12. self.country = country
  13. self.city = city
  14. self.name = name
  15.  
  16. def __repr__(self):
  17. return f"Airport {self.name} in {self.city}, {self.country}, with IATA: {self.IATA}"
  18.  
  19.  
  20. def collect_flights_to_list(flights, airports) -> list:
  21. with open(flights, "r", encoding="UTF-8") as csv_file, \
  22. open(airports, "r", encoding="UTF-8") as dat_file:
  23. flights_reader = csv.reader(csv_file, delimiter=";")
  24. data_dict = {}
  25. for line in dat_file.readlines():
  26. line = line.split(",")
  27. name = line[1].replace('"', '')
  28. city = line[2].replace('"', '')
  29. country = line[3].replace('"', '')
  30. IATA = line[4].replace('"', '')
  31. latitude = line[6]
  32. longitude = line[7]
  33. data_dict[IATA] = Flight(name, city, country, IATA, latitude, longitude)
  34.  
  35. next(flights_reader)
  36.  
  37. airports_data = []
  38. for line in flights_reader:
  39. city, IATA = line
  40. airports_data.append(data_dict.get(IATA))
  41.  
  42. return airports_data
  43.  
  44.  
  45. def add_flight(start_airport: Flight, target_airport: Flight, color):
  46. start_longitude = float(start_airport.longitude)
  47. start_latitude = float(start_airport.latitude)
  48. end_longitude = float(target_airport.longitude)
  49. end_latitude = float(target_airport.latitude)
  50.  
  51. plt.plot([start_longitude, end_longitude], [start_latitude, end_latitude],
  52. color=color, linewidth=1.5, marker='o',
  53. transform=ccrs.Geodetic())
  54.  
  55.  
  56. def add_airport_name(airport: Flight):
  57. longitude = float(airport.longitude)
  58. latitude = float(airport.latitude)
  59. plt.text(longitude - 0.75, latitude + 0.25, airport.IATA,
  60. horizontalalignment='right',
  61. transform=ccrs.PlateCarree())
  62.  
  63.  
  64. if __name__ == '__main__':
  65. ax = plt.axes(projection=ccrs.PlateCarree())
  66. ax.add_feature(cartopy.feature.LAND)
  67. ax.add_feature(cartopy.feature.OCEAN)
  68. ax.add_feature(cartopy.feature.COASTLINE)
  69. ax.add_feature(cartopy.feature.BORDERS, linestyle='-', alpha=.5)
  70. plt.title("Dmitri Härm")
  71.  
  72. airports_fly = collect_flights_to_list("data/otselennud.csv", "data/airports.dat")
  73. airports_covid_time = collect_flights_to_list("data/flights21.csv", "data/airports.dat")
  74. tll = airports_fly[0]
  75. add_airport_name(tll)
  76.  
  77. for airport in airports_fly:
  78. add_flight(tll, airport, "blue")
  79. add_airport_name(airport)
  80.  
  81. for airport in airports_covid_time:
  82. add_flight(tll, airport, "red")
  83.  
  84. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement