amosfang commited on
Commit
b92f7cb
·
verified ·
1 Parent(s): e879d98

Create utils.py

Browse files
Files changed (1) hide show
  1. utils.py +89 -0
utils.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import json
3
+
4
+ import pandas as pd
5
+
6
+ import gmplot
7
+ import matplotlib.pyplot as plt
8
+ from matplotlib.colors import LinearSegmentedColormap
9
+
10
+ import os
11
+ from dotenv import load_dotenv
12
+
13
+ # Load the .env file
14
+ load_dotenv()
15
+
16
+
17
+ lta_data_mall_token = os.getenv('LTA_DATAMALL_API_KEY')
18
+
19
+ headers = {
20
+ 'AccountKey': lta_data_mall_token
21
+ }
22
+
23
+ payload = {}
24
+
25
+
26
+ def get_lta_data(url_dict, headers=headers, payload=payload):
27
+
28
+ for key, url in url_dict.items():
29
+
30
+ response = requests.request("GET", url, headers=headers, data=payload)
31
+
32
+ json_data = response.text
33
+
34
+ data_dict = json.loads(json_data)
35
+
36
+ if key == "Incidents":
37
+ incidents_df = pd.DataFrame(data_dict['value'])
38
+ elif key == "SpeedBands":
39
+ speedbands_df = pd.DataFrame(data_dict['value'])
40
+
41
+ return incidents_df, speedbands_df
42
+
43
+ def zip_columns(row):
44
+ '''
45
+ Function to zip values from four columns
46
+ '''
47
+ return list(zip([row['StartLat'], row['StartLon']], [row['EndLat'], row['EndLon']]))
48
+
49
+
50
+ # Define color mapping function
51
+ def get_color_from_value(value):
52
+ '''
53
+ This function allows you to change the colors of the plotted points on Google Map.
54
+
55
+ For example, you could use a gradient scale based on value ranges
56
+ Return the color based on your logic
57
+ '''
58
+ # Example: Mapping values from 0 to 100 to a gradient scale from red to green
59
+ min_value = 0
60
+ max_value = 1
61
+
62
+ # Can be used with color gradient
63
+ normalized_value = (value - min_value) / (max_value - min_value) # Normalize value between 0 and 1
64
+
65
+ if normalized_value <= 0.5:
66
+ return '#ffff00' # Yellow color for values less than or equal to 0
67
+ elif normalized_value >= 0.5:
68
+ return '#ff0000' # Red color for values greater than or equal to 1
69
+ else:
70
+ return '#0000ff' # Blue color for other values
71
+
72
+
73
+ def plot_polygon_with_speed(polygons, speeds, gmap, map='map.html'):
74
+
75
+ # Define a custom color map from red to blue
76
+ colors = [(1, 0, 0), (0, 0, 1)] # Red to Blue
77
+ cmap = LinearSegmentedColormap.from_list("CustomMap", colors, N=8)
78
+
79
+ # Plot polygons with corresponding colors based on speed
80
+ for polygon, speed in zip(polygons, speeds):
81
+ color = cmap(speed / 8) # Map speed to color gradient
82
+ # Convert latitudes and longitudes to floats
83
+ lats = [float(polygon[0][0]), float(polygon[0][1])]
84
+ lngs = [float(polygon[1][0]), float(polygon[1][1])]
85
+
86
+ gmap.polygon(lats, lngs, color='#%02x%02x%02x' % (int(color[0]*255), int(color[1]*255), int(color[2]*255)), edge_width=2)
87
+
88
+ # Draw the map
89
+ gmap.draw(map)