summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Gasch <[email protected]>2022-04-27 07:42:49 -0700
committerScott Gasch <[email protected]>2022-04-27 07:42:49 -0700
commit7a43ca82e9e832c17a92fe65d73bb3686f5f24e6 (patch)
tree60d9c98e59f6786f528ed24eb51cab88074548b4
parent10f3dbfb14c357e70055d76031bd0345abfc1759 (diff)
Change address, recognize numbers, add copyright.
-rw-r--r--geocode.py21
1 files changed, 17 insertions, 4 deletions
diff --git a/geocode.py b/geocode.py
index 023fe33..87c23f5 100644
--- a/geocode.py
+++ b/geocode.py
@@ -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