From 80418172db5f8ee48c3dde96ea71aa4c36a9ad37 Mon Sep 17 00:00:00 2001 From: Kyle Pope Date: Tue, 17 Mar 2026 15:58:20 +0800 Subject: [PATCH] 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) --- backend/app/routers/locations.py | 35 ++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/backend/app/routers/locations.py b/backend/app/routers/locations.py index 7944451..8f39a60 100644 --- a/backend/app/routers/locations.py +++ b/backend/app/routers/locations.py @@ -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",