Spaces:
Runtime error
Runtime error
Create gps_tracker.py
Browse files- gps_tracker.py +69 -0
gps_tracker.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, jsonify
|
2 |
+
import folium
|
3 |
+
from gps_tracker import GPSTracker
|
4 |
+
from route_optimizer import RouteOptimizer
|
5 |
+
from schedule_optimizer import ScheduleOptimizer
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
|
9 |
+
# Initialize our components
|
10 |
+
gps_tracker = GPSTracker()
|
11 |
+
destinations = [(40.7128, -74.0060), (34.0522, -118.2437), (51.5074, -0.1278)] # Example destinations
|
12 |
+
|
13 |
+
@app.route('/')
|
14 |
+
def transportation_dashboard():
|
15 |
+
# Create map centered on New York City
|
16 |
+
m = folium.Map(location=[40.7128, -74.0060], zoom_start=12)
|
17 |
+
|
18 |
+
# Get current vehicle locations
|
19 |
+
vehicles = gps_tracker.get_vehicle_locations()
|
20 |
+
|
21 |
+
# Add markers for each vehicle
|
22 |
+
for vehicle_id, (lat, lon) in vehicles.items():
|
23 |
+
folium.Marker([lat, lon], popup=vehicle_id).add_to(m)
|
24 |
+
|
25 |
+
# Optimize routes
|
26 |
+
route_optimizer = RouteOptimizer(list(vehicles.keys()), destinations)
|
27 |
+
optimized_routes = route_optimizer.optimize()
|
28 |
+
|
29 |
+
# Add polylines for optimized routes
|
30 |
+
for vehicle, destination in optimized_routes:
|
31 |
+
vehicle_location = vehicles[vehicle]
|
32 |
+
folium.PolyLine([vehicle_location, destination], color="red", weight=2.5, opacity=1).add_to(m)
|
33 |
+
|
34 |
+
# Optimize schedules
|
35 |
+
vehicle_schedules = {vehicle: [1, 2, 3, 4, 5] for vehicle in vehicles} # Example schedules
|
36 |
+
schedule_optimizer = ScheduleOptimizer(vehicle_schedules)
|
37 |
+
optimized_schedules = schedule_optimizer.optimize()
|
38 |
+
|
39 |
+
return render_template('dashboard.html', map=m._repr_html_(), schedules=optimized_schedules)
|
40 |
+
|
41 |
+
@app.route('/update', methods=['POST'])
|
42 |
+
def update():
|
43 |
+
# Simulate movement of vehicles
|
44 |
+
gps_tracker.simulate_movement()
|
45 |
+
|
46 |
+
# Get updated vehicle locations
|
47 |
+
updated_locations = gps_tracker.get_vehicle_locations()
|
48 |
+
|
49 |
+
# Re-optimize routes based on new locations
|
50 |
+
route_optimizer = RouteOptimizer(list(updated_locations.keys()), destinations)
|
51 |
+
new_routes = route_optimizer.optimize()
|
52 |
+
|
53 |
+
# Re-optimize schedules
|
54 |
+
vehicle_schedules = {vehicle: [1, 2, 3, 4, 5] for vehicle in updated_locations} # Example schedules
|
55 |
+
schedule_optimizer = ScheduleOptimizer(vehicle_schedules)
|
56 |
+
new_schedules = schedule_optimizer.optimize()
|
57 |
+
|
58 |
+
return jsonify({
|
59 |
+
'locations': updated_locations,
|
60 |
+
'routes': new_routes,
|
61 |
+
'schedules': new_schedules
|
62 |
+
})
|
63 |
+
|
64 |
+
if __name__ == '__main__':
|
65 |
+
# Initialize some vehicles
|
66 |
+
gps_tracker.add_vehicle('Vehicle1')
|
67 |
+
gps_tracker.add_vehicle('Vehicle2')
|
68 |
+
|
69 |
+
app.run(debug=True)
|