ashok2216 commited on
Commit
6030ef9
·
verified ·
1 Parent(s): 4d9866d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -0
app.py CHANGED
@@ -38,6 +38,69 @@ from huggingface_hub import InferenceApi, login, InferenceClient
38
  import branca.colormap as cm
39
  from functools import lru_cache
40
  import time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  # Cache the airport data to avoid reloading it every time
43
  @st.cache_data(ttl=3600) # Cache for 1 hour
@@ -380,6 +443,39 @@ def flight_tracking(flight_view_level, country, local_time_zone, flight_info, ai
380
  tiles='CartoDB dark_matter'
381
  )
382
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
383
  # Create colormap
384
  if color == "rainbow":
385
  colormap = cm.LinearColormap(
@@ -541,6 +637,15 @@ with st.sidebar:
541
  st.write('Now Airports are Visible')
542
  else:
543
  air_port=0
 
 
 
 
 
 
 
 
 
544
  view = st.slider('Increase Flight Visibility',1,6,2)
545
  st.write("You Selected:", view)
546
  cou = st.text_input('Type Country Name', 'north america')
@@ -561,6 +666,7 @@ with st.sidebar:
561
  elif clr == 'hot':
562
  st.write('The current color is', "****:red[Hot]****")
563
  else: None
 
564
  # with st.spinner('Wait!, We Requesting API Data...'):
565
  # try:
566
  flight_tracking(flight_view_level=view, country=cou,flight_info=info,
 
38
  import branca.colormap as cm
39
  from functools import lru_cache
40
  import time
41
+ import math
42
+
43
+ # Weather API configuration
44
+ WEATHER_API_KEY = os.getenv("OPENWEATHER_API_KEY")
45
+ WEATHER_API_URL = "https://api.openweathermap.org/data/2.5/weather"
46
+
47
+ @st.cache_data(ttl=1800) # Cache for 30 minutes
48
+ def fetch_weather_data(lat, lon):
49
+ """Fetch weather data for a specific location"""
50
+ try:
51
+ params = {
52
+ 'lat': lat,
53
+ 'lon': lon,
54
+ 'appid': WEATHER_API_KEY,
55
+ 'units': 'metric'
56
+ }
57
+ response = requests.get(WEATHER_API_URL, params=params, timeout=10)
58
+ response.raise_for_status()
59
+ return response.json()
60
+ except Exception as e:
61
+ st.warning(f"Could not fetch weather data: {str(e)}")
62
+ return None
63
+
64
+ def create_weather_heatmap(weather_data, map_obj):
65
+ """Create a heatmap layer for weather conditions"""
66
+ if not weather_data:
67
+ return
68
+
69
+ # Extract weather data points
70
+ heat_data = []
71
+ for data in weather_data:
72
+ if data and 'main' in data and 'coord' in data:
73
+ lat = data['coord']['lat']
74
+ lon = data['coord']['lon']
75
+ temp = data['main']['temp']
76
+ humidity = data['main']['humidity']
77
+ # Use temperature as the weight for the heatmap
78
+ heat_data.append([lat, lon, temp])
79
+
80
+ if heat_data:
81
+ # Create heatmap layer
82
+ plugins.HeatMap(
83
+ heat_data,
84
+ min_opacity=0.5,
85
+ max_val=max([point[2] for point in heat_data]),
86
+ radius=25,
87
+ blur=15,
88
+ gradient={0.4: 'blue', 0.6: 'lime', 0.7: 'yellow', 1: 'red'}
89
+ ).add_to(map_obj)
90
+
91
+ def get_weather_grid(lat_min, lat_max, lon_min, lon_max, grid_size=5):
92
+ """Create a grid of points for weather data collection"""
93
+ lat_step = (lat_max - lat_min) / grid_size
94
+ lon_step = (lon_max - lon_min) / grid_size
95
+
96
+ weather_points = []
97
+ for i in range(grid_size + 1):
98
+ for j in range(grid_size + 1):
99
+ lat = lat_min + (i * lat_step)
100
+ lon = lon_min + (j * lon_step)
101
+ weather_points.append((lat, lon))
102
+
103
+ return weather_points
104
 
105
  # Cache the airport data to avoid reloading it every time
106
  @st.cache_data(ttl=3600) # Cache for 1 hour
 
443
  tiles='CartoDB dark_matter'
444
  )
445
 
446
+ # Add weather heatmap if API key is available
447
+ if WEATHER_API_KEY:
448
+ with st.spinner('Fetching weather data...'):
449
+ # Get weather grid points
450
+ weather_points = get_weather_grid(lat_min, lat_max, lon_min, lon_max)
451
+
452
+ # Fetch weather data for all points
453
+ weather_data = []
454
+ for lat, lon in weather_points:
455
+ data = fetch_weather_data(lat, lon)
456
+ if data:
457
+ weather_data.append(data)
458
+
459
+ # Create weather heatmap
460
+ create_weather_heatmap(weather_data, m)
461
+
462
+ # Add weather legend
463
+ legend_html = '''
464
+ <div style="position: fixed;
465
+ bottom: 50px; right: 50px; width: 150px; height: 100px;
466
+ background-color: white; padding: 10px; border-radius: 5px;
467
+ border: 2px solid grey; z-index: 9999;">
468
+ <h4 style="margin: 0 0 5px 0;">Temperature (°C)</h4>
469
+ <div style="background: linear-gradient(to right, blue, lime, yellow, red);
470
+ height: 20px; width: 100%; margin-bottom: 5px;"></div>
471
+ <div style="display: flex; justify-content: space-between;">
472
+ <span>Cold</span>
473
+ <span>Hot</span>
474
+ </div>
475
+ </div>
476
+ '''
477
+ m.get_root().html.add_child(folium.Element(legend_html))
478
+
479
  # Create colormap
480
  if color == "rainbow":
481
  colormap = cm.LinearColormap(
 
637
  st.write('Now Airports are Visible')
638
  else:
639
  air_port=0
640
+
641
+ # Add weather toggle
642
+ weather_on = st.toggle('Show Weather Heatmap')
643
+ if weather_on:
644
+ if not WEATHER_API_KEY:
645
+ st.warning("Please set OPENWEATHER_API_KEY environment variable to show weather data")
646
+ else:
647
+ st.write(':rainbow[Weather data will be displayed as a heatmap]')
648
+
649
  view = st.slider('Increase Flight Visibility',1,6,2)
650
  st.write("You Selected:", view)
651
  cou = st.text_input('Type Country Name', 'north america')
 
666
  elif clr == 'hot':
667
  st.write('The current color is', "****:red[Hot]****")
668
  else: None
669
+
670
  # with st.spinner('Wait!, We Requesting API Data...'):
671
  # try:
672
  flight_tracking(flight_view_level=view, country=cou,flight_info=info,