Skip to content

Commit

Permalink
feat: replace address grid with city/county name for geocoding results
Browse files Browse the repository at this point in the history
  • Loading branch information
stdavis committed Dec 6, 2024
1 parent f46065c commit 88b26cb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
16 changes: 12 additions & 4 deletions src/masquerade/providers/web_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def _get_retry_session():
session = _get_retry_session()


def make_geocode_request(address, zone, out_spatial_reference, max_locations):
def make_geocode_request(address: str, zone: str, out_spatial_reference: int, max_locations: int) -> list:
"""makes a request to the web api geocoding service"""
parameters = {
"apiKey": os.getenv("WEB_API_KEY"),
Expand All @@ -119,10 +119,10 @@ def make_geocode_request(address, zone, out_spatial_reference, max_locations):

if "score" in result:
if result["score"] == 100 or max_locations == 1 and result["score"] >= MIN_SCORE_FOR_BATCH:
return [etl_candidate(result)]
return [etl_candidate(result, out_spatial_reference)]

if "candidates" in result:
return [etl_candidate(candidate) for candidate in result["candidates"]]
return [etl_candidate(candidate, out_spatial_reference) for candidate in result["candidates"]]

return []

Expand Down Expand Up @@ -153,14 +153,22 @@ def get_candidate_from_single_line(single_line_address, out_spatial_reference):
return None


def etl_candidate(ugrc_candidate):
def etl_candidate(ugrc_candidate: dict, spatial_reference: int) -> dict:
"""translates an UGRC Web API candidate to an Esri locator candidate"""
address = ugrc_candidate["address"] if "address" in ugrc_candidate else ugrc_candidate["matchAddress"]
try:
standardized_address = ugrc_candidate["standardizedAddress"]
except KeyError:
standardized_address = None

match_city = address.split(",")[-1].strip()
city_or_county = open_sgid.get_city(
ugrc_candidate["location"]["x"], ugrc_candidate["location"]["y"], spatial_reference
) or open_sgid.get_county(ugrc_candidate["location"]["x"], ugrc_candidate["location"]["y"], spatial_reference)

if city_or_county and match_city.upper() != city_or_county.upper():
address = address.replace(match_city, city_or_county.upper())

return {
"address": address,
"attributes": {
Expand Down
44 changes: 36 additions & 8 deletions tests/test_web_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import re
from unittest import mock

from pytest import raises

Expand All @@ -17,25 +18,52 @@
)


def test_etl_candidate():
@mock.patch("masquerade.providers.open_sgid.get_city")
def test_etl_candidate(get_city_mock):
get_city_mock.return_value = "WEST VALLEY CITY"
input = {
"address": "3989 SOUTH FRONTAGE RD, Salt Lake City",
"address": "3989 SOUTH FRONTAGE RD, SALT LAKE CITY",
"location": {"x": 416707.62244415266, "y": 4508458.9846219225},
"score": 69.92,
"locator": "Centerlines.StatewideRoads",
"addressGrid": "Salt Lake City",
"addressGrid": "SALT LAKE CITY",
}

result = etl_candidate(input)
result = etl_candidate(input, 26912)

assert result["address"] == input["address"]
assert result["address"] == "3989 SOUTH FRONTAGE RD, WEST VALLEY CITY"
assert result["attributes"]["score"] == 69.92
assert result["attributes"]["locator"] == input["locator"]
assert result["attributes"]["addressGrid"] == input["addressGrid"]
assert result["location"] == input["location"]


def test_etl_candidate_base_result():
@mock.patch("masquerade.providers.open_sgid.get_city")
@mock.patch("masquerade.providers.open_sgid.get_county")
def test_etl_candidate_fallback_to_county(get_county_mock, get_city_mock):
get_city_mock.return_value = None
get_county_mock.return_value = "SALT LAKE COUNTY"
input = {
"address": "3989 SOUTH FRONTAGE RD, SALT LAKE CITY",
"location": {"x": 416707.62244415266, "y": 4508458.9846219225},
"score": 69.92,
"locator": "Centerlines.StatewideRoads",
"addressGrid": "SALT LAKE CITY",
}

result = etl_candidate(input, 26912)

assert result["address"] == "3989 SOUTH FRONTAGE RD, SALT LAKE COUNTY"
assert result["attributes"]["score"] == 69.92
assert result["attributes"]["locator"] == input["locator"]
assert result["attributes"]["addressGrid"] == input["addressGrid"]
assert result["location"] == input["location"]


@mock.patch("masquerade.providers.open_sgid.get_city")
def test_etl_candidate_base_result(get_city_mock):
get_city_mock.return_value = "HOLLADAY"

#: this is a test for the base result object as opposed to result.candidate objects
input = {
"location": {"x": 429340.24421129236, "y": 4504146.207401402},
Expand All @@ -48,9 +76,9 @@ def test_etl_candidate_base_result():
"candidates": [],
}

result = etl_candidate(input)
result = etl_candidate(input, 26912)

assert result["address"] == input["matchAddress"]
assert result["address"] == "3987 S 1925 E, HOLLADAY"
assert result["score"] == 100
assert result["attributes"]["locator"] == input["locator"]
assert result["attributes"]["addressGrid"] == input["addressGrid"]
Expand Down

0 comments on commit 88b26cb

Please sign in to comment.