Change address, recognize numbers, add copyright.
authorScott Gasch <[email protected]>
Wed, 27 Apr 2022 14:42:49 +0000 (07:42 -0700)
committerScott Gasch <[email protected]>
Wed, 27 Apr 2022 14:42:49 +0000 (07:42 -0700)
geocode.py

index 023fe336f3a775908476cacc35bf1167573a0980..87c23f57da30259eef321b2f35affce43d562574 100644 (file)
@@ -1,5 +1,7 @@
 #!/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"""
 
@@ -11,15 +13,21 @@ import requests
 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'
@@ -45,8 +53,13 @@ def geocode_address(address: str) -> Optional[Dict[str, str]]:
                 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