Spaces:
Sleeping
Sleeping
Download src/components/map.py from NextAI-cl/waterpolice-test: direct link, hf CLI and curl.
- Browser
- Download file 4.82 kB
-
https://huggingface.co/spaces/NextAI-cl/waterpolice-test/resolve/main/src/components/map.py
- Command line
-
hf download hf://spaces/NextAI-cl/waterpolice-test/src/components/map.py
-
curl -L -o map.py https://huggingface.co/spaces/NextAI-cl/waterpolice-test/resolve/main/src/components/map.py
4.82 kB
| """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 | |