-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoute_OptimizationUsingAPI.py
More file actions
89 lines (79 loc) · 3.66 KB
/
Copy pathRoute_OptimizationUsingAPI.py
File metadata and controls
89 lines (79 loc) · 3.66 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import requests
import json
# Function to get real-time traffic data from TomTom API
def get_real_time_traffic_data(start_point, end_point):
API_KEY = 'YOUR_TOMTOM_API_KEY' # Replace with your TomTom API Key
url = f"https://api.tomtom.com/traffic/services/4/flowSegmentData/relative0/10/json?key={API_KEY}&point={start_point['lat']},{start_point['lon']}"
response = requests.get(url)
if response.status_code == 200:
traffic_data = response.json()
return traffic_data
else:
print(f"Error fetching traffic data: {response.status_code}")
return None
# Function to get route data from Google Maps Directions API
def get_route_data(start_point, end_point):
API_KEY = 'YOUR_GOOGLE_MAPS_API_KEY' # Replace with your Google Maps API Key
url = f"https://maps.googleapis.com/maps/api/directions/json?origin={start_point}&destination={end_point}&key={API_KEY}"
response = requests.get(url)
if response.status_code == 200:
route_data = response.json()
return route_data
else:
print(f"Error fetching route data: {response.status_code}")
return None
# Function to get air quality and weather data from AQICN API
def get_weather_data(city):
API_KEY = 'YOUR_AQICN_API_KEY' # Replace with your AQICN API Key
url = f"https://api.waqi.info/feed/{city}/?token={API_KEY}"
response = requests.get(url)
if response.status_code == 200:
weather_data = response.json()
return weather_data
else:
print(f"Error fetching weather data: {response.status_code}")
return None
# Function to calculate vehicle emissions based on distance and vehicle efficiency
def calculate_emissions(distance, fuel_efficiency, emission_factor):
emissions = (distance / fuel_efficiency) * emission_factor
return emissions
# Main function for user input and routing optimization
def main():
# Get user input for vehicle details and route
start_point = {
"lat": float(input("Enter start point latitude: ")),
"lon": float(input("Enter start point longitude: "))
}
end_point = {
"lat": float(input("Enter end point latitude: ")),
"lon": float(input("Enter end point longitude: "))
}
city = input("Enter your city for weather and air quality data: ")
vehicle_type = input("Enter vehicle type (gas/electric): ")
fuel_efficiency = float(input("Enter fuel efficiency (km per litre): "))
emission_factor = 2.31 # gCO2 per litre (standard for gasoline vehicles)
# Fetch real-time traffic data
traffic_data = get_real_time_traffic_data(start_point, end_point)
if traffic_data:
print("Traffic data fetched successfully.")
# Fetch route data
route_data = get_route_data(f"{start_point['lat']},{start_point['lon']}", f"{end_point['lat']},{end_point['lon']}")
if route_data:
print("Route data fetched successfully.")
routes = route_data.get('routes', [])
if routes:
route = routes[0]
distance = route['legs'][0]['distance']['value'] / 1000 # Convert meters to kilometers
print(f"Route distance: {distance} km")
else:
print("No routes found.")
return
# Fetch weather and air quality data
weather_data = get_weather_data(city)
if weather_data:
print(f"Weather data: {weather_data['data']['aqi']} AQI")
# Calculate emissions for the route
emissions = calculate_emissions(distance, fuel_efficiency, emission_factor)
print(f"Estimated CO2 emissions for the route: {emissions} grams")
if __name__ == "__main__":
main()