summaryrefslogtreecommitdiff
path: root/utils.py
blob: 880dd2a7a15b73ca068685afbea6af74e9cf514b (plain)
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
import folium
import pandas as pd
import requests


# make a function that turns a list of lists of coordinates into a string
def list_to_string(list_of_lists):
    """
    Takes a list of lists of coordinates and returns a string of the coordinates
    """
    string = ''
    for i in list_of_lists:
        string += str(i[1]) + ',' + str(i[0]) + ';'
    return string


def create_json_df(coordinate_string):
    coordinates = requests.get('http://acetyl.net:5000/trip/v1/bike/' + coordinate_string)
    coordinates = coordinates.json()

    # Create a dataframe from the JSON
    df = pd.DataFrame(coordinates['waypoints'])

    # Separate the location column into lon and lat columns
    df['lat'] = df['location'].apply(lambda x: x[0])
    df['lon'] = df['location'].apply(lambda x: x[1])

    df['waypoint_index'] = df['waypoint_index'].astype(int)

    # Map out the waypoints in order of the waypoint index
    df = df.sort_values(by=['waypoint_index'])

    return df


def get_trip_time(coordinate_string):
    """
    Takes a list of lists of coordinates and returns the time of the trip in hours
    """
    coordinates = requests.get('http://acetyl.net:5000/trip/v1/bike/' + coordinate_string)
    coordinates = coordinates.json()

    return int(coordinates['trips'][0]['duration']) / 3600