souvik0306 commited on
Commit
de2eb04
·
1 Parent(s): 7b17a2d

updated gradio

Browse files
Files changed (3) hide show
  1. gradio_ui.py +29 -14
  2. map_generator.py +67 -0
  3. route_map.html +177 -0
gradio_ui.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  import pandas as pd
 
3
  from flight_distance import *
4
  from optimize import *
5
  from weather import *
@@ -8,14 +9,13 @@ from weather import *
8
  airport_df = pd.read_csv(r'airport.csv') # Adjust the path to your CSV file
9
  aircraft_df = pd.read_csv(r'aircraft.csv') # Adjust the path to your CSV file
10
 
11
- # Create a combined option list with both IATA codes and airport names
12
  airport_options = [f"{row['IATA']} - {row['Airport_Name']}" for _, row in airport_df.iterrows()]
 
13
 
14
  # Ensure the correct column is used for aircraft types
15
  aircraft_type_column = 'Aircraft'
16
  aircraft_options = aircraft_df[aircraft_type_column].tolist()
17
 
18
- # Gradio function to determine if a route can be flown
19
  def check_route(airport_selections, aircraft_type):
20
  # Extract IATA codes from the selected options
21
  airports = [selection.split(" - ")[0] for selection in airport_selections]
@@ -42,9 +42,13 @@ def check_route(airport_selections, aircraft_type):
42
 
43
  # Check if aircraft details were retrieved successfully
44
  if isinstance(aircraft_specs, str):
45
- return {"Error": aircraft_specs} # Return error message if aircraft not found
46
 
47
- # Prepare sector-wise details for flight time, fuel required, and refueling need
 
 
 
 
48
  sector_details = []
49
  refuel_required = False # Flag to track if refueling is required
50
 
@@ -63,6 +67,7 @@ def check_route(airport_selections, aircraft_type):
63
  # Check if refueling is required for this sector
64
  if fuel > aircraft_specs['Max_Fuel_Capacity_kg']:
65
  sector_info["Refuel Required"] = "Yes"
 
66
  refuel_required = True
67
  else:
68
  sector_info["Refuel Required"] = "No"
@@ -83,13 +88,17 @@ def check_route(airport_selections, aircraft_type):
83
 
84
  if fuel > aircraft_specs['Max_Fuel_Capacity_kg']:
85
  final_leg_info["Refuel Required"] = "Yes"
 
86
  refuel_required = True
87
  else:
88
  final_leg_info["Refuel Required"] = "No"
89
 
90
  sector_details.append(final_leg_info)
91
 
92
- # Step 7: Prepare and return result
 
 
 
93
  if refuel_required:
94
  result = {
95
  "Optimal Route": " -> ".join(optimal_route) + f" -> {optimal_route[0]}",
@@ -105,20 +114,26 @@ def check_route(airport_selections, aircraft_type):
105
  "Sector Details": sector_details
106
  }
107
 
108
- return result
109
-
110
 
111
  # Gradio Interface
112
  with gr.Blocks() as demo:
113
  gr.Markdown("## Airport Route Feasibility Checker")
114
- airport_selector = gr.Dropdown(airport_options, multiselect=True, label="Select Airports (IATA - Name)")
115
- aircraft_selector = gr.Dropdown(aircraft_options, label="Select Aircraft Type")
116
- check_button = gr.Button("Check Route Feasibility")
117
-
118
- result_output = gr.JSON(label="Result")
 
 
 
 
 
 
 
119
 
120
  # Connect the button click to the check_route function
121
- check_button.click(fn=check_route, inputs=[airport_selector, aircraft_selector], outputs=result_output)
122
 
123
  # Launch the Gradio app
124
- demo.launch()
 
1
  import gradio as gr
2
  import pandas as pd
3
+ from map_generator import *
4
  from flight_distance import *
5
  from optimize import *
6
  from weather import *
 
9
  airport_df = pd.read_csv(r'airport.csv') # Adjust the path to your CSV file
