File size: 1,687 Bytes
7a3d380
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Address geocoding via the Google Geocoding API.

Used by the AI intake flow: when Claude extracts a claim whose text has
an address but no explicit coordinates, this resolves the address to
lat/lon so the claim lands on the map at its real location instead of
the region-center placeholder.

Key-gated: uses GOOGLE_MAPS_API_KEY (the same key as the Routes API;
enable 'Geocoding API' on the Google Cloud project). Without a key,
callers fall back to the region-center placeholder.
"""

from __future__ import annotations

import json
import os
import urllib.parse
import urllib.request

ENDPOINT = "https://maps.googleapis.com/maps/api/geocode/json"


class GeocodeError(RuntimeError):
    pass


def geocode(address: str, api_key: str | None = None,
            region_hint: str = "us") -> tuple[float, float]:
    """Resolve an address to (lat, lon). Raises GeocodeError on failure."""
    api_key = api_key or os.environ.get("GOOGLE_MAPS_API_KEY")
    if not api_key:
        raise GeocodeError("GOOGLE_MAPS_API_KEY not set")
    params = urllib.parse.urlencode({
        "address": address, "key": api_key, "region": region_hint,
    })
    try:
        with urllib.request.urlopen(f"{ENDPOINT}?{params}",
                                    timeout=15) as resp:
            data = json.loads(resp.read())
    except OSError as e:
        raise GeocodeError(f"Geocoding request failed: {e}") from e
    if data.get("status") != "OK" or not data.get("results"):
        raise GeocodeError(f"Geocoding returned {data.get('status')} "
                           f"for {address!r}")
    loc = data["results"][0]["geometry"]["location"]
    return float(loc["lat"]), float(loc["lng"])