Spaces:
Runtime error
Runtime error
| from flask import Flask, render_template, jsonify | |
| import folium | |
| from gps_tracker import GPSTracker | |
| from route_optimizer import RouteOptimizer | |
| from schedule_optimizer import ScheduleOptimizer | |
| app = Flask(__name__) | |
| # Initialize our components | |
| gps_tracker = GPSTracker() | |
| destinations = [(40.7128, -74.0060), (34.0522, -118.2437), (51.5074, -0.1278)] # Example destinations | |
| def transportation_dashboard(): | |
| # Create map centered on New York City | |
| m = folium.Map(location=[40.7128, -74.0060], zoom_start=12) | |
| # Get current vehicle locations | |
| vehicles = gps_tracker.get_vehicle_locations() | |
| # Add markers for each vehicle | |
| for vehicle_id, (lat, lon) in vehicles.items(): | |
| folium.Marker([lat, lon], popup=vehicle_id).add_to(m) | |
| # Optimize routes | |
| route_optimizer = RouteOptimizer(list(vehicles.keys()), destinations) | |
| optimized_routes = route_optimizer.optimize() | |
| # Add polylines for optimized routes | |
| for vehicle, destination in optimized_routes: | |
| vehicle_location = vehicles[vehicle] | |
| folium.PolyLine([vehicle_location, destination], color="red", weight=2.5, opacity=1).add_to(m) | |
| # Optimize schedules | |
| vehicle_schedules = {vehicle: [1, 2, 3, 4, 5] for vehicle in vehicles} # Example schedules | |
| schedule_optimizer = ScheduleOptimizer(vehicle_schedules) | |
| optimized_schedules = schedule_optimizer.optimize() | |
| return render_template('dashboard.html', map=m._repr_html_(), schedules=optimized_schedules) | |
| def update(): | |
| # Simulate movement of vehicles | |
| gps_tracker.simulate_movement() | |
| # Get updated vehicle locations | |
| updated_locations = gps_tracker.get_vehicle_locations() | |
| # Re-optimize routes based on new locations | |
| route_optimizer = RouteOptimizer(list(updated_locations.keys()), destinations) | |
| new_routes = route_optimizer.optimize() | |
| # Re-optimize schedules | |
| vehicle_schedules = {vehicle: [1, 2, 3, 4, 5] for vehicle in updated_locations} # Example schedules | |
| schedule_optimizer = ScheduleOptimizer(vehicle_schedules) | |
| new_schedules = schedule_optimizer.optimize() | |
| return jsonify({ | |
| 'locations': updated_locations, | |
| 'routes': new_routes, | |
| 'schedules': new_schedules | |
| }) | |
| if __name__ == '__main__': | |
| # Initialize some vehicles | |
| gps_tracker.add_vehicle('Vehicle1') | |
| gps_tracker.add_vehicle('Vehicle2') | |
| app.run(debug=True) |