From 7a43ca82e9e832c17a92fe65d73bb3686f5f24e6 Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Wed, 27 Apr 2022 07:42:49 -0700 Subject: [PATCH] Change address, recognize numbers, add copyright. --- geocode.py | 21 +++++++++++++++++---- 1 file 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 -- 2.45.0