Skip to content

Commit 8304daf

Browse files
committed
Address search handles commas, and address suggestion works for example addresses
1 parent 23d451d commit 8304daf

2 files changed

Lines changed: 101 additions & 36 deletions

File tree

logs/service-err.log

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4799,3 +4799,7 @@ Unable to create process using '"C:\Users\jamez\AppData\Local\Microsoft\WindowsA
47994799
Unable to create process using '"C:\Users\jamez\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe" -m uvicorn server:app --host 0.0.0.0 --port 8000'
48004800
Unable to create process using '"C:\Users\jamez\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe" -m uvicorn server:app --host 0.0.0.0 --port 8000'
48014801
Unable to create process using '"C:\Users\jamez\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe" -m uvicorn server:app --host 0.0.0.0 --port 8000'
4802+
Unable to create process using '"C:\Users\jamez\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe" -m uvicorn server:app --host 0.0.0.0 --port 8000'
4803+
Unable to create process using '"C:\Users\jamez\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe" -m uvicorn server:app --host 0.0.0.0 --port 8000'
4804+
Unable to create process using '"C:\Users\jamez\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe" -m uvicorn server:app --host 0.0.0.0 --port 8000'
4805+
Unable to create process using '"C:\Users\jamez\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe" -m uvicorn server:app --host 0.0.0.0 --port 8000'

service/api/address.py

Lines changed: 97 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,53 @@
99
ELEV_URL = "https://maps.six.nsw.gov.au/arcgis/rest/services/public/NSW_5M_Elevation/ImageServer/identify"
1010

1111

12+
# def _query_address(address_string: str, out_sr: str) -> dict | None:
13+
# """Private helper — queries address API with given output spatial reference"""
14+
# params = {
15+
# "where": f"address = '{address_string.upper()}'",
16+
# "outFields": "address",
17+
# "returnGeometry": True,
18+
# "outSR": out_sr,
19+
# "f": "json"
20+
# }
21+
22+
# response = requests.get(ADDR_URL, params=params)
23+
# data = response.json()
24+
25+
# features = data.get("features", [])
26+
# if not features:
27+
# return None
28+
29+
# return features[0]
30+
31+
32+
# ADMIN_LAYERS = {
33+
# "suburb": (2, "suburbname"),
34+
# "lga": (8, "lganame"),
35+
# "parish": (5, "parishname"),
36+
# "county": (11, "countyname"),
37+
# }
1238
def _query_address(address_string: str, out_sr: str) -> dict | None:
13-
"""Private helper — queries address API with given output spatial reference"""
14-
params = {
15-
"where": f"address = '{address_string.upper()}'",
16-
"outFields": "address",
17-
"returnGeometry": True,
18-
"outSR": out_sr,
19-
"f": "json"
20-
}
21-
22-
response = requests.get(ADDR_URL, params=params)
23-
data = response.json()
39+
def _query(where: str) -> dict | None:
40+
params = {
41+
"where": where,
42+
"outFields": "address",
43+
"returnGeometry": True,
44+
"outSR": out_sr,
45+
"f": "json",
46+
}
47+
response = requests.get(ADDR_URL, params=params)
48+
features = response.json().get("features", [])
49+
return features[0] if features else None
2450

25-
features = data.get("features", [])
26-
if not features:
27-
return None
51+
# Try exact match first — fastest and most precise
52+
result = _query(f"address = '{address_string.upper()}'")
53+
if result:
54+
return result
2855

29-
return features[0]
56+
# Fall back to starts-with LIKE — handles suburb name variations
57+
# e.g. "1 SMITH STREET BONDI" matches "1 SMITH STREET BONDI BEACH"
58+
return _query(f"address LIKE '{address_string.upper()}%'")
3059

3160

3261
ADMIN_LAYERS = {
@@ -131,41 +160,73 @@ def get_address_coordinates(address_string: str, out_sr: int = 7856) -> Address
131160
)
132161

133162

134-
def get_address_suggestions(address_string: str, limit: int = 10) -> list[str] | None:
135-
"""
136-
Returns a list of matching addresses from NSW API using LIKE query.
163+
# def get_address_suggestions(address_string: str, limit: int = 10) -> list[str] | None:
164+
# """
165+
# Returns a list of matching addresses from NSW API using LIKE query.
137166

138-
Used for address suggestion/autocomplete — user types partial address,
139-
this returns candidates for the user to pick from.
167+
# Used for address suggestion/autocomplete — user types partial address,
168+
# this returns candidates for the user to pick from.
140169

141-
Example: "14 Dellview Street Tamarama" might return:
142-
- "14-16 DELLVIEW STREET TAMARAMA"
143-
- "14 DELLVIEW STREET TAMARAMA"
144-
"""
145-
from service.utils import sanitise_address
170+
# Example: "14 Dellview Street Tamarama" might return:
171+
# - "14-16 DELLVIEW STREET TAMARAMA"
172+
# - "14 DELLVIEW STREET TAMARAMA"
173+
# """
174+
# from service.utils import sanitise_address
146175

147-
normalised = sanitise_address(address_string)
176+
# normalised = sanitise_address(address_string)
177+
178+
# params = {
179+
# "where": f"address LIKE '%{normalised}%'",
180+
# "outFields": "address",
181+
# "returnGeometry": False,
182+
# "resultRecordCount": limit,
183+
# "f": "json"
184+
# }
185+
186+
# try:
187+
# response = requests.get(ADDR_URL, params=params)
188+
# data = response.json()
189+
# features = data.get("features", [])
190+
191+
# if not features:
192+
# return None
193+
194+
# # Extract unique addresses
195+
# addresses = list(set(f["attributes"]["address"] for f in features))
196+
# return sorted(addresses)
148197

198+
# except Exception as e:
199+
# print(f"[address] Error querying suggestions for '{address_string}': {e}")
200+
# return None
201+
def get_address_suggestions(address_string: str, limit: int = 10) -> list[str] | None:
202+
from service.utils import sanitise_address
203+
204+
normalised = sanitise_address(address_string)
205+
206+
# AND together a LIKE condition per token so "14 DELLVIEW TAMARAMA" matches
207+
# "14-16 DELLVIEW STREET TAMARAMA" — single full-string LIKE would miss it
208+
tokens = [t for t in normalised.split() if len(t) > 1]
209+
if not tokens:
210+
return None
211+
212+
where = " AND ".join(f"address LIKE '%{token}%'" for token in tokens)
213+
149214
params = {
150-
"where": f"address LIKE '%{normalised}%'",
151-
"outFields": "address",
152-
"returnGeometry": False,
215+
"where": where,
216+
"outFields": "address",
217+
"returnGeometry": False,
153218
"resultRecordCount": limit,
154-
"f": "json"
219+
"f": "json",
155220
}
156-
221+
157222
try:
158223
response = requests.get(ADDR_URL, params=params)
159224
data = response.json()
160225
features = data.get("features", [])
161-
162226
if not features:
163227
return None
164-
165-
# Extract unique addresses
166-
addresses = list(set(f["attributes"]["address"] for f in features))
228+
addresses = list({f["attributes"]["address"] for f in features})
167229
return sorted(addresses)
168-
169230
except Exception as e:
170231
print(f"[address] Error querying suggestions for '{address_string}': {e}")
171232
return None

0 commit comments

Comments
 (0)