Spaces:
Sleeping
Sleeping
| import requests | |
| import folium | |
| import gradio as gr | |
| # =========================== | |
| # Configuración | |
| # =========================== | |
| # Coordenadas de ciudades en EE.UU. | |
| ciudades = { | |
| 'Nueva York': [40.7128, -74.0060], | |
| 'Los Ángeles': [34.0522, -118.2437], | |
| 'Chicago': [41.8781, -87.6298], | |
| 'Houston': [29.7604, -95.3698], | |
| 'Miami': [25.7617, -80.1918], | |
| 'Seattle': [47.6062, -122.3321], | |
| 'Denver': [39.7392, -104.9903], | |
| 'Boston': [42.3601, -71.0589], | |
| 'Atlanta': [33.7490, -84.3880], | |
| 'San Francisco': [37.7749, -122.4194] | |
| } | |
| # API abierta: Open-Meteo (no necesitas token) | |
| API_URL = "https://api.open-meteo.com/v1/forecast" | |
| # =========================== | |
| # Función para obtener datos climáticos en tiempo real | |
| # =========================== | |
| def obtener_datos_climaticos(lat, lon, variables): | |
| params = { | |
| "latitude": lat, | |
| "longitude": lon, | |
| "current_weather": True | |
| } | |
| response = requests.get(API_URL, params=params) | |
| if response.status_code == 200: | |
| data = response.json()["current_weather"] | |
| # Filtramos solo las variables seleccionadas | |
| resultado = {var: data.get(var, 'N/A') for var in variables} | |
| resultado['temperature'] = data.get('temperature', 'N/A') | |
| resultado['windspeed'] = data.get('windspeed', 'N/A') | |
| return resultado | |
| else: | |
| return None | |
| # =========================== | |
| # Crear Mapa Interactivo | |
| # =========================== | |
| def crear_mapa(ciudades_seleccionadas, variable_minima, filtro_variable): | |
| mapa = folium.Map(location=[39.8283, -98.5795], zoom_start=4, tiles='CartoDB positron') | |
| variables_a_consultar = ["temperature", "windspeed"] | |
| for ciudad in ciudades_seleccionadas: | |
| lat, lon = ciudades[ciudad] | |
| datos = obtener_datos_climaticos(lat, lon, variables_a_consultar) | |
| if datos: | |
| valor_filtro = datos.get(filtro_variable, 0) | |
| if valor_filtro != 'N/A' and float(valor_filtro) >= variable_minima: | |
| popup_text = ( | |
| f"<b>{ciudad}</b><br>" | |
| f"🌡️ Temp: {datos['temperature']}°C<br>" | |
| f"💨 Viento: {datos['windspeed']} km/h<br>" | |
| ) | |
| folium.CircleMarker( | |
| location=[lat, lon], | |
| radius=8, | |
| fill=True, | |
| color='blue', | |
| fill_color='cyan', | |
| fill_opacity=0.7, | |
| popup=popup_text | |
| ).add_to(mapa) | |
| return mapa._repr_html_() | |
| # =========================== | |
| # Interfaz Gradio | |
| # =========================== | |
| gr.Interface( | |
| fn=crear_mapa, | |
| inputs=[ | |
| gr.CheckboxGroup(choices=list(ciudades.keys()), label="Selecciona ciudades", value=["Nueva York", "Los Ángeles"]), | |
| gr.Slider(0, 50, step=1, value=10, label="Umbral mínimo para filtrar"), | |
| gr.Dropdown(choices=["temperature", "windspeed"], value="temperature", label="Variable para filtrar") | |
| ], | |
| outputs=gr.HTML(label="🌎 Mapa Interactivo"), | |
| title="🌦️ Mapa Clima USA en Tiempo Real", | |
| description="Visualiza el clima actual de ciudades de EE.UU. Filtra por temperatura o viento.", | |
| theme="soft", | |
| live=True | |
| ).launch() | |