Spaces:
Running
Running
Shrijayan Rajendran8
Add initial project setup with FastAPI, Docker, and geocoding functionality
5754a38
from geopy.geocoders import Nominatim | |
import logging | |
import time | |
def get_lat_long(address): | |
logger = logging.getLogger() | |
geolocator = Nominatim(user_agent="coordinate_finder", timeout=5) | |
try: | |
logger.info(f"Geocoding address: '{address}'") | |
location = geolocator.geocode(address) | |
if location: | |
logger.info(f"Geocoded '{address}' to ({location.latitude}, {location.longitude})") | |
return (location.latitude, location.longitude) | |
else: | |
logger.warning(f"Failed to geocode address: '{address}'") | |
# Try a simpler version of the address by removing zip code if present | |
if ',' in address: | |
simpler_address = address.split(',')[0] | |
logger.info(f"Trying simpler address: '{simpler_address}'") | |
time.sleep(1) # Wait a bit before trying again | |
location = geolocator.geocode(simpler_address) | |
if location: | |
logger.info(f"Geocoded simplified '{simpler_address}' to ({location.latitude}, {location.longitude})") | |
return (location.latitude, location.longitude) | |
else: | |
logger.warning(f"Also failed with simpler address: '{simpler_address}'") | |
return None | |
except Exception as e: | |
logger.error(f"Error geocoding '{address}': {str(e)}") | |
return None | |
if __name__ == "__main__": | |
logging.basicConfig( | |
level=logging.INFO, | |
format='%(asctime)s - %(levelname)s - %(message)s' | |
) | |
logger = logging.getLogger() | |
start_address = "2665 SOUTH DR, SANTA CLARA, 95051" | |
end_address = "450 E PERSIAN DR, SUNNYVALE, 94089" | |
logger.info(f"Testing geocoding with two addresses") | |
start_coords = get_lat_long(start_address) | |
logger.info(f"Coordinates for '{start_address}': {start_coords}") | |
end_coords = get_lat_long(end_address) | |
logger.info(f"Coordinates for '{end_address}': {end_coords}") | |
if start_coords and end_coords: | |
# Calculate distance (this would require additional imports) | |
logger.info(f"Coordinates successfully retrieved for both addresses") |