File size: 3,252 Bytes
ca1d8d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e93bd12
ca1d8d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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()