10
  aircraft_df = pd.read_csv(r'aircraft.csv') # Adjust the path to your CSV file
11
 
 
12
  airport_options = [f"{row['IATA']} - {row['Airport_Name']}" for _, row in airport_df.iterrows()]
13
+ airports_dict = {row['IATA']: row['Airport_Name'] for _, row in airport_df.iterrows()} # For map display
14
 
15
  # Ensure the correct column is used for aircraft types
16
  aircraft_type_column = 'Aircraft'
17
  aircraft_options = aircraft_df[aircraft_type_column].tolist()
18
 
 
19
  def check_route(airport_selections, aircraft_type):
20
  # Extract IATA codes from the selected options
21
  airports = [selection.split(" - ")[0] for selection in airport_selections]
 
42
 
43
  # Check if aircraft details were retrieved successfully
44
  if isinstance(aircraft_specs, str):
45
+ return {"Error": aircraft_specs}, "" # Return error message if aircraft not found
46
 
47
+ # Step 7: Check if the aircraft can fly the route
48
+ route_feasibility = check_route_feasibility(optimal_route, trip_distance, aircraft_specs)
49
+
50
+ # Collect sectors needing refuel
51
+ refuel_sectors = set() # Track sectors that require refueling
52
  sector_details = []
53
  refuel_required = False # Flag to track if refueling is required
54
 
 
67
  # Check if refueling is required for this sector
68
  if fuel > aircraft_specs['Max_Fuel_Capacity_kg']:
69
  sector_info["Refuel Required"] = "Yes"
70
+ refuel_sectors.add((optimal_route[i], optimal_route[i + 1])) # Add to refuel sectors
71
  refuel_required = True
72
  else:
73
  sector_info["Refuel Required"] = "No"
 
88
 
89
  if fuel > aircraft_specs['Max_Fuel_Capacity_kg']:
90
  final_leg_info["Refuel Required"] = "Yes"
91
+ refuel_sectors.add((optimal_route[-1], optimal_route[0])) # Add final leg to refuel sectors
92
  refuel_required = True
93
  else:
94
  final_leg_info["Refuel Required"] = "No"
95
 
96
  sector_details.append(final_leg_info)
97
 
98
+ # Step 8: Create the route map with refuel sectors highlighted
99
+ map_html = create_route_map(airports_dict, lat_long_dict, optimal_route, refuel_sectors)
100
+
101
+ # Step 9: Prepare and return result
102
  if refuel_required:
103
  result = {
104
  "Optimal Route": " -> ".join(optimal_route) + f" -> {optimal_route[0]}",
 
114
  "Sector Details": sector_details
115
  }
116
 
117
+ return result, map_html
 
118
 
119
  # Gradio Interface
120
  with gr.Blocks() as demo:
121
  gr.Markdown("## Airport Route Feasibility Checker")
122
+
123
+ # Place components in two columns for results and map
124
+ with gr.Row():
125
+ with gr.Column():
126
+ airport_selector = gr.Dropdown(airport_options, multiselect=True, label="Select Airports (IATA - Name)")
127
+ aircraft_selector = gr.Dropdown(aircraft_options, label="Select Aircraft Type")
128
+ check_button = gr.Button("Check Route Feasibility")
129
+ result_output = gr.JSON(label="Result")
130
+
131
+ with gr.Column():
132
+ gr.Markdown("## Route Map")
133
+ map_output = gr.HTML(label="Route Map")
134
 
135
  # Connect the button click to the check_route function
136
+ check_button.click(fn=check_route, inputs=[airport_selector, aircraft_selector], outputs=[result_output, map_output])
137
 
138
  # Launch the Gradio app
