Rework Nominatim results: name as label, address as full street address

Name now uses the Nominatim place/building label (e.g. "The Quadrant")
when available, falling back to street address. Address field now
contains the full formatted address (house number, road, suburb, city,
state, postcode, country) instead of just the city/state portion.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Kyle 2026-03-17 15:58:20 +08:00
parent d4117818c7
commit 80418172db

View File

@ -71,29 +71,34 @@ async def search_locations(
addr = item.get("address", {})
house_number = addr.get("house_number", "")
road = addr.get("road", "")
# If Nominatim didn't return a house_number but the user's
# query starts with one, preserve it from the original query.
if not house_number and road:
m = re.match(r"^(\d+[\w/-]*)\s+", q.strip())
if m:
house_number = m.group(1)
# Build a name that preserves the house number
if house_number and road:
name = f"{house_number} {road}"
elif road:
name = road
# Name = place/building label from Nominatim (e.g. "The Quadrant").
# Falls back to street address if no distinct place name exists.
osm_name = item.get("name", "")
street = f"{house_number} {road}" if house_number and road else road
if osm_name and osm_name != road:
name = osm_name
elif street:
name = street
else:
# Fallback: first comma-separated segment
name = display_name.split(",", 1)[0].strip()
# Address: everything after the street portion
name_prefix = f"{house_number}, " if house_number else ""
road_prefix = f"{road}, " if road else ""
strip_prefix = name_prefix + road_prefix
if strip_prefix and display_name.startswith(strip_prefix):
address = display_name[len(strip_prefix):].strip()
else:
name_parts = display_name.split(",", 1)
address = name_parts[1].strip() if len(name_parts) > 1 else display_name
# Address = full street address with suburb/state/postcode.
addr_parts = []
if street:
addr_parts.append(street)
for key in ("suburb", "city", "state", "postcode", "country"):
val = addr.get(key, "")
if val:
addr_parts.append(val)
address = ", ".join(addr_parts) if addr_parts else display_name
results.append(
LocationSearchResult(
source="nominatim",