|
from geopy.geocoders import Nominatim |
|
from geopy import distance |
|
|
|
|
|
def get_coords_from_address(address: str) -> str: |
|
""" |
|
Converts a street address into latitude and longitude coordinates. |
|
|
|
Args: |
|
address (str): The address to search for (e.g., "Eiffel Tower, Paris"). |
|
|
|
Returns: |
|
str: A formatted string with the coordinates "Lat: XX.XXXX, Lon: YY.YYYY" |
|
or an error message if the address is not found. |
|
""" |
|
try: |
|
geolocator = Nominatim(user_agent="geocalc_mcp_app_hackathon") |
|
|
|
location = geolocator.geocode(address) |
|
|
|
if location: |
|
|
|
lat = round(location.latitude, 4) |
|
lon = round(location.longitude, 4) |
|
return f"Lat: {lat}, Lon: {lon}" |
|
else: |
|
return "Address not found. Please try being more specific. E.g., '1600 Amphitheatre Parkway, Mountain View, CA'" |
|
|
|
except Exception as e: |
|
print(f"An error occurred: {e}") |
|
return "An error occurred while trying to contact the geocoding service." |
|
|
|
|
|
def calculate_distance(lat1: float, lon1: float, lat2: float, lon2: float, unit: str = "km") -> str: |
|
""" |
|
Calculates the distance between two points on the Earth's surface using the Haversine formula. |
|
|
|
Args: |
|
lat1 (float): Latitude of the first point. |
|
lon1 (float): Longitude of the first point. |
|
lat2 (float): Latitude of the second point. |
|
lon2 (float): Longitude of the second point. |
|
unit (str, optional): Unit of measurement for the distance. Default is "km". |
|
|
|
Returns: |
|
float: The distance between the two points in kilometers. |
|
""" |
|
|
|
print("calculate_distance", lat1, lon1, lat2, lon2, unit) |
|
result = "The distance between the two points is: " |
|
if unit == "km": |
|
result += distance.distance((lat1, lon1), (lat2, lon2)).km |
|
else: |
|
result += distance.distance((lat1, lon1), (lat2, lon2)).miles |
|
|
|
return result |
|
|