File size: 947 Bytes
7b6b271
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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}")