#!/usr/bin/env python3 """ Location Setup Script for Fleet Optimization Simulator Easy setup for custom locations using Google Maps API """ import sys import json from location_config import LocationManager, initialize_location_manager def setup_custom_location(): """Interactive setup for custom location""" print("๐ŸŒ Custom Location Setup for Fleet Optimization Simulator") print("=" * 60) # Initialize location manager api_key = "AIzaSyBTA3eACtpCPR9DDi8EhOt1cI7Cy08Mkfg" manager = LocationManager(api_key) print("\nChoose setup method:") print("1. ๐Ÿ“ Enter coordinates manually") print("2. ๐Ÿ” Search by address/place name") print("3. ๐Ÿ“‹ Use predefined location") print("4. โŒ Exit") choice = input("\nEnter your choice (1-4): ").strip() if choice == '1': setup_by_coordinates(manager) elif choice == '2': setup_by_address(manager) elif choice == '3': setup_predefined(manager) elif choice == '4': print("๐Ÿ‘‹ Goodbye!") return else: print("โŒ Invalid choice") return def setup_by_coordinates(manager): """Setup location using coordinates""" print("\n๐Ÿ“ Manual Coordinate Setup") print("-" * 30) try: name = input("Enter location name (e.g., 'San Francisco'): ").strip() if not name: print("โŒ Location name is required") return lat = float(input("Enter latitude (e.g., 37.7749): ")) lng = float(input("Enter longitude (e.g., -122.4194): ")) # Validate coordinates if not manager.validate_coordinates(lat, lng): print("โŒ Invalid coordinates. Latitude must be -90 to 90, longitude -180 to 180") return # Get location info location_info = manager.reverse_geocode(lat, lng) if location_info: print(f"๐Ÿ“ Location found: {location_info['formatted_address']}") # Create location bounds = manager.get_bounds_from_center(lat, lng, 10) # 10km radius location = manager.create_custom_location(name, lat, lng, bounds) if location: print(f"โœ… Custom location created: {name}") print(f" Center: {lat:.4f}, {lng:.4f}") print(f" Hotspots: {len(location.hotspots)}") # Save to file filename = f"custom_location_{name.lower().replace(' ', '_')}.json" manager.save_custom_location(location, filename) print(f"๐Ÿ’พ Location saved to: {filename}") # Test the location test_location(manager, location) else: print("โŒ Failed to create location") except ValueError: print("โŒ Invalid number format") except Exception as e: print(f"โŒ Error: {e}") def setup_by_address(manager): """Setup location using address search""" print("\n๐Ÿ” Address Search Setup") print("-" * 25) address = input("Enter address or place name (e.g., 'Times Square, New York'): ").strip() if not address: print("โŒ Address is required") return print(f"๐Ÿ” Searching for: {address}") # Geocode the address coords = manager.geocode(address) if not coords: print("โŒ Address not found. Please try a different address.") return lat, lng = coords print(f"๐Ÿ“ Found coordinates: {lat:.4f}, {lng:.4f}") # Get location info location_info = manager.reverse_geocode(lat, lng) if location_info: print(f"๐Ÿ“ Location: {location_info['formatted_address']}") # Create location name = input("Enter a name for this location: ").strip() or address bounds = manager.get_bounds_from_center(lat, lng, 10) location = manager.create_custom_location(name, lat, lng, bounds) if location: print(f"โœ… Custom location created: {name}") print(f" Center: {lat:.4f}, {lng:.4f}") print(f" Hotspots: {len(location.hotspots)}") # Save to file filename = f"custom_location_{name.lower().replace(' ', '_')}.json" manager.save_custom_location(location, filename) print(f"๐Ÿ’พ Location saved to: {filename}") # Test the location test_location(manager, location) else: print("โŒ Failed to create location") def setup_predefined(manager): """Setup using predefined location""" print("\n๐Ÿ“‹ Predefined Location Setup") print("-" * 30) locations = manager.get_available_locations() print("Available predefined locations:") for i, loc in enumerate(locations, 1): print(f" {i}. {loc}") try: choice = int(input(f"\nSelect location (1-{len(locations)}): ")) if 1 <= choice <= len(locations): location_id = locations[choice - 1] manager.set_location(location_id) location = manager.current_location print(f"โœ… Location set to: {location.name}") print(f" Center: {location.center_lat:.4f}, {location.center_lng:.4f}") print(f" Hotspots: {len(location.hotspots)}") print(f" Country: {location.country}") # Test the location test_location(manager, location) else: print("โŒ Invalid choice") except ValueError: print("โŒ Invalid number format") except Exception as e: print(f"โŒ Error: {e}") def test_location(manager, location): """Test the location setup""" print(f"\n๐Ÿงช Testing location: {location.name}") print("-" * 40) # Test geocoding print("๐Ÿ” Testing geocoding...") coords = manager.geocode(f"{location.name}, {location.country}") if coords: print(f" โœ… Geocoding works: {coords[0]:.4f}, {coords[1]:.4f}") else: print(" โš ๏ธ Geocoding test failed") # Test reverse geocoding print("๐Ÿ“ Testing reverse geocoding...") location_info = manager.reverse_geocode(location.center_lat, location.center_lng) if location_info: print(f" โœ… Reverse geocoding works: {location_info['formatted_address']}") else: print(" โš ๏ธ Reverse geocoding test failed") # Show hotspots print(f"๐Ÿข Hotspots ({len(location.hotspots)}):") for i, hotspot in enumerate(location.hotspots[:3], 1): # Show first 3 print(f" {i}. {hotspot['name']} ({hotspot['lat']:.4f}, {hotspot['lng']:.4f})") if len(location.hotspots) > 3: print(f" ... and {len(location.hotspots) - 3} more") print(f"\nโœ… Location setup complete! You can now use '{location.name}' in the simulator.") def show_usage_examples(): """Show usage examples""" print("\n๐Ÿ“– Usage Examples") print("-" * 20) print("1. In the simulator interface:") print(" - Select from dropdown: new_york, london, tokyo, singapore") print(" - Or create custom location with coordinates") print() print("2. Programmatically:") print(" from realtime_fleet_optimizer import RealTimeFleetOptimizer") print(" optimizer = RealTimeFleetOptimizer('london')") print(" # or") print(" optimizer.set_location('tokyo')") print() print("3. Custom location:") print(" optimizer.create_custom_location('Paris', 48.8566, 2.3522, bounds)") def main(): """Main function""" if len(sys.argv) > 1 and sys.argv[1] == '--help': show_usage_examples() return try: setup_custom_location() except KeyboardInterrupt: print("\n๐Ÿ‘‹ Setup cancelled by user") except Exception as e: print(f"โŒ Unexpected error: {e}") if __name__ == "__main__": main()