#!/usr/bin/env python # coding: utf-8 # In[1]: import pandas as pd import folium import utils # In[2]: # Load the data ListA = pd.read_csv('List A.csv') ListB = pd.read_csv('List B.csv') ListC = pd.read_csv('List C.csv') ListD = pd.read_csv('List D.csv') # In[3]: # Create two centroids, one in the North End and one in the Seaport District centroids = [[42.365, -71.054], [42.351, -71.045]] northeastern_coordinate = "-71.09033,42.33976" # In[4]: # Combine the two lists and add a column to indicate the list ListA['list'] = 'A' ListB['list'] = 'B' ListC['list'] = 'C' ListD['list'] = 'D' TotalList = pd.concat([ListA, ListB, ListC, ListD]) # In[5]: # Remove all columns but name and gps TotalList = TotalList[['name', 'gps', 'list']] # In[6]: # Convert the gps column to a list of lists for k-means TotalList['gps'] = TotalList['gps'].apply(lambda x: x.strip('[]').split(',')) TotalList['gps'] = TotalList['gps'].apply(lambda x: [float(i) for i in x]) # In[7]: display(TotalList) # # 2 Routes # ## Cluster and Minimize # In[8]: # Cluster and minimize the data _, routes = utils.cluster_and_optimize(TotalList, centroids, northeastern_coordinate, time_diff=0.25, max_time=24) route_1_coordinates = routes[0] route_2_coordinates = routes[1] # ## Create JSON # In[9]: # Create a JSON request for the API # This is the data we want to get from the API route_1 = utils.list_to_string(route_1_coordinates) route_2 = utils.list_to_string(route_2_coordinates) # In[10]: # Create a dataframe from the JSON df1 = utils.create_json_df(route_1, utils.list_to_string([centroids[0]]), northeastern_coordinate) df2 = utils.create_json_df(route_2, utils.list_to_string([centroids[1]]), northeastern_coordinate) # In[11]: # Add columns for the route number df1['route'] = 1 df2['route'] = 2 # Concatenate the two dataframes df = pd.concat([df1, df2], ignore_index=True) # In[12]: display(df) # ## Map # In[13]: # Create a map m = folium.Map(location=[df['lon'].mean(), df['lat'].mean()], zoom_start=11) # Add the points and lines for the two routes with different colors colors = ['red', 'blue'] for route in df['route'].unique(): df_route = df[df['route'] == route] folium.PolyLine(df_route[['lon', 'lat']].values.tolist(), color=colors[route - 1]).add_to(m) for i in range(len(df_route)): folium.CircleMarker(df_route[['lon', 'lat']].iloc[i].values.tolist(), radius=3, color=colors[route - 1]).add_to( m) # Display the map m # ## Results # In[14]: # Get the number of waypoints for each route route_1_waypoints = len(route_1_coordinates) route_2_waypoints = len(route_2_coordinates) print("Route 1 has {} waypoints".format(route_1_waypoints)) print("Route 2 has {} waypoints".format(route_2_waypoints)) # In[15]: trip_hrs_1 = utils.get_trip_time(route_1, route_1_waypoints, utils.list_to_string([centroids[0]]), northeastern_coordinate) print("The trip will take {} hours".format(trip_hrs_1)) trip_hrs_2 = utils.get_trip_time(route_2, route_2_waypoints, utils.list_to_string([centroids[1]]), northeastern_coordinate) print("The trip will take {} hours".format(trip_hrs_2)) # # 3 Routes # In[16]: # Cluster and minimize the data # Add a third centroid in the Financial District centroids.append([42.356, -71.055]) _, routes = utils.cluster_and_optimize(TotalList, centroids, northeastern_coordinate, time_diff=0.3, max_time=24) route_1_coordinates = routes[0] route_2_coordinates = routes[1] route_3_coordinates = routes[2] # ## Create JSON # In[17]: # Create a JSON request for the API # This is the data we want to get from the API route_1 = utils.list_to_string(route_1_coordinates) route_2 = utils.list_to_string(route_2_coordinates) route_3 = utils.list_to_string(route_3_coordinates) # In[18]: # Create a dataframe from the JSON df1 = utils.create_json_df(route_1, utils.list_to_string([centroids[0]]), northeastern_coordinate) df2 = utils.create_json_df(route_2, utils.list_to_string([centroids[1]]), northeastern_coordinate) df3 = utils.create_json_df(route_3, utils.list_to_string([centroids[2]]), northeastern_coordinate) # In[19]: # Add columns for the route number df1['route'] = 1 df2['route'] = 2 df3['route'] = 3 # Concatenate the three dataframes df = pd.concat([df1, df2, df3], ignore_index=True) # In[20]: display(df) # ## Map # In[21]: # Create a map m = folium.Map(location=[df['lon'].mean(), df['lat'].mean()], zoom_start=11) # Add the points and lines for the three routes with different colors colors = ['red', 'blue', 'green'] for route in df['route'].unique(): df_route = df[df['route'] == route] folium.PolyLine(df_route[['lon', 'lat']].values.tolist(), color=colors[route - 1]).add_to(m) for i in range(len(df_route)): folium.CircleMarker(df_route[['lon', 'lat']].iloc[i].values.tolist(), radius=3, color=colors[route - 1]).add_to( m) # Display the map m # ## Results # In[22]: # Get the number of waypoints for each route route_1_waypoints = len(route_1_coordinates) route_2_waypoints = len(route_2_coordinates) route_3_waypoints = len(route_3_coordinates) print("Route 1 has {} waypoints".format(route_1_waypoints)) print("Route 2 has {} waypoints".format(route_2_waypoints)) print("Route 3 has {} waypoints".format(route_3_waypoints)) # In[23]: # Get the trip time for each route trip_hrs_1 = utils.get_trip_time(route_1, route_1_waypoints, utils.list_to_string([centroids[0]]), northeastern_coordinate) print("The trip will take {} hours".format(trip_hrs_1)) trip_hrs_2 = utils.get_trip_time(route_2, route_2_waypoints, utils.list_to_string([centroids[1]]), northeastern_coordinate) print("The trip will take {} hours".format(trip_hrs_2)) trip_hrs_3 = utils.get_trip_time(route_3, route_3_waypoints, utils.list_to_string([centroids[2]]), northeastern_coordinate) print("The trip will take {} hours".format(trip_hrs_3)) # # 10 ROUTES (because I can) # In[24]: # Cluster and minimize the data # Add seven more centroids around Boston with different latitudes and longitudes for i in range(7): centroids.append([42.365 + i * 0.01, -71.054 + i * 0.01]) _, routes = utils.cluster_and_optimize(TotalList, centroids, northeastern_coordinate, time_diff=0.5, max_time=24) # ## Create JSON # In[25]: # Create a JSON request for the API # This is the data we want to get from the API route_strings = [] for route in routes: route_strings.append(utils.list_to_string(route)) # In[26]: # Create a dataframe from the JSON dfs = [] for i in range(len(routes)): dfs.append(utils.create_json_df(route_strings[i], utils.list_to_string([centroids[i]]), northeastern_coordinate)) # Concatenate the dataframes df = pd.concat(dfs, ignore_index=True) # In[30]: # Add columns for the route number for i in range(len(routes)): df['route'].iloc[i * len(routes[i]):(i + 1) * len(routes[i])] = i + 1 # In[31]: # Display the dataframe display(df) # ## Map # In[37]: # Create a map m = folium.Map(location=[df['lon'].mean(), df['lat'].mean()], zoom_start=11) # Add the points and lines for the three routes with different colors colors = ['red', 'blue', 'green', 'orange', 'purple', 'pink', 'black', 'gray', 'brown', 'yellow'] for route in df['route'].unique(): df_route = df[df['route'] == route] folium.PolyLine(df_route[['lon', 'lat']].values.tolist(), color=colors[route - 1]).add_to(m) for i in range(len(df_route)): folium.CircleMarker(df_route[['lon', 'lat']].iloc[i].values.tolist(), radius=3, color=colors[route - 1]).add_to( m) # Display the map m # ## Results # In[36]: # Get the number of waypoints for each route route_waypoints = [] for route in routes: route_waypoints.append(len(route)) for i in range(len(route_waypoints)): print("Route {} has {} waypoints".format(i + 1, route_waypoints[i])) # In[34]: # Get the trip time for each route trip_hrs = [] for i in range(len(routes)): trip_hrs.append(utils.get_trip_time(route_strings[i], route_waypoints[i], utils.list_to_string([centroids[i]]), northeastern_coordinate)) for i in range(len(trip_hrs)): print("The trip will take {} hours".format(trip_hrs[i])) # In[ ]: