File size: 2,622 Bytes
b92f7cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import requests
import json

import pandas as pd

import gmplot
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

import os
from dotenv import load_dotenv

# Load the .env file
load_dotenv()


lta_data_mall_token = os.getenv('LTA_DATAMALL_API_KEY')

headers = {
  'AccountKey': lta_data_mall_token
}

payload = {}


def get_lta_data(url_dict, headers=headers, payload=payload):

    for key, url in url_dict.items():

        response = requests.request("GET", url, headers=headers, data=payload)

        json_data = response.text

        data_dict = json.loads(json_data)

        if key == "Incidents":
            incidents_df = pd.DataFrame(data_dict['value'])
        elif key == "SpeedBands":
            speedbands_df = pd.DataFrame(data_dict['value'])
    
    return incidents_df, speedbands_df

def zip_columns(row):
    '''
        Function to zip values from four columns
    '''
    return list(zip([row['StartLat'], row['StartLon']], [row['EndLat'], row['EndLon']]))


# Define color mapping function
def get_color_from_value(value):
    '''
        This function allows you to change the colors of the plotted points on Google Map.

        For example, you could use a gradient scale based on value ranges
        Return the color based on your logic
    '''
    # Example: Mapping values from 0 to 100 to a gradient scale from red to green
    min_value = 0
    max_value = 1

    # Can be used with color gradient
    normalized_value = (value - min_value) / (max_value - min_value)  # Normalize value between 0 and 1

    if normalized_value  <= 0.5:
        return '#ffff00'  # Yellow color for values less than or equal to 0
    elif normalized_value  >= 0.5:
        return '#ff0000'  # Red color for values greater than or equal to 1
    else:
        return '#0000ff'  # Blue color for other values


def plot_polygon_with_speed(polygons, speeds, gmap, map='map.html'):

    # Define a custom color map from red to blue
    colors = [(1, 0, 0), (0, 0, 1)]  # Red to Blue
    cmap = LinearSegmentedColormap.from_list("CustomMap", colors, N=8)

    # Plot polygons with corresponding colors based on speed
    for polygon, speed in zip(polygons, speeds):
        color = cmap(speed / 8)  # Map speed to color gradient
        # Convert latitudes and longitudes to floats
        lats = [float(polygon[0][0]), float(polygon[0][1])]
        lngs = [float(polygon[1][0]), float(polygon[1][1])]

        gmap.polygon(lats, lngs, color='#%02x%02x%02x' % (int(color[0]*255), int(color[1]*255), int(color[2]*255)), edge_width=2)

    # Draw the map
    gmap.draw(map)