From: Scott Gasch Date: Wed, 27 Apr 2022 14:42:49 +0000 (-0700) Subject: Change address, recognize numbers, add copyright. X-Git-Url: https://wannabe.guru.org/gitweb/?a=commitdiff_plain;h=7a43ca82e9e832c17a92fe65d73bb3686f5f24e6;p=python_utils.git Change address, recognize numbers, add copyright. --- 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