File size: 6,068 Bytes
8c899ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import folium
import pandas as pd
from folium import plugins
from src.map_utils import legend_macro
import leafmap.foliumap as leafmap

EPICENTER_LOCATION = [31.12210171476489, -8.42945837915193]
BORDER_COLOR = "black"

def parse_gg_sheet(url):
    url = url.replace("edit#gid=", "export?format=csv&gid=")
    df = pd.read_csv(url, on_bad_lines="warn")
    return df

# Session for Requests
# session = requests.Session()
# @st.cache_data(persist=True)
# def parse_latlng_from_link(url):
#     try:
#         # extract latitude and longitude from gmaps link
#         if "@" not in url:
#             resp = session.head(url, allow_redirects=True)
#             url = resp.url
#         latlng = url.split("@")[1].split(",")[0:2]
#         return [float(latlng[0]), float(latlng[1])]
#     except Exception as e:
#         return None

def add_latlng_col(df, process_column):
    """Add a latlng column to the dataframe"""
    df = df.assign(latlng=df.iloc[:, process_column].apply(parse_latlng))
    return df

# parse latlng (column 4) to [lat, lng]
import re
def parse_latlng(latlng):
    if pd.isna(latlng):
        return None
        # lat, lng = latlng.split(",")
        # return [float(lat), float(lng)]

    try:
        # check if it matches (30.9529832, -7.1010705) or (30.9529832,-7.1010705)
        if re.match(r"\(\d+\.\d+,\s?-\d+\.\d+\)", latlng):
            lat, lng = latlng[1:-1].split(",")
            return [float(lat), float(lng)]
        # check of it matches 30.9529832, -7.1010705 or 30.9529832,-7.1010705
        elif re.match(r"\d+\.\d+,\s?-\d+\.\d+", latlng):
            lat, lng = latlng.split(",")
            return [float(lat), float(lng)]
        # check if it matches 30,9529832, -7,1010705 or 30,9529832,-7,1010705, match1=30,9529832 and match2=-7,1010705
        elif re.match(r"\d+,\d+,\s?-\d+,\d+", latlng):
            d1, d2, d3, d4 = latlng.split(",")
            return [float(".".join([d1, d2])), float(".".join([d3, d4]))]
    except Exception as e:
        print(f"Error parsing latlng: {latlng}  Reason: {e}")
        return None
    print(f"Error parsing latlng: {latlng}")
    return None

def add_epicentre_to_map(map_obj):
    icon_epicentre = folium.plugins.BeautifyIcon(
        icon='spinner',
        spin=True,
        border_color='#b3334f',
        background_color='#b3334f',
        text_color='white'
    )
    folium.Marker(location=EPICENTER_LOCATION, 
                  popup="Epicenter مركز الزلزال", 
                  icon=icon_epicentre).add_to(map_obj)


def add_danger_distances_to_map(map_obj):
    Danger_Distances_group = folium.FeatureGroup(name='Danger distances - earthquake magnitude 7 | مسافات الخطر - قوة الزلازل 7').add_to(map_obj)
    
    zones = [
        {"radius": 100000, "fill_opacity": 0.1, "weight": 1, "fill_color": "yellow", "tooltip": "50 to 100 km - Moderate risk area | منطقة خطر معتدلة"},
        {"radius": 50000, "fill_opacity": 0.1, "weight": 1, "fill_color": "orange", "tooltip": "30 to 50 km - High risk zone | منطقة عالية المخاطر"},
        {"radius": 30000, "fill_opacity": 0.2, "weight": 1, "fill_color": "#FF0000", "tooltip": "10 to 30 km - Very high risk zone | منطقة شديدة الخطورة"},
        {"radius": 10000, "fill_opacity": 0.2, "weight": 0.2, "fill_color": "#8B0000", "tooltip": "0 to 10km - direct impact zone | منطقة التأثير المباشر"}
    ]

    for zone in zones:
        folium.Circle(
            location=EPICENTER_LOCATION,
            radius=zone["radius"],
            color=BORDER_COLOR,
            weight=zone["weight"],
            fill_opacity=zone["fill_opacity"],
            opacity=zone["fill_opacity"],  # Assuming border opacity should match fill_opacity
            fill_color=zone["fill_color"],
            tooltip=zone["tooltip"],
        ).add_to(Danger_Distances_group)


def init_map():
    m = leafmap.Map(
        center=[31.228674, -7.992047], 
        zoom=8.5,
        height="800px",
        max_bounds=True,
        min_zoom=8.5,
        max_lat=35.628674,
        min_lat=29.628674,
        max_lng=-4.992047,
        min_lng=-10.992047
    )
    # Add a search bar to the map
    plugins.Geocoder(
        collapsed=False,
        position="topright",
        placeholder="Search | البحث",
    ).add_to(m)

    # Add Fullscreen button to the map
    plugins.Fullscreen(
        position="topright",
        title="Expand me | تكبير الخريطة",
        title_cancel="Exit me | تصغير الخريطة",
        force_separate_button=True,
    ).add_to(m)

    # Satellite View from Mapbox
    tileurl = "https://api.mapbox.com/styles/v1/phd2020/clmer2mra01d001pbgjkictpt/tiles/256/{z}/{x}/{y}@2x?access_token=pk.eyJ1IjoicGhkMjAyMCIsImEiOiJja29lZzFwZmUwNHkzMm5wMjZnYjVvcGltIn0.tE0ritrelQOyLdKUH6hgOw"
    folium.TileLayer(
        tiles=tileurl,
        attr="Satellite View",
        name="Satellite View | عرض القمر الصناعي",
        overlay=False,
        control=True,
    ).add_to(m)

    # Json with refreshed images from maxar
    mosaics = ['10300100CB8C5600'] #,'10300100CBAF7D00'] #,'10300100E5404900','10300100E5A0A100'] #,'10300100EB8B4500','10300100EBC1A000','10300100ECC53700','10300100ED11EA00','10300500CFF91800','10300500E4F91400','10300500E4F91500','10300500E4F91700','10300500E4F91800','10300500E4F91900','10400100797DAC00','1040010083289000','10400100889ABF00','104001008A8E9800','104001008B75BB00','1040050047DC6400','1040050057DC8500','1050050030DE8900']
    for i, mosaic_id in enumerate(mosaics): 
        mosaic = f'https://open.gishub.org/maxar-open-data/datasets/Morocco-Earthquake-Sept-2023/{mosaic_id}.json'
        m.add_stac_layer(mosaic, name=f'mosaic_id_{i}')

    # Add danger zones
    add_epicentre_to_map(m)
    add_danger_distances_to_map(m)

    # Add a LayerControl to the map to toggle between layers (Satellite View and Default One)
    folium.LayerControl().add_to(m)

    # Macro to add legend
    m.get_root().add_child(legend_macro)
    return m