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