Spaces:
Sleeping
Sleeping
Update utils/utils.py
Browse files- utils/utils.py +19 -20
utils/utils.py
CHANGED
@@ -10,7 +10,7 @@ from ipyleaflet import AwesomeIcon
|
|
10 |
|
11 |
from shiny.express import ui
|
12 |
|
13 |
-
from utils.config import
|
14 |
|
15 |
|
16 |
# Cache expires after 1 week
|
@@ -40,9 +40,9 @@ def get_bounds(country: str) -> Tuple[float]:
|
|
40 |
|
41 |
|
42 |
@cached(cache=TTLCache(maxsize=3000, ttl=ONE_WEEK_SEC)) # Memory
|
43 |
-
def
|
44 |
"""
|
45 |
-
The road distance calculated using
|
46 |
or optimal road distance based on the available road data and routing algorithm.
|
47 |
|
48 |
origin is a tuple of lat, lon
|
@@ -51,28 +51,27 @@ def ops_trip_distance(origin: tuple, destination: tuple) -> float:
|
|
51 |
Returns: the calculiated trip distance or a default value
|
52 |
"""
|
53 |
|
54 |
-
#
|
55 |
-
|
56 |
-
url = 'https://api.openrouteservice.org/v2/directions/driving-car'
|
57 |
|
58 |
-
# Request parameters
|
59 |
-
params = {
|
60 |
-
'api_key': ors_api_key,
|
61 |
-
'start': f'{origin[1]},{origin[0]}', # lon, lat
|
62 |
-
'end': f'{destination[1]},{destination[0]}' # lon, lat
|
63 |
-
}
|
64 |
|
65 |
# Send request
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
#
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
74 |
else:
|
75 |
distance = TRIP_DISTANCE # Default
|
|
|
76 |
back_to_nairobi()
|
77 |
|
78 |
return distance
|
|
|
10 |
|
11 |
from shiny.express import ui
|
12 |
|
13 |
+
from utils.config import MAPS_API_KEY, BRANDCOLORS, BASEMAPS, ONE_WEEK_SEC, LOCATIONS, TRIP_DISTANCE
|
14 |
|
15 |
|
16 |
# Cache expires after 1 week
|
|
|
40 |
|
41 |
|
42 |
@cached(cache=TTLCache(maxsize=3000, ttl=ONE_WEEK_SEC)) # Memory
|
43 |
+
def google_maps_trip_distance(origin: tuple, destination: tuple) -> float:
|
44 |
"""
|
45 |
+
The road distance calculated using Google Maps distance matrix api with the driving car is the shortest
|
46 |
or optimal road distance based on the available road data and routing algorithm.
|
47 |
|
48 |
origin is a tuple of lat, lon
|
|
|
51 |
Returns: the calculiated trip distance or a default value
|
52 |
"""
|
53 |
|
54 |
+
# Google Maps API URL
|
55 |
+
url = f"https://maps.googleapis.com/maps/api/distancematrix/json?origins={origin[0]},{origin[1]}&destinations={destination[0]},{destination[1]}&key={MAPS_API_KEY}"
|
|
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
# Send request
|
59 |
+
response = requests.get(url)
|
60 |
+
|
61 |
+
if response.status_code == 200:
|
62 |
+
# Decode the response
|
63 |
+
data = response.json()
|
64 |
+
|
65 |
+
# Extract distance information
|
66 |
+
if "rows" in data and len(data["rows"]) > 0:
|
67 |
+
distance_info = data["rows"][0]["elements"][0]["distance"]
|
68 |
+
distance = float(distance_info['value'])
|
69 |
+
else:
|
70 |
+
distance = TRIP_DISTANCE # Default
|
71 |
+
back_to_nairobi()
|
72 |
else:
|
73 |
distance = TRIP_DISTANCE # Default
|
74 |
+
# print(response.status_code)
|
75 |
back_to_nairobi()
|
76 |
|
77 |
return distance
|