Skip to content

Commit

Permalink
handle some more edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsonlive committed Jan 9, 2024
1 parent a06e72a commit ada50e6
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions core/bam_core/utils/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
COMMON_ZIPCODE_MISTAKES = {
"112007": "11207",
}
# default bin response from nyc planning labs
DEFAULT_BIN_RESPONSE = "3000000"


def _fix_zip_code(zip_code: Optional[str]) -> str:
Expand Down Expand Up @@ -72,9 +74,10 @@ def format_address(
if no_place_response:
# if no place response, use granularity from the norm address response
granularity = norm_address.get("verdict", {}).get("validationGranularity", "")
input_granularity = norm_address.get("verdict", {}).get("inputGranularity", "")
if granularity == "SUB_PREMISE":
response["cleaned_address_accuracy"] = "Apartment"
elif granularity == "PREMISE":
elif granularity == "PREMISE" or input_granularity == "PREMISE":
response["cleaned_address_accuracy"] = "Building"

usps_data = norm_address.get("uspsData", {}).get("standardizedAddress", {})
Expand All @@ -91,17 +94,22 @@ def format_address(
# if no formatted address, use the place address
if not cleaned_address:
cleaned_address = place_address.upper()
response["cleaned_address"] = cleaned_address

# return the cleaned address and
# lookup the bin using the nyc planning labs api
nycpl_response = nycpl.search(cleaned_address)
response["bin"] = (
nycpl_response.get("features", [{}])[0]
.get("properties", {})
.get("addendum", {})
.get("pad", {})
.get("bin", "")
)
# only if the granularity is returned
if response["cleaned_address_accuracy"] != "No result":
response["cleaned_address"] = cleaned_address
nycpl_response = nycpl.search(cleaned_address)
bin = (
nycpl_response.get("features", [{}])[0]
.get("properties", {})
.get("addendum", {})
.get("pad", {})
.get("bin", "")
)
if bin and bin != DEFAULT_BIN_RESPONSE:
response["bin"] = bin
return response


Expand Down

0 comments on commit ada50e6

Please sign in to comment.