From 49e76ecd1aa46c2dcd9797e8f5d303a6394002d4 Mon Sep 17 00:00:00 2001 From: dgarciaperales Date: Mon, 14 Apr 2025 00:57:33 -0400 Subject: [PATCH 1/2] inserted traffic_api.py to be used as a module in bus_extraction.py --- bus_extraction.py | 2 ++ traffic_api.py | 85 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 traffic_api.py diff --git a/bus_extraction.py b/bus_extraction.py index d604a127f..6e0642766 100644 --- a/bus_extraction.py +++ b/bus_extraction.py @@ -5,6 +5,8 @@ from datetime import datetime, timedelta import re import psycopg2 +from traffic_api import get_traffic_level + # Create an unverified SSL context to bypass SSL certificate errors try: diff --git a/traffic_api.py b/traffic_api.py new file mode 100644 index 000000000..6718769fe --- /dev/null +++ b/traffic_api.py @@ -0,0 +1,85 @@ +import requests +import pytz +from datetime import datetime +import random + +TRAFFIC_API_KEY = "AIzaSyCeOi3OLyGCSnYNgLAeU9UBMWiT6JyMt2Y" # Replace with your actual key + +def get_traffic_level(origin, destination): + url = "https://maps.googleapis.com/maps/api/distancematrix/json" + + params = { + "origins": f"{origin[0]},{origin[1]}", # Convert tuple to lat,lng format + "destinations": f"{destination[0]},{destination[1]}", # Convert tuple to lat,lng format + "departure_time": 'now', + "traffic_model": "best_guess", + "key": TRAFFIC_API_KEY + } + + try: + response = requests.get(url, params=params) + response.raise_for_status() # Raise an error for bad status codes (e.g., 400, 500) + data = response.json() + + # Check if the API returns valid data + if data.get("status") != "OK": + print("Error: API response status is not OK") + return None + + element = data["rows"][0]["elements"][0] + normal_duration = element["duration"]["value"] + traffic_duration = element.get("duration_in_traffic", {}).get("value", normal_duration) + + if origin == destination: + return "Origin and destination are the same." + + if traffic_duration <= normal_duration: + #print('Low') + return "Low" + elif traffic_duration <= 1.3 * normal_duration: + #print('Medium') + return "Medium" + else: + #print('High') + return "High" + + except requests.exceptions.RequestException as e: + print(f"Error making API request: {e}") + return None + + +stops_dict = { + "Marta": (33.78124531457225, -84.38609515855521), + "Technology square": (33.77693168042234, -84.38985431852313), + "5th Street WB": (33.77696524446127, -84.39177713175643), + "Russ Chandler Stadium": (33.77700226733594, -84.39405232716865), + "Klaus Building WB": (33.77750297477602, -84.3955435318594), + "Nanotechnology": (33.778361110340406, -84.39806140119877), + "Kendeda Building": (33.7784220826717, -84.39957983900081), + "Couch Park": (33.77804350814352, -84.4020813989256), + "CRC & Stamps Health": (33.7750633043881, -84.40272409315902), + "Ferst Drive & Campus Center": (33.7733218207678, -84.39921557992542), + "Transit Hub": (33.77314438101725, -84.39700294107278), + "Campus Center": (33.77347874294925, -84.3991488920771), + "Exhibition Hall": (33.77508233091718, -84.40238068190583), + "Ferst Dr & Hemphill Ave": (33.778433040576374, -84.40085640923526), + "Cherry Emerson": (33.77822274400314, -84.3973364838895), + "Klaus Building EB": (33.77715065747476, -84.39554176833916), + "Ferst Dr & Fowler St": (33.776870955985096, -84.39380500062757), + "5th Street Bridge EB": (33.7768316079988, -84.39190623554501), + "Technology Square EB": (33.77679019002961, -84.38972625670338), + "College of Business": (33.77675787854372, -84.38777167574695), + "Academy of Medicine": (33.77848885105469, -84.38719153190587) +} + +origin_stop = random.choice(list(stops_dict.keys())) +origin_coords = stops_dict[origin_stop] +print(origin_stop) + +destination_stop = random.choice(list(stops_dict.keys())) +destination_coords = stops_dict[destination_stop] +print(destination_stop) + +print(get_traffic_level(origin_coords, destination_coords)) + + From 04931bc424a60dd6c6c0af0430165fca892aa2b4 Mon Sep 17 00:00:00 2001 From: dgarciaperales Date: Mon, 14 Apr 2025 01:17:40 -0400 Subject: [PATCH 2/2] added traffic changes to main file --- bus_extraction.py | 42 +++++++++++++++++++++++++++++++++++++++++- traffic_api.py | 41 +++-------------------------------------- 2 files changed, 44 insertions(+), 39 deletions(-) diff --git a/bus_extraction.py b/bus_extraction.py index 6e0642766..f37f060ed 100644 --- a/bus_extraction.py +++ b/bus_extraction.py @@ -5,6 +5,7 @@ from datetime import datetime, timedelta import re import psycopg2 +import random from traffic_api import get_traffic_level @@ -76,6 +77,45 @@ vehicles = json.loads(data.decode("utf-8")) +# sample stops for Gold route +stops_dict = { + "Marta": (33.78124531457225, -84.38609515855521), + "Technology square": (33.77693168042234, -84.38985431852313), + "5th Street WB": (33.77696524446127, -84.39177713175643), + "Russ Chandler Stadium": (33.77700226733594, -84.39405232716865), + "Klaus Building WB": (33.77750297477602, -84.3955435318594), + "Nanotechnology": (33.778361110340406, -84.39806140119877), + "Kendeda Building": (33.7784220826717, -84.39957983900081), + "Couch Park": (33.77804350814352, -84.4020813989256), + "CRC & Stamps Health": (33.7750633043881, -84.40272409315902), + "Ferst Drive & Campus Center": (33.7733218207678, -84.39921557992542), + "Transit Hub": (33.77314438101725, -84.39700294107278), + "Campus Center": (33.77347874294925, -84.3991488920771), + "Exhibition Hall": (33.77508233091718, -84.40238068190583), + "Ferst Dr & Hemphill Ave": (33.778433040576374, -84.40085640923526), + "Cherry Emerson": (33.77822274400314, -84.3973364838895), + "Klaus Building EB": (33.77715065747476, -84.39554176833916), + "Ferst Dr & Fowler St": (33.776870955985096, -84.39380500062757), + "5th Street Bridge EB": (33.7768316079988, -84.39190623554501), + "Technology Square EB": (33.77679019002961, -84.38972625670338), + "College of Business": (33.77675787854372, -84.38777167574695), + "Academy of Medicine": (33.77848885105469, -84.38719153190587) +} + +# random origin stop choice +origin_stop = random.choice(list(stops_dict.keys())) +# its coordinates +origin_coords = stops_dict[origin_stop] +#print(origin_stop) + +# random destination stop choice +destination_stop = random.choice(list(stops_dict.keys())) +# its coordinates +destination_coords = stops_dict[destination_stop] +#print(destination_stop) + +#print(get_traffic_level(origin_coords, destination_coords)) + bus_data = [] "RouteID" for vehicle in vehicles: @@ -110,7 +150,7 @@ "Month": month, "Time_Of_Day": time, "Bus_Line": vehicle["RouteID"], - "Traffic_Patterns": "N/A", + "Traffic_Patterns": get_traffic_level(origin_coords, destination_coords), "Bus_Speed": vehicle["GroundSpeed"] }) diff --git a/traffic_api.py b/traffic_api.py index 6718769fe..e06d90bd8 100644 --- a/traffic_api.py +++ b/traffic_api.py @@ -3,8 +3,10 @@ from datetime import datetime import random -TRAFFIC_API_KEY = "AIzaSyCeOi3OLyGCSnYNgLAeU9UBMWiT6JyMt2Y" # Replace with your actual key +TRAFFIC_API_KEY = "key" # Replace with Traffic API key in google cloud platform starting with AIza + +# upon calling should return a level of traffic - low, medium, high OR a message indicating the origin and destination are the same def get_traffic_level(origin, destination): url = "https://maps.googleapis.com/maps/api/distancematrix/json" @@ -46,40 +48,3 @@ def get_traffic_level(origin, destination): except requests.exceptions.RequestException as e: print(f"Error making API request: {e}") return None - - -stops_dict = { - "Marta": (33.78124531457225, -84.38609515855521), - "Technology square": (33.77693168042234, -84.38985431852313), - "5th Street WB": (33.77696524446127, -84.39177713175643), - "Russ Chandler Stadium": (33.77700226733594, -84.39405232716865), - "Klaus Building WB": (33.77750297477602, -84.3955435318594), - "Nanotechnology": (33.778361110340406, -84.39806140119877), - "Kendeda Building": (33.7784220826717, -84.39957983900081), - "Couch Park": (33.77804350814352, -84.4020813989256), - "CRC & Stamps Health": (33.7750633043881, -84.40272409315902), - "Ferst Drive & Campus Center": (33.7733218207678, -84.39921557992542), - "Transit Hub": (33.77314438101725, -84.39700294107278), - "Campus Center": (33.77347874294925, -84.3991488920771), - "Exhibition Hall": (33.77508233091718, -84.40238068190583), - "Ferst Dr & Hemphill Ave": (33.778433040576374, -84.40085640923526), - "Cherry Emerson": (33.77822274400314, -84.3973364838895), - "Klaus Building EB": (33.77715065747476, -84.39554176833916), - "Ferst Dr & Fowler St": (33.776870955985096, -84.39380500062757), - "5th Street Bridge EB": (33.7768316079988, -84.39190623554501), - "Technology Square EB": (33.77679019002961, -84.38972625670338), - "College of Business": (33.77675787854372, -84.38777167574695), - "Academy of Medicine": (33.77848885105469, -84.38719153190587) -} - -origin_stop = random.choice(list(stops_dict.keys())) -origin_coords = stops_dict[origin_stop] -print(origin_stop) - -destination_stop = random.choice(list(stops_dict.keys())) -destination_coords = stops_dict[destination_stop] -print(destination_stop) - -print(get_traffic_level(origin_coords, destination_coords)) - -