139
+ demo.launch()
map_generator.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import folium
2
+
3
+ # Function to create the map
4
+ def create_route_map(airports, lat_long_dict, optimal_route, refuel_sectors):
5
+ """
6
+ Create a map displaying the optimal route with red straight lines,
7
+ and dotted lines for sectors requiring refuel. Adds a legend for line meanings.
8
+ """
9
+ # Create the map centered at the first airport
10
+ start_lat, start_long = lat_long_dict[optimal_route[0]]
11
+ route_map = folium.Map(location=[start_lat, start_long], zoom_start=4)
12
+
13
+ # Collect bounds for autoscaling the map
14
+ bounds = []
15
+
16
+ # Add markers for each airport
17
+ for i, airport in enumerate(optimal_route):
18
+ lat, lon = lat_long_dict[airport]
19
+ bounds.append([lat, lon])
20
+ folium.Marker([lat, lon], popup=f"{airport} - {airports[airport]}").add_to(route_map)
21
+
22
+ # Draw lines between the airports
23
+ for i in range(len(optimal_route) - 1):
24
+ airport1 = optimal_route[i]
25
+ airport2 = optimal_route[i + 1]
26
+ lat1, lon1 = lat_long_dict[airport1]
27
+ lat2, lon2 = lat_long_dict[airport2]
28
+
29
+ # Check if refuel is required for this sector
30
+ if (airport1, airport2) in refuel_sectors or (airport2, airport1) in refuel_sectors:
31
+ folium.PolyLine(
32
+ locations=[(lat1, lon1), (lat2, lon2)], color="red", weight=2.5, opacity=1, dash_array="10,10"
33
+ ).add_to(route_map)
34
+ else:
35
+ folium.PolyLine(
36
+ locations=[(lat1, lon1), (lat2, lon2)], color="red", weight=2.5, opacity=1
37
+ ).add_to(route_map)
38
+
39
+ # Special case for two points: check the return leg explicitly
40
+ lat_last, lon_last = lat_long_dict[optimal_route[-1]]
41
+ lat_start, lon_start = lat_long_dict[optimal_route[0]]
42
+ if (optimal_route[-1], optimal_route[0]) in refuel_sectors or (optimal_route[0], optimal_route[-1]) in refuel_sectors:
43
+ folium.PolyLine(
44
+ locations=[(lat_last, lon_last), (lat_start, lon_start)], color="red", weight=2.5, opacity=1, dash_array="10,10"
45
+ ).add_to(route_map)
46
+ else:
47
+ folium.PolyLine(
48
+ locations=[(lat_last, lon_last), (lat_start, lon_start)], color="red", weight=2.5, opacity=1
49
+ ).add_to(route_map)
50
+
51
+ # Autoscale the map to fit all points
52
+ route_map.fit_bounds(bounds)
53
+
54
+ # Add custom legend as a child of the map
55
+ legend_html = '''
56
+ <div style="position: fixed;
57
+ bottom: 50px; left: 50px; width: 250px; height: 90px;
58
+ background-color: white; border:2px solid grey; z-index:9999; font-size:14px;">
59
+ &nbsp; <strong>Legend</strong><br>
60
+ &nbsp; <i class="fa fa-circle" style="color:red"></i> Solid line: No refuel required<br>
61
+ &nbsp; <i class="fa fa-circle--" style="color:red"></i> Dotted line: Refuel required
62
+ </div>
63
+ '''
64
+ route_map.get_root().html.add_child(folium.Element(legend_html))
65
+
66
+ # Convert the map to HTML string
67
+ return route_map._repr_html_()
route_map.html ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+
5
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
6
+
7
+ <script>
8
+ L_NO_TOUCH = false;
9
+ L_DISABLE_3D = false;
10
+ </script>
11
+
12
+ <style>html, body {width: 100%;height: 100%;margin: 0;padding: 0;}</style>
13
+ <style>#map {position:absolute;top:0;bottom:0;right:0;left:0;}</style>
14
+ <script src="https://cdn.jsdelivr.net/npm/leaflet@1.9.3/dist/leaflet.js"></script>
15
+ <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
16
+ <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"></script>
17
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.js"></script>
18
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/leaflet@1.9.3/dist/leaflet.css"/>
19
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css"/>
20
+ <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css"/>
21
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.2.0/css/all.min.css"/>
22
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.css"/>
23
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/python-visualization/folium/folium/templates/leaflet.awesome.rotate.min.css"/>
24
+
25
+ <meta name="viewport" content="width=device-width,
26
+ initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
27
+ <style>
28
+ #map_f2b9b17a8d8355bf6792b4ae422a25b1 {
29
+ position: relative;
30
+ width: 100.0%;
31
+ height: 100.0%;
32
+ left: 0.0%;
33
+ top: 0.0%;
34
+ }
35
+ .leaflet-container { font-size: 1rem; }
36
+ </style>
37
+
38
+ </head>
39
+ <body>
40
+
41
+
42
+ <div class="folium-map" id="map_f2b9b17a8d8355bf6792b4ae422a25b1" ></div>
43
+
44
+ </body>
45
+ <script>
46
+
47
+
48
+ var map_f2b9b17a8d8355bf6792b4ae422a25b1 = L.map(
49
+ "map_f2b9b17a8d8355bf6792b4ae422a25b1",
50
+ {
51
+ center: [22.654739, 88.446722],
52
+ crs: L.CRS.EPSG3857,
53
+ zoom: 4,
54
+ zoomControl: true,
55
+ preferCanvas: false,
56
+ }
57
+ );
58
+
59
+
60
+
61
+
62
+
63
+ var tile_layer_312841c4bf44a13069f366baaa0847d4 = L.tileLayer(
64
+ "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
65
+ {"attribution": "\u0026copy; \u003ca href=\"https://www.openstreetmap.org/copyright\"\u003eOpenStreetMap\u003c/a\u003e contributors", "detectRetina": false, "maxNativeZoom": 19, "maxZoom": 19, "minZoom": 0, "noWrap": false, "opacity": 1, "subdomains": "abc", "tms": false}
66
+ );
67
+
68
+
69
+ tile_layer_312841c4bf44a13069f366baaa0847d4.addTo(map_f2b9b17a8d8355bf6792b4ae422a25b1);
70
+
71
+
72
+ var marker_9d9271717824cf9da0dab95f48db98eb = L.marker(
73
+ [22.654739, 88.446722],
74
+ {}
75
+ ).addTo(map_f2b9b17a8d8355bf6792b4ae422a25b1);
76
+
77
+
78
+ var popup_cc05141a40a4dd28cf42a84b81505b8f = L.popup({"maxWidth": "100%"});
79
+
80
+
81
+
82
+ var html_5ad3a9f80f35854605a087077a62f44c = $(`<div id="html_5ad3a9f80f35854605a087077a62f44c" style="width: 100.0%; height: 100.0%;">CCU</div>`)[0];
83
+ popup_cc05141a40a4dd28cf42a84b81505b8f.setContent(html_5ad3a9f80f35854605a087077a62f44c);
84
+
85
+
86
+
87
+ marker_9d9271717824cf9da0dab95f48db98eb.bindPopup(popup_cc05141a40a4dd28cf42a84b81505b8f)
88
+ ;
89
+
90
+
91
+
92
+
93
+ marker_9d9271717824cf9da0dab95f48db98eb.bindTooltip(
94
+ `<div>
95
+ CCU
96
+ </div>`,
97
+ {"sticky": true}
98
+ );
99
+
100
+
101
+ var poly_line_64c1ea99ecdd2ac8195ad3c5c09a9416 = L.polyline(
102
+ [[22.654739, 88.446722], [28.5665, 77.103088]],
103
+ {"bubblingMouseEvents": true, "color": "blue", "dashArray": null, "dashOffset": null, "fill": false, "fillColor": "blue", "fillOpacity": 0.2, "fillRule": "evenodd", "lineCap": "round", "lineJoin": "round", "noClip": false, "opacity": 1, "smoothFactor": 1.0, "stroke": true, "weight": 2.5}
104
+ ).addTo(map_f2b9b17a8d8355bf6792b4ae422a25b1);
105
+
106
+
107
+ var marker_45bec2c73a65e7ea5f8d1ce2b04c4ea5 = L.marker(
108
+ [28.5665, 77.103088],
109
+ {}
110
+ ).addTo(map_f2b9b17a8d8355bf6792b4ae422a25b1);
111
+
112
+
113
+ var popup_a38073ceac50e5501253d2665fa086e2 = L.popup({"maxWidth": "100%"});
114
+
115
+
116
+
117
+ var html_fc2f02230d43e90ed1657f137240ff6a = $(`<div id="html_fc2f02230d43e90ed1657f137240ff6a" style="width: 100.0%; height: 100.0%;">DEL</div>`)[0];
118
+ popup_a38073ceac50e5501253d2665fa086e2.setContent(html_fc2f02230d43e90ed1657f137240ff6a);
119
+
120
+
121
+
122
+ marker_45bec2c73a65e7ea5f8d1ce2b04c4ea5.bindPopup(popup_a38073ceac50e5501253d2665fa086e2)
123
+ ;
124
+
125
+
126
+
127
+
128
+ marker_45bec2c73a65e7ea5f8d1ce2b04c4ea5.bindTooltip(
129
+ `<div>
130
+ DEL
131
+ </div>`,
132
+ {"sticky": true}
133
+ );
134
+
135
+
136
+ var poly_line_7393b88083ba4e5eb5ce6f397bea175a = L.polyline(
137
+ [[28.5665, 77.103088], [12.949986, 77.668206]],
138
+ {"bubblingMouseEvents": true, "color": "blue", "dashArray": null, "dashOffset": null, "fill": false, "fillColor": "blue", "fillOpacity": 0.2, "fillRule": "evenodd", "lineCap": "round", "lineJoin": "round", "noClip": false, "opacity": 1, "smoothFactor": 1.0, "stroke": true, "weight": 2.5}
139
+ ).addTo(map_f2b9b17a8d8355bf6792b4ae422a25b1);
140
+
141
+
142
+ var marker_789b26203246e616ede3030000ac61c6 = L.marker(
143
+ [12.949986, 77.668206],
144
+ {}
145
+ ).addTo(map_f2b9b17a8d8355bf6792b4ae422a25b1);
146
+
147
+
148
+ var popup_3eaae3513672d94c46be693c21c58c22 = L.popup({"maxWidth": "100%"});
149
+
150
+
151
+
152
+ var html_f14429d8ebd2e4ae40284df9766aabfe = $(`<div id="html_f14429d8ebd2e4ae40284df9766aabfe" style="width: 100.0%; height: 100.0%;">BLR</div>`)[0];
153
+ popup_3eaae3513672d94c46be693c21c58c22.setContent(html_f14429d8ebd2e4ae40284df9766aabfe);
154
+
155
+
156
+
157
+ marker_789b26203246e616ede3030000ac61c6.bindPopup(popup_3eaae3513672d94c46be693c21c58c22)
158
+ ;
159
+
160
+
161
+
162
+
163
+ marker_789b26203246e616ede3030000ac61c6.bindTooltip(
164
+ `<div>
165
+ BLR
166
+ </div>`,
167
+ {"sticky": true}
168
+ );
169
+
170
+
171
+ var poly_line_6a981383714f043d8255c1ab6bbdf417 = L.polyline(
172
+ [[12.949986, 77.668206], [22.654739, 88.446722]],
173
+ {"bubblingMouseEvents": true, "color": "blue", "dashArray": null, "dashOffset": null, "fill": false, "fillColor": "blue", "fillOpacity": 0.2, "fillRule": "evenodd", "lineCap": "round", "lineJoin": "round", "noClip": false, "opacity": 1, "smoothFactor": 1.0, "stroke": true, "weight": 2.5}
174
+ ).addTo(map_f2b9b17a8d8355bf6792b4ae422a25b1);
175
+
176
+ </script>
177
+ </html>