Advertisement
UniQuet0p1

Untitled

Apr 27th, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. import cartopy.crs as ccrs
  2. import matplotlib.pyplot as plt
  3. import csv
  4. import cartopy
  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_path, airports_path) -> list:
  21.     with open(flights_path, "r", encoding="UTF-8") as csv_file, \
  22.             open(airports_path, "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("Vadim Solovjov")
  71.  
  72.     normal_flight = collect_flights_to_list("flt/otselennud.csv", "flt/airports.dat")
  73.     covid_flight = collect_flights_to_list("flt/flights21.csv", "flt/airports.dat")
  74.     TLL = normal_flight[0]
  75.     add_airport_name(TLL)
  76.  
  77.     for airport in normal_flight:
  78.         add_flight(TLL, airport, "green")
  79.         add_airport_name(airport)
  80.  
  81.     for airport in covid_flight:
  82.         add_flight(TLL, airport, "red")
  83.  
  84.     plt.show()
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement