Spaces:
Sleeping
Sleeping
File size: 2,264 Bytes
5754a38 |
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 |
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") |