#!/usr/bin/env python3
+# © Copyright 2021-2022, Scott Gasch
+
"""Wrapper around US Census address geocoder API described here:
https://www2.census.gov/geo/pdfs/maps-data/data/Census_Geocoder_User_Guide.pdf"""
from bs4 import BeautifulSoup
from requests.utils import requote_uri
+import string_utils
+
logger = logging.getLogger(__name__)
def geocode_address(address: str) -> Optional[Dict[str, str]]:
"""Send a single address to the US Census geocoding API.
- >>> out = geocode_address('5 Shelbern Dr,,, 07738')
+ >>> out = geocode_address('4600 Silver Hill Rd,, 20233')
>>> out['Matched Address']
- '5 SHELBERN DR, LINCROFT, NJ, 07738'
+ '4600 SILVER HILL RD, WASHINGTON, DC, 20233'
+ >>> out['Interpolated Longitude (X) Coordinates']
+ -76.92743
+ >>> out['Interpolated Latitude (Y) Coordinates']
+ 38.84599
"""
url = 'https://geocoding.geo.census.gov/geocoder/geographies/onelineaddress'
logger.debug('Label is: "%s"', label)
else:
if label:
- out[label] = line.strip()
- logger.debug('Value is: "%s"', out[label])
+ value = line.strip()
+ if string_utils.is_integer_number(value):
+ value = int(value)
+ elif string_utils.is_number(value):
+ value = float(value)
+ logger.debug('Value is: "%s"', value)
+ out[label] = value
return out