From 23342ac8ac75dcce2f7b5ef2c26d5e29adeab750 Mon Sep 17 00:00:00 2001 From: itsGarrin Date: Sun, 5 Nov 2023 20:27:58 -0500 Subject: Continued work on the Zesty Salesman algorithm --- utils.py | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) (limited to 'utils.py') diff --git a/utils.py b/utils.py index f250a9c..880dd2a 100644 --- a/utils.py +++ b/utils.py @@ -1,5 +1,9 @@ -# make a function that turns a list of lists of coordinates into a string +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 @@ -7,4 +11,33 @@ def list_to_string(list_of_lists): string = '' for i in list_of_lists: string += str(i[1]) + ',' + str(i[0]) + ';' - return string \ No newline at end of file + 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 -- cgit v1.2.3