Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
from datetime import datetime | |
import folium | |
import pandas as pd | |
COLOR_MAPPING = { | |
"إغاثة": "red", | |
"مساعدة طبية": "orange", | |
"مأوى": "beige", | |
"طعام وماء": "blue", | |
"مخاطر (تسرب الغاز، تلف في الخدمات العامة...)": "gray", | |
} | |
ICON_MAPPING = { | |
"إغاثة": "bell", # life ring icon for rescue | |
"مساعدة طبية": "heart", # medical kit for medical assistance | |
"مأوى": "home", # home icon for shelter | |
"طعام وماء": "cutlery", # cutlery (fork and knife) for food & water | |
"مخاطر (تسرب الغاز، تلف في الخدمات العامة...)": "Warning" # warning triangle for dangers | |
} | |
def numcol(x): | |
return ord(x) - ord("A") | |
def marker_request(request): | |
# in case of multiple requests we use the first one for the marker's icon | |
# requests are already sorted by priority from the form | |
try: | |
displayed_request = request.split(',')[0] | |
except: | |
displayed_request = request | |
return displayed_request | |
## Interventions | |
def display_interventions(interventions_df, selected_statuses, map_obj, intervention_fgs): | |
"""Display NGO interventions on the map""" | |
for index, row in interventions_df.iterrows(): | |
village_status = row[interventions_df.columns[numcol("K")]] | |
is_future_intervention = ( | |
row[interventions_df.columns[numcol("H")]] == "Intervention prévue dans le futur / Planned future intervention" | |
) | |
if pd.isna(village_status) and not is_future_intervention: | |
village_status = "Partiellement satisfait / Partially Served" | |
if village_status not in selected_statuses: | |
continue | |
if is_future_intervention: | |
color_mk = "pink" | |
status = "Planned ⌛" | |
elif village_status != "Critique, Besoin d'aide en urgence / Critical, in urgent need of help": | |
# past intervention and village not in a critical condition | |
color_mk = "green" | |
status = "Done ✅" | |
else: | |
color_mk = "darkgreen" | |
status = "Partial 📝" | |
# ['Horodateur', 'Organization Name', 'Association ID', 'Speciality', | |
# 'Phone 1', 'Phone 2', 'Email', 'Intervention status', | |
# 'Intervention date', 'Types of Help Provided', | |
# 'Current situation of the area', 'Future help type', 'Douar name', | |
# 'Commune', 'Caidat', ' Google Maps Link', | |
# 'Accessibility to the targeted douar', 'Type of accessibility', | |
# 'Population of the douar if available', 'Intervention Additional Info', | |
# 'Any Additional info is welcome', 'Automatic Extracted Coordinates', | |
# 'Unnamed: 22'], | |
org = row[interventions_df.columns[numcol("B")]] | |
association_id = row[interventions_df.columns[numcol("C")]] | |
speciality = row[interventions_df.columns[numcol("D")]] | |
phone_1 = row[interventions_df.columns[numcol("E")]] | |
phone_2 = row[interventions_df.columns[numcol("F")]] | |
email = row[interventions_df.columns[numcol("G")]] | |
intervention_status = row[interventions_df.columns[numcol("H")]] | |
intervention_date = row[interventions_df.columns[numcol("I")]] | |
help_type = row[interventions_df.columns[numcol("J")]] | |
current_situation = row[interventions_df.columns[numcol("K")]] | |
future_help_type = row[interventions_df.columns[numcol("L")]] | |
douar_name = row[interventions_df.columns[numcol("M")]] | |
commune = row[interventions_df.columns[numcol("N")]] | |
caidat = row[interventions_df.columns[numcol("O")]] | |
row[interventions_df.columns[numcol("P")]] | |
accessibility_to_douar = row[interventions_df.columns[numcol("Q")]] | |
type_of_accessibility = row[interventions_df.columns[numcol("R")]] | |
population = row[interventions_df.columns[numcol("S")]] | |
intervention_additional_info = row[interventions_df.columns[numcol("T")]] | |
any_additional_info = row[interventions_df.columns[numcol("U")]] | |
full_douar_name = ", ".join([x for x in [douar_name, commune, caidat] if not pd.isna(x)]) | |
intervention_info = f""" | |
<b>Org:</b> {org}<br> | |
<b>Association ID:</b> {association_id}<br> | |
<b>Speciality:</b> {speciality}<br> | |
<b>Phone 1:</b> {phone_1}<br> | |
<b>Phone 2:</b> {phone_2}<br> | |
<b>Email:</b> {email}<br> | |
<b>Intervention Date:</b> {intervention_date}<br> | |
<b>Intervention Status:</b> {intervention_status}<br> | |
<b>Help Type:</b> {help_type}<br> | |
<b>Current Situation:</b> {current_situation}<br> | |
<b>Future Help Type:</b> {future_help_type}<br> | |
<b>Douar Name:</b> {full_douar_name}<br> | |
<b>Accessibility to Douar:</b> {accessibility_to_douar}<br> | |
<b>Type of Accessibility:</b> {type_of_accessibility}<br> | |
<b>Population:</b> {population}<br> | |
<b>Intervention Additional Info:</b> {intervention_additional_info}<br> | |
<b>Any Additional Info:</b> {any_additional_info}<br> | |
""" #TODO: filter nans | |
if row["latlng"] is None: | |
continue | |
fg = intervention_fgs[status] | |
fg.add_child( | |
folium.Marker( | |
location=row["latlng"], | |
tooltip=full_douar_name, | |
popup=folium.Popup(intervention_info, max_width=300), | |
icon=folium.Icon(color=color_mk), | |
) | |
) | |
def display_solved(solved_verified_requests, selected_statuses, feature_group): | |
for index, row in solved_verified_requests.iterrows(): | |
if row["latlng"] is None: | |
continue | |
intervention_status = row[solved_verified_requests.columns[8]] | |
is_future_intervention = ( | |
intervention_status == "Planned" | |
) | |
if is_future_intervention: | |
status = "Planned ⌛" | |
icon = folium.Icon(icon="heart", prefix="glyphicon", color="pink", icon_color="red") | |
else: | |
status = "Done ✅" | |
icon = folium.Icon(icon="heart", prefix="glyphicon", color="green", icon_color="red") | |
# if village_status not in selected_statuses: | |
# continue # TODO: enable filters | |
intervention_type = row[solved_verified_requests.columns[2]] | |
details = row[solved_verified_requests.columns[3]] | |
contact = row[solved_verified_requests.columns[4]] | |
location = row[solved_verified_requests.columns[5]] | |
org = row[solved_verified_requests.columns[9]] | |
intervention_date = row[solved_verified_requests.columns[10]] | |
remarks = row[solved_verified_requests.columns[11]] | |
intervention_info = f""" | |
<b>Intervention Date:</b> {intervention_date}<br> | |
<b>Org:</b> {org}<br> | |
<b>Intervention:</b> {intervention_type}<br> | |
<b>Invervention Status:</b> {status}<br> | |
<b>Details:</b> {details}<br> | |
<b>Location:</b> {location}<br> | |
<b>Remarks:</b> {remarks}<br> | |
<b>Contact:</b> {contact}<br> | |
""" | |
# golden color | |
feature_group.add_child( | |
folium.Marker( | |
location=row["latlng"], | |
tooltip=location, | |
popup=folium.Popup(intervention_info, max_width=300), | |
icon=icon | |
) | |
) | |
## Requests | |
def show_requests(filtered_df, feature_group): | |
"""Display victim requests on the map""" | |
for index, row in filtered_df.iterrows(): | |
request_type = row["ما هي احتياجاتك؟ (أضفها إذا لم يتم ذكرها)"] | |
displayed_request = marker_request(request_type) # TODO: the marker should depend on selected_options | |
long_lat = row["latlng"] | |
maps_url = f"https://maps.google.com/?q={long_lat}" | |
douar = row[filtered_df.columns[3]] | |
person_in_place = row[filtered_df.columns[6]] | |
douar_info = row[filtered_df.columns[9]] | |
source = row[filtered_df.columns[10]] | |
# we display all requests in popup text and use the first one for the icon/color | |
display_text = f""" | |
<b>Request Type:</b> {request_type}<br> | |
<b>Id:</b> {row["id"]}<br> | |
<b>Source:</b> {source}<br> | |
<b>Person in place:</b> {person_in_place}<br> | |
<b>Douar:</b> {douar}<br> | |
<b>Douar Info:</b> {douar_info}<br> | |
<a href="{maps_url}" target="_blank" rel="noopener noreferrer"><b>Google Maps</b></a> | |
""" | |
icon_name = ICON_MAPPING.get(request_type, "list") | |
if long_lat is None: | |
continue | |
feature_group.add_child( | |
folium.Marker( | |
location=long_lat, | |
tooltip=row[" لأي جماعة / قيادة / دوار تنتمون ؟"] | |
if not pd.isna(row[" لأي جماعة / قيادة / دوار تنتمون ؟"]) | |
else None, | |
popup=folium.Popup(display_text, max_width=300), | |
icon=folium.Icon( | |
color=COLOR_MAPPING.get(displayed_request, "beige"), icon=icon_name, prefix="glyphicon" | |
), | |
) | |
) | |
def show_verified_requests(filtered_verified_df, emergency_fgs): | |
"""Display verified victim requests on the map""" | |
global fg | |
verified_color_mapping = { | |
"Low": "beige", | |
"Medium": "orange", | |
"High": "red", | |
} | |
for index, row in filtered_verified_df.iterrows(): | |
long_lat = row["latlng"] | |
# we display all requests in popup text and use the first one for the icon/color | |
display_text = "" | |
for col, val in zip(filtered_verified_df.columns, row): | |
if col == "Help Details": | |
request_type = row["Help Details"] | |
marker_request(request_type) # TODO: the marker should depend on selected_options | |
display_text += f"<b>Request Type:</b> {request_type}<br>" | |
elif col == "Location Details": | |
display_text += f"<b>Location:</b> {val}<br>" | |
elif col == "Emergency Degree": | |
display_text += f"<b>Emergency Degree:</b> {val}<br>" | |
elif col == "Verification Date": | |
display_text += f"<b>Verification Date:</b> {val}<br>" | |
elif col == "id": | |
display_text = f"<b>Id:</b> {val}<br>" + display_text | |
elif col == "latlng": | |
maps_url = f"https://maps.google.com/?q={val}" | |
display_text += ( | |
f'<a href="{maps_url}" target="_blank" rel="noopener noreferrer"><b>Google Maps</b></a><br>' | |
) | |
# mark as solved button | |
id_in_sheet = row["id"] + 2 | |
display_text += f"<a href='https://docs.google.com/forms/d/e/1FAIpQLSdyAcOAULumk4A1DsfrwUsGdZ-9G5xOUuD3vHdQOp3nGNAZXw/viewform?usp=pp_url&entry.1499427789={id_in_sheet}&entry.1666684596={datetime.now().strftime('%Y-%m-%d')}' target='_blank' rel='noopener noreferrer'><b>Mark as solved</b></a><br>" | |
icon_name = ICON_MAPPING.get(request_type, "list") | |
emergency = row.get("Emergency Degree", "Low") | |
if long_lat is None: | |
continue | |
location = row["Location Details"] | |
# Select the correct feature group | |
fg_emergency_group = emergency_fgs[emergency] | |
fg_emergency_group.add_child( | |
folium.Marker( | |
location=long_lat, | |
tooltip=location if not pd.isna(location) else None, | |
popup=folium.Popup(display_text, max_width=300), | |
icon=folium.Icon( | |
color=verified_color_mapping.get(emergency, "beige"), icon=icon_name, prefix="glyphicon" | |
), | |
) | |
) |