Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,8 +6,9 @@ from math import radians, cos, sin, sqrt, atan2
|
|
| 6 |
from branca.element import MacroElement
|
| 7 |
from jinja2 import Template
|
| 8 |
|
| 9 |
-
# 1. Districts and Coordinates
|
| 10 |
places = {
|
|
|
|
| 11 |
"Nyarugenge": (-1.9577, 30.0619), "Gasabo": (-1.9400, 30.0861), "Kicukiro": (-1.9781, 30.0597),
|
| 12 |
"Burera": (-1.4800, 29.7300), "Gakenke": (-1.5700, 29.7561), "Rulindo": (-1.8333, 30.0833),
|
| 13 |
"Musanze": (-1.5014, 29.6344), "Gicumbi": (-1.5794, 30.0542), "Nyagatare": (-1.3100, 30.3000),
|
|
@@ -20,9 +21,9 @@ places = {
|
|
| 20 |
"Nyamasheke": (-2.4700, 29.3222), "Ngororero": (-1.8733, 29.5811)
|
| 21 |
}
|
| 22 |
|
| 23 |
-
# 2. Distance calculator
|
| 24 |
def haversine(coord1, coord2):
|
| 25 |
-
R = 6371
|
| 26 |
lat1, lon1 = coord1
|
| 27 |
lat2, lon2 = coord2
|
| 28 |
dlat = radians(lat2 - lat1)
|
|
@@ -31,7 +32,7 @@ def haversine(coord1, coord2):
|
|
| 31 |
c = 2 * atan2(sqrt(a), sqrt(1 - a))
|
| 32 |
return R * c
|
| 33 |
|
| 34 |
-
# 3.
|
| 35 |
edges = [
|
| 36 |
("Nyarugenge", "Gasabo"), ("Gasabo", "Kicukiro"), ("Kicukiro", "Bugesera"), ("Bugesera", "Rwamagana"),
|
| 37 |
("Rwamagana", "Kayonza"), ("Kayonza", "Kirehe"), ("Kirehe", "Ngoma"), ("Ngoma", "Gatsibo"),
|
|
@@ -41,7 +42,10 @@ edges = [
|
|
| 41 |
("Ngororero", "Muhanga"), ("Muhanga", "Kamonyi"), ("Kamonyi", "Nyarugenge"), ("Muhanga", "Ruhango"),
|
| 42 |
("Ruhango", "Nyanza"), ("Nyanza", "Huye"), ("Huye", "Gisagara"), ("Gisagara", "Nyaruguru"),
|
| 43 |
("Nyaruguru", "Nyamagabe"), ("Nyamagabe", "Karongi"), ("Ngororero", "Ruhango"),
|
| 44 |
-
("Gicumbi", "Gasabo"), ("Bugesera", "Ngoma")
|
|
|
|
|
|
|
|
|
|
| 45 |
]
|
| 46 |
|
| 47 |
# 4. Create Graph
|
|
@@ -49,7 +53,7 @@ G = nx.Graph()
|
|
| 49 |
for u, v in edges:
|
| 50 |
G.add_edge(u, v, weight=haversine(places[u], places[v]))
|
| 51 |
|
| 52 |
-
# 5.
|
| 53 |
class AnimateMarker(MacroElement):
|
| 54 |
_template = Template("""
|
| 55 |
{% macro script(this, kwargs) %}
|
|
@@ -72,7 +76,7 @@ class AnimateMarker(MacroElement):
|
|
| 72 |
self._name = "AnimateMarker"
|
| 73 |
self.locations = locations
|
| 74 |
|
| 75 |
-
# 6.
|
| 76 |
def generate_map(start, end):
|
| 77 |
if start == end:
|
| 78 |
return "Hitamo aho utangiriye n’aho ugiye bitandukanye.", ""
|
|
@@ -84,7 +88,7 @@ def generate_map(start, end):
|
|
| 84 |
path = nx.astar_path(G, start, end, heuristic=lambda u, v: haversine(places[u], places[v]), weight='weight')
|
| 85 |
coords = [places[p] for p in path]
|
| 86 |
|
| 87 |
-
m = Map(location=[-
|
| 88 |
for name, coord in places.items():
|
| 89 |
Marker(location=coord, popup=name).add_to(m)
|
| 90 |
|
|
@@ -95,7 +99,7 @@ def generate_map(start, end):
|
|
| 95 |
except Exception as e:
|
| 96 |
return f"Ntibishoboka kubona inzira: {str(e)}", ""
|
| 97 |
|
| 98 |
-
# 7. Gradio
|
| 99 |
iface = gr.Interface(
|
| 100 |
fn=generate_map,
|
| 101 |
inputs=[
|
|
@@ -111,4 +115,3 @@ iface = gr.Interface(
|
|
| 111 |
)
|
| 112 |
|
| 113 |
iface.launch()
|
| 114 |
-
|
|
|
|
| 6 |
from branca.element import MacroElement
|
| 7 |
from jinja2 import Template
|
| 8 |
|
| 9 |
+
# 1. Districts and Coordinates (Harimo Kigali)
|
| 10 |
places = {
|
| 11 |
+
"Kigali": (-1.9441, 30.0619), # Kigali city center
|
| 12 |
"Nyarugenge": (-1.9577, 30.0619), "Gasabo": (-1.9400, 30.0861), "Kicukiro": (-1.9781, 30.0597),
|
| 13 |
"Burera": (-1.4800, 29.7300), "Gakenke": (-1.5700, 29.7561), "Rulindo": (-1.8333, 30.0833),
|
| 14 |
"Musanze": (-1.5014, 29.6344), "Gicumbi": (-1.5794, 30.0542), "Nyagatare": (-1.3100, 30.3000),
|
|
|
|
| 21 |
"Nyamasheke": (-2.4700, 29.3222), "Ngororero": (-1.8733, 29.5811)
|
| 22 |
}
|
| 23 |
|
| 24 |
+
# 2. Distance calculator (Haversine Formula)
|
| 25 |
def haversine(coord1, coord2):
|
| 26 |
+
R = 6371 # Earth radius in km
|
| 27 |
lat1, lon1 = coord1
|
| 28 |
lat2, lon2 = coord2
|
| 29 |
dlat = radians(lat2 - lat1)
|
|
|
|
| 32 |
c = 2 * atan2(sqrt(a), sqrt(1 - a))
|
| 33 |
return R * c
|
| 34 |
|
| 35 |
+
# 3. Edges (connect Kigali and other districts)
|
| 36 |
edges = [
|
| 37 |
("Nyarugenge", "Gasabo"), ("Gasabo", "Kicukiro"), ("Kicukiro", "Bugesera"), ("Bugesera", "Rwamagana"),
|
| 38 |
("Rwamagana", "Kayonza"), ("Kayonza", "Kirehe"), ("Kirehe", "Ngoma"), ("Ngoma", "Gatsibo"),
|
|
|
|
| 42 |
("Ngororero", "Muhanga"), ("Muhanga", "Kamonyi"), ("Kamonyi", "Nyarugenge"), ("Muhanga", "Ruhango"),
|
| 43 |
("Ruhango", "Nyanza"), ("Nyanza", "Huye"), ("Huye", "Gisagara"), ("Gisagara", "Nyaruguru"),
|
| 44 |
("Nyaruguru", "Nyamagabe"), ("Nyamagabe", "Karongi"), ("Ngororero", "Ruhango"),
|
| 45 |
+
("Gicumbi", "Gasabo"), ("Bugesera", "Ngoma"),
|
| 46 |
+
|
| 47 |
+
# Kigali connections
|
| 48 |
+
("Kigali", "Nyarugenge"), ("Kigali", "Gasabo"), ("Kigali", "Kicukiro")
|
| 49 |
]
|
| 50 |
|
| 51 |
# 4. Create Graph
|
|
|
|
| 53 |
for u, v in edges:
|
| 54 |
G.add_edge(u, v, weight=haversine(places[u], places[v]))
|
| 55 |
|
| 56 |
+
# 5. Animation class
|
| 57 |
class AnimateMarker(MacroElement):
|
| 58 |
_template = Template("""
|
| 59 |
{% macro script(this, kwargs) %}
|
|
|
|
| 76 |
self._name = "AnimateMarker"
|
| 77 |
self.locations = locations
|
| 78 |
|
| 79 |
+
# 6. Routing Function
|
| 80 |
def generate_map(start, end):
|
| 81 |
if start == end:
|
| 82 |
return "Hitamo aho utangiriye n’aho ugiye bitandukanye.", ""
|
|
|
|
| 88 |
path = nx.astar_path(G, start, end, heuristic=lambda u, v: haversine(places[u], places[v]), weight='weight')
|
| 89 |
coords = [places[p] for p in path]
|
| 90 |
|
| 91 |
+
m = Map(location=[-1.9441, 30.0619], zoom_start=9)
|
| 92 |
for name, coord in places.items():
|
| 93 |
Marker(location=coord, popup=name).add_to(m)
|
| 94 |
|
|
|
|
| 99 |
except Exception as e:
|
| 100 |
return f"Ntibishoboka kubona inzira: {str(e)}", ""
|
| 101 |
|
| 102 |
+
# 7. Gradio Interface
|
| 103 |
iface = gr.Interface(
|
| 104 |
fn=generate_map,
|
| 105 |
inputs=[
|
|
|
|
| 115 |
)
|
| 116 |
|
| 117 |
iface.launch()
|
|
|