File size: 4,823 Bytes
279a849
 
 
 
ed16c76
 
 
 
 
 
 
 
 
 
279a849
54330c3
ed16c76
54330c3
ed16c76
54330c3
ed16c76
279a849
ed16c76
279a849
 
ed16c76
 
279a849
 
97b6873
279a849
 
 
 
 
 
 
 
 
 
 
 
 
ed16c76
279a849
 
ed16c76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
279a849
 
 
ed16c76
279a849
 
 
e28c076
279a849
 
 
 
 
ed16c76
279a849
 
 
 
 
 
 
ed16c76
 
279a849
 
ed16c76
279a849
 
 
e28c076
279a849
 
 
ed16c76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
279a849
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""Map visualization components."""

import folium

from models.config import NIVELES_RIESGO


def obtener_color_por_prioridad(prioridad):
    """Retorna color basado en prioridad (ALTA/MEDIA/Normal)."""
    config = NIVELES_RIESGO.get(prioridad, NIVELES_RIESGO['Normal'])
    color_map = {'#F44336': 'red', '#FF9800': 'orange', '#4CAF50': 'green'}
    color_name = color_map.get(config['color'], 'blue')
    return config['color'], color_name, config['label']


def obtener_color_por_score(score_riesgo):
    """Legacy: color por score 0-5."""
    if score_riesgo > 4.5:
        return '#FF0000', 'red', 'Alto'
    elif score_riesgo >= 2.5:
        return '#FFA500', 'orange', 'Medio'
    else:
        return '#00FF00', 'green', 'Bajo'


def crear_mapa(lat, lon, resultado=None, score_riesgo=None, prediccion=None, consumo_real=None):
    """Crea un mapa con el punto seleccionado y colores segun riesgo."""
    mapa = folium.Map(
        location=[lat, lon],
        zoom_start=18,
        tiles=None
    )

    # Capas de tiles
    folium.TileLayer('OpenStreetMap', name='Calles').add_to(mapa)
    folium.TileLayer(
        tiles='https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
        attr='Esri',
        name='Satelital'
    ).add_to(mapa)
    folium.TileLayer(
        tiles='http://mt0.google.com/vt/lyrs=y&hl=en&x={x}&y={y}&z={z}',
        attr='Google',
        name='Google Hibrido'
    ).add_to(mapa)

    # Determinar color del marcador
    if resultado is not None:
        prioridad = resultado.get('prioridad', 'Normal')
        color_hex, color_name, nivel_label = obtener_color_por_prioridad(prioridad)
        votos = resultado.get('votos', 0)
        consumo_actual = resultado.get('consumo_actual', 0)
        consumo_prom = resultado.get('consumo_promedio', 0)

        popup_text = f"""
        <b>Analisis de Anomalia</b><br>
        <b>Lat:</b> {lat:.6f} | <b>Lon:</b> {lon:.6f}<br>
        <hr>
        <b>Prioridad:</b> {prioridad} ({votos}/2 votos)<br>
        <b>IF:</b> {'Anomalia' if resultado.get('if_anomalia') else 'Normal'} (score: {resultado.get('if_score', 0):.3f})<br>
        <b>LOF:</b> {'Anomalia' if resultado.get('lof_anomalia') else 'Normal'} (score: {resultado.get('lof_score', 0):.3f})<br>
        <hr>
        <b>Consumo actual:</b> {consumo_actual} m3<br>
        <b>Consumo promedio:</b> {consumo_prom:.1f} m3
        """
    elif score_riesgo is not None:
        color_hex, color_name, _ = obtener_color_por_score(score_riesgo)
        popup_text = f"""
        <b>Latitud:</b> {lat:.6f}<br>
        <b>Longitud:</b> {lon:.6f}<br>
        <b>Score Riesgo:</b> {score_riesgo:.2f}
        """
    else:
        color_name = 'blue'
        color_hex = '#3388ff'
        popup_text = f"""
        <b>Latitud:</b> {lat:.6f}<br>
        <b>Longitud:</b> {lon:.6f}
        """

    # Marcador
    folium.Marker(
        [lat, lon],
        popup=folium.Popup(popup_text, max_width=300),
        icon=folium.Icon(color=color_name, icon='tint', prefix='fa'),
        tooltip=f"Lat: {lat:.4f}, Lon: {lon:.4f}"
    ).add_to(mapa)

    # Circulo de area de influencia
    if resultado is not None or score_riesgo is not None:
        folium.Circle(
            [lat, lon],
            radius=30,
            color=color_hex,
            fill=True,
            fillColor=color_hex,
            fillOpacity=0.2,
            weight=2
        ).add_to(mapa)

    # Leyenda
    legend_html = '''
    <div style="position: fixed;
                bottom: 50px; right: 50px; width: 180px; height: 130px;
                background-color: white; border:2px solid grey; z-index:9999;
                font-size:14px; padding: 10px; border-radius: 5px; color: black;">
        <p style="margin: 0; font-weight: bold; text-align: center; margin-bottom: 10px; color: black;">
            Nivel de Riesgo
        </p>
        <p style="margin: 5px 0; color: black;">
            <span style="background-color: #F44336; width: 20px; height: 20px; display: inline-block; border: 1px solid black;"></span>
            <span style="margin-left: 5px;">Alto (2 votos)</span>
        </p>
        <p style="margin: 5px 0; color: black;">
            <span style="background-color: #FF9800; width: 20px; height: 20px; display: inline-block; border: 1px solid black;"></span>
            <span style="margin-left: 5px;">Medio (1 voto)</span>
        </p>
        <p style="margin: 5px 0; color: black;">
            <span style="background-color: #4CAF50; width: 20px; height: 20px; display: inline-block; border: 1px solid black;"></span>
            <span style="margin-left: 5px;">Bajo (0 votos)</span>
        </p>
    </div>
    '''
    mapa.get_root().html.add_child(folium.Element(legend_html))

    folium.LayerControl().add_to(mapa)
    return mapa