#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar  5 17:16:05 2026

@author: pjoulaud
"""

import folium
import webbrowser
import os
import math

def generer_carte_avec_grille():
    print("--- Configuration de la carte avec quadrillage (5°) ---")
    # Création de la carte
    ma_carte = folium.Map(location=[50, 0], zoom_start=5)
    try:
        for i in range(3):
            print(f"### POINT {i+1} ###")
            lat_centre = float(input("Latitude : "))
            lon_centre = float(input("Longitude : "))
            rayon_km = float(input("Rayon (km) : "))
            # 1. Dessiner le cercle
            folium.Circle(
                location=[lat_centre, lon_centre],
                radius=rayon_km * 1000,
                color="blue", fill=True
            ).add_to(ma_carte)

        # 2. Ajout du quadrillage tous les 5 degrés
        # On définit une zone d'affichage de +/- 15 degrés autour du centre
        pas = 5
        etendue = 90
        
        # Lignes horizontales (Parallèles)
        for lat in range(int(lat_centre - etendue), int(lat_centre + etendue + 1), pas):
            folium.PolyLine(
                locations=[[lat, -180], [lat, 180]],
                color="gray", weight=1, opacity=0.5,
                tooltip=f"Lat: {lat}°"
            ).add_to(ma_carte)

        # Lignes verticales (Méridiens)
        for lon in range(int(lon_centre - etendue), int(lon_centre + etendue + 1), pas):
            folium.PolyLine(
                locations=[[-90, lon], [90, lon]],
                color="gray", weight=1, opacity=0.5,
                tooltip=f"Lon: {lon}°"
            ).add_to(ma_carte)

        # Sauvegarde
        nom_fichier = "carte_grille_5deg.html"
        ma_carte.save(nom_fichier)
        webbrowser.open(f"file://{os.path.abspath(nom_fichier)}")
        print(f"\nCarte générée avec succès : {nom_fichier}")

    except ValueError:
        print("Erreur : Entrée invalide.")

if __name__ == "__main__":
    generer_carte_avec_grille()