loubnabnl HF staff commited on
Commit
a7d29b5
1 Parent(s): 15ad0b9

Update src/utils.py

Browse files
Files changed (1) hide show
  1. src/utils.py +50 -18
src/utils.py CHANGED
@@ -9,27 +9,59 @@ 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=None,
32
- spin=False,
33
  border_color='#b3334f',
34
  background_color='#b3334f',
35
  text_color='white'
@@ -89,13 +121,13 @@ def init_map():
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
 
9
 
10
  def parse_gg_sheet(url):
11
  url = url.replace("edit#gid=", "export?format=csv&gid=")
 
12
  df = pd.read_csv(url, on_bad_lines="warn")
13
+ return df
14
 
15
+ # Session for Requests
16
+ # session = requests.Session()
17
+ # @st.cache_data(persist=True)
18
+ # def parse_latlng_from_link(url):
19
+ # try:
20
+ # # extract latitude and longitude from gmaps link
21
+ # if "@" not in url:
22
+ # resp = session.head(url, allow_redirects=True)
23
+ # url = resp.url
24
+ # latlng = url.split("@")[1].split(",")[0:2]
25
+ # return [float(latlng[0]), float(latlng[1])]
26
+ # except Exception as e:
27
+ # return None
28
+
29
+ def add_latlng_col(df, process_column):
30
+ """Add a latlng column to the dataframe"""
31
+ df = df.assign(latlng=df.iloc[:, process_column].apply(parse_latlng))
32
  return df
33
 
34
+ # parse latlng (column 4) to [lat, lng]
35
+ import re
36
+ def parse_latlng(latlng):
37
+ if pd.isna(latlng):
38
+ return None
39
+ # lat, lng = latlng.split(",")
40
+ # return [float(lat), float(lng)]
41
+
42
+ try:
43
+ # check if it matches (30.9529832, -7.1010705) or (30.9529832,-7.1010705)
44
+ if re.match(r"\(\d+\.\d+,\s?-\d+\.\d+\)", latlng):
45
+ lat, lng = latlng[1:-1].split(",")
46
+ return [float(lat), float(lng)]
47
+ # check of it matches 30.9529832, -7.1010705 or 30.9529832,-7.1010705
48
+ elif re.match(r"\d+\.\d+,\s?-\d+\.\d+", latlng):
49
+ lat, lng = latlng.split(",")
50
+ return [float(lat), float(lng)]
51
+ # check if it matches 30,9529832, -7,1010705 or 30,9529832,-7,1010705, match1=30,9529832 and match2=-7,1010705
52
+ elif re.match(r"\d+,\d+,\s?-\d+,\d+", latlng):
53
+ d1, d2, d3, d4 = latlng.split(",")
54
+ return [float(".".join([d1, d2])), float(".".join([d3, d4]))]
55
+ except Exception as e:
56
+ print(f"Error parsing latlng: {latlng} Reason: {e}")
57
+ return None
58
+ print(f"Error parsing latlng: {latlng}")
59
+ return None
60
 
61
  def add_epicentre_to_map(map_obj):
62
+ # Removed the spinner to not confuse the users as the map is already loaded
63
  icon_epicentre = folium.plugins.BeautifyIcon(
64
+ icon='star',
 
65
  border_color='#b3334f',
66
  background_color='#b3334f',
67
  text_color='white'
 
121
  ).add_to(m)
122
 
123
  # Satellite View from Mapbox
124
+ tileurl = "https://marocmap.ikiker.com/maroc/{z}/{x}/{y}.png"
125
  folium.TileLayer(
126
  tiles=tileurl,
127
+ attr="Maroc Map",
128
+ name="Maroc Map",
129
  overlay=False,
130
+ control=False,
131
  ).add_to(m)
132
 
133
  # Add danger zones