D3MI4N's picture
Production ready app version
7b6b271
raw
history blame contribute delete
947 Bytes
import re
from geopy.geocoders import Nominatim
def extract_coordinates_from_gmaps_url(url_or_string):
# Match Google Maps URLs with lat/lng OR raw coordinates like "36.7156,-4.4044"
coord_pattern = re.compile(r'(-?\d+\.\d+),\s*(-?\d+\.\d+)')
match = coord_pattern.search(url_or_string)
if match:
return {"lat": float(match.group(1)), "lng": float(match.group(2))}
return None
def get_coordinates_from_location(location_str):
# Try to extract coordinates from string first
coords = extract_coordinates_from_gmaps_url(location_str)
if coords:
return coords
# Otherwise, treat it as a place name and geocode it
geolocator = Nominatim(user_agent="surf-spot-finder")
location = geolocator.geocode(location_str)
if location:
return {"lat": location.latitude, "lng": location.longitude}
raise ValueError(f"Could not resolve coordinates for input: {location_str}")