loubnabnl HF staff commited on
Commit
86a34e4
1 Parent(s): bb85d5e

Create utils.py

Browse files
Files changed (1) hide show
  1. src/utils.py +110 -0
src/utils.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import folium
2
+ import pandas as pd
3
+ from folium import plugins
4
+ from src.map_utils import legend_macro
5
+
6
+
7
+ EPICENTER_LOCATION = [31.12210171476489, -8.42945837915193]
8
+ BORDER_COLOR = "black"
9
+
10
+ def parse_gg_sheet(url):
11
+ url = url.replace("edit#gid=", "export?format=csv&gid=")
12
+ print(url)
13
+ df = pd.read_csv(url, on_bad_lines="warn")
14
+
15
+ # parse latlng (column 4) to [lat, lng]
16
+ def parse_latlng(latlng):
17
+ try:
18
+ lat, lng = latlng.split(",")
19
+ return [float(lat), float(lng)]
20
+ except Exception as e:
21
+ print(f"Error parsing latlng: {e}")
22
+ return None
23
+
24
+ if df.shape[1] > 4:
25
+ df = df.assign(latlng=df.iloc[:, 4].apply(parse_latlng))
26
+ return df
27
+
28
+
29
+ def add_epicentre_to_map(map_obj):
30
+ icon_epicentre = folium.plugins.BeautifyIcon(
31
+ icon='spinner',
32
+ spin=True,
33
+ border_color='#b3334f',
34
+ background_color='#b3334f',
35
+ text_color='white'
36
+ )
37
+ folium.Marker(location=EPICENTER_LOCATION,
38
+ popup="Epicenter مركز الزلزال",
39
+ icon=icon_epicentre).add_to(map_obj)
40
+
41
+
42
+ def add_danger_distances_to_map(map_obj):
43
+ Danger_Distances_group = folium.FeatureGroup(name='Danger distances - earthquake magnitude 7 | مسافات الخطر - قوة الزلازل 7').add_to(map_obj)
44
+
45
+ zones = [
46
+ {"radius": 100000, "fill_opacity": 0.1, "weight": 1, "fill_color": "yellow", "tooltip": "50 to 100 km - Moderate risk area | منطقة خطر معتدلة"},
47
+ {"radius": 50000, "fill_opacity": 0.1, "weight": 1, "fill_color": "orange", "tooltip": "30 to 50 km - High risk zone | منطقة عالية المخاطر"},
48
+ {"radius": 30000, "fill_opacity": 0.2, "weight": 1, "fill_color": "#FF0000", "tooltip": "10 to 30 km - Very high risk zone | منطقة شديدة الخطورة"},
49
+ {"radius": 10000, "fill_opacity": 0.2, "weight": 0.2, "fill_color": "#8B0000", "tooltip": "0 to 10km - direct impact zone | منطقة التأثير المباشر"}
50
+ ]
51
+
52
+ for zone in zones:
53
+ folium.Circle(
54
+ location=EPICENTER_LOCATION,
55
+ radius=zone["radius"],
56
+ color=BORDER_COLOR,
57
+ weight=zone["weight"],
58
+ fill_opacity=zone["fill_opacity"],
59
+ opacity=zone["fill_opacity"], # Assuming border opacity should match fill_opacity
60
+ fill_color=zone["fill_color"],
61
+ tooltip=zone["tooltip"],
62
+ ).add_to(Danger_Distances_group)
63
+
64
+
65
+ def init_map():
66
+ m = folium.Map(
67
+ location=[31.228674, -7.992047],
68
+ zoom_start=8.5,
69
+ min_zoom=8.5,
70
+ max_lat=35.628674,
71
+ min_lat=29.628674,
72
+ max_lon=-4.992047,
73
+ min_lon=-10.992047,
74
+ max_bounds=True,
75
+ )
76
+ # Add a search bar to the map
77
+ plugins.Geocoder(
78
+ collapsed=False,
79
+ position="topright",
80
+ placeholder="Search | البحث",
81
+ ).add_to(m)
82
+
83
+ # Add Fullscreen button to the map
84
+ plugins.Fullscreen(
85
+ position="topright",
86
+ title="Expand me | تكبير الخريطة",
87
+ title_cancel="Exit me | تصغير الخريطة",
88
+ force_separate_button=True,
89
+ ).add_to(m)
90
+
91
+ # Satellite View from Mapbox
92
+ tileurl = "https://api.mapbox.com/styles/v1/phd2020/clmer2mra01d001pbgjkictpt/tiles/256/{z}/{x}/{y}@2x?access_token=pk.eyJ1IjoicGhkMjAyMCIsImEiOiJja29lZzFwZmUwNHkzMm5wMjZnYjVvcGltIn0.tE0ritrelQOyLdKUH6hgOw"
93
+ folium.TileLayer(
94
+ tiles=tileurl,
95
+ attr="Satellite View",
96
+ name="Satellite View | عرض القمر الصناعي",
97
+ overlay=False,
98
+ control=True,
99
+ ).add_to(m)
100
+
101
+ # Add danger zones
102
+ add_epicentre_to_map(m)
103
+ add_danger_distances_to_map(m)
104
+
105
+ # Add a LayerControl to the map to toggle between layers (Satellite View and Default One)
106
+ folium.LayerControl().add_to(m)
107
+
108
+ # Macro to add legend
109
+ m.get_root().add_child(legend_macro)
110
+ return m