<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Multiple Routes Analysis
In this section, we are trying to answer a very interesting question
Step1:  Generate Random Routes
In order to accomplish this goal, we need to have a function that generates random points within some geospatial region. Hence the function get_random_point_in_polygon is created
Step2:  Now we simply copy the text above and go to https
Step4:  Use the web UI
Copy the above result and paste it in the web UI at /#/Main/RouteLab
Then, select a time interval, date interval and click query data. You may go to the Rethinkdb Web UI to make sure your query is actually getting executed.
When it's done, simpy click COPY TO CLIPBOARD button to copy the result.
Then create a test.json file and store it in the same directory as the this file.
Analyzing data
First load the json, and build an appropirate dataframe for it.
Step5:  Be Bold and try 100 routes
<ASSISTANT_TASK:>
Python Code:
## import system module
import json
import rethinkdb as r
import time
import datetime as dt
import asyncio
from shapely.geometry import Point, Polygon
import random
import pandas as pd
import os
import matplotlib.pyplot as plt
## import custom module
from streettraffic.server import TrafficServer
from streettraffic.predefined.cities import San_Francisco_polygon
settings = {
    'app_id': 'F8aPRXcW3MmyUvQ8Z3J9',  # this is where you put your App ID from here.com
    'app_code' : 'IVp1_zoGHdLdz0GvD_Eqsw', # this is where you put your App Code from here.com
    'map_tile_base_url': 'https://1.traffic.maps.cit.api.here.com/maptile/2.1/traffictile/newest/normal.day/',
    'json_tile_base_url': 'https://traffic.cit.api.here.com/traffic/6.2/flow.json?'
}
## initialize traffic server
server = TrafficServer(settings)
Explanation: Multiple Routes Analysis
In this section, we are trying to answer a very interesting question: within a city, do different routes experiences the same traffic pattern.
First let's import our moudles
End of explanation
def get_random_point_in_polygon(poly):
     (minx, miny, maxx, maxy) = poly.bounds
     while True:
         p = Point(random.uniform(minx, maxx), random.uniform(miny, maxy))
         if poly.contains(p):
             return p
atlanta_polygon = Polygon([[33.658529, -84.471782], [33.667928, -84.351730], [33.883809, -84.347570], [33.855681, -84.469405]])
sample_points = []
for i in range(100):
    point_in_poly = get_random_point_in_polygon(atlanta_polygon)
    sample_points += [[point_in_poly.x, point_in_poly.y]]
print(server.traffic_data.format_list_points_for_display(sample_points))
Explanation: Generate Random Routes
In order to accomplish this goal, we need to have a function that generates random points within some geospatial region. Hence the function get_random_point_in_polygon is created
End of explanation
sample_route_count = 2
route_obj_collection = []
for i in range(sample_route_count):
    point_in_poly1 = get_random_point_in_polygon(atlanta_polygon)
    point_in_poly2 = get_random_point_in_polygon(atlanta_polygon)
    route_obj_collection += [[
    {
        "lat": point_in_poly1.x,
        "lng": point_in_poly1.y
    },
    {
        "lat": point_in_poly2.x,
        "lng": point_in_poly2.y
    }
    ]]
    
route_obj_collection_json = json.dumps(route_obj_collection)
print(route_obj_collection_json)
Explanation: Now we simply copy the text above and go to https://www.darrinward.com/lat-long/ for plotting. The result would look like the following picture.
Now that we know we can generate random points, let's generate random routes. Let sample_route_count = 3, and we can create 3 random routes
End of explanation
# load the test.json
with open('test.json') as f:
    route_traffic_pattern_collection = json.load(f)
# create a function that takes an overview_path and generate the distance 
def overview_path_distance(overview_path):
    
    This function extracts the longest_n routes in route_traffic_pattern_collection
    
    distance = 0
    for i in range(len(overview_path)-1):
        point1 = overview_path[i]
        point2 = overview_path[i+1]
        distance += server.util.get_distance([point1['lat'], point1['lng']], [point2['lat'], point2['lng']])
    return distance
# now we build the dataframe
df = pd.DataFrame(index = [json.dumps(item['origin_destination']) for item in route_traffic_pattern_collection])
df['distance (in meters)'] = [overview_path_distance(item['route']['routes'][0]['overview_path']) for item in route_traffic_pattern_collection]
for i in range(len(route_traffic_pattern_collection[0]['chartLabel'])):
    df[route_traffic_pattern_collection[0]['chartLabel'][i]] = [item['chartData'][i] for item in route_traffic_pattern_collection]
df.sort_values(by='distance (in meters)')
df
# remove the 'distance (in meters)' column and then we can do analysis
del df['distance (in meters)']
df
# Now we can do all sorts of fun things with it. 
# feel free to comment out the following statement and see various possibilites
#print(df.mean(axis=1))
#print(df.std())
#print(df.median())
# for each route, give me the mean Jamming Factor of all the instant (2:00:00 PM, 2:30:00 PM, ..., 4:30:00 PM)
df.mean(axis=1)
Explanation: Use the web UI
Copy the above result and paste it in the web UI at /#/Main/RouteLab
Then, select a time interval, date interval and click query data. You may go to the Rethinkdb Web UI to make sure your query is actually getting executed.
When it's done, simpy click COPY TO CLIPBOARD button to copy the result.
Then create a test.json file and store it in the same directory as the this file.
Analyzing data
First load the json, and build an appropirate dataframe for it.
End of explanation
## For reproducibility, we executed the following script and store
## route_obj_collection_json
# sample_route_count = 100
# route_obj_collection = []
# for i in range(sample_route_count):
#     point_in_poly1 = get_random_point_in_polygon(atlanta_polygon)
#     point_in_poly2 = get_random_point_in_polygon(atlanta_polygon)
#     route_obj_collection += [[
#     {
#         "lat": point_in_poly1.x,
#         "lng": point_in_poly1.y
#     },
#     {
#         "lat": point_in_poly2.x,
#         "lng": point_in_poly2.y
#     }
#     ]]
# route_obj_collection_json = json.dumps(route_obj_collection)
with open('route_obj_collection_json.json') as f:
    route_obj_collection_json = json.load(f)
    
## after copying and pasting route_obj_collection_json into WEB UI, getting results and load 
## it in route_traffic_pattern_collection, we get this:
with open('route_traffic_pattern_collection.json') as f:
    route_traffic_pattern_collection = json.load(f)
    
df = pd.DataFrame(index = [json.dumps(item['origin_destination']) for item in route_traffic_pattern_collection])
df['distance (in meters)'] = [overview_path_distance(item['route']['routes'][0]['overview_path']) for item in route_traffic_pattern_collection]
for i in range(len(route_traffic_pattern_collection[0]['chartLabel'])):
    df[route_traffic_pattern_collection[0]['chartLabel'][i]] = [item['chartData'][i] for item in route_traffic_pattern_collection]
df2 = df.sort_values(by='distance (in meters)')
df2
# The following graph shows on average, what is the Jamming Factor throughout 24 hours for those 20 routes.
df3 = df2[-20:]
del df3['distance (in meters)']
df3.mean(axis=1).plot()
plt.show()
# The following graph extracts the worst jamming factor of each routes
df4 = df2[-20:]
del df4['distance (in meters)']
df4.max(axis=1).plot()
plt.show()
Explanation: Be Bold and try 100 routes
End of explanation
<END_TASK>