X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=geocode.py;h=e9e5c35c5fbec6a272518351c461ec5a4fed4243;hb=e46158e49121b8a955bb07b73f5bcf9928b79c90;hp=39593609f31bd4f3f00fa1556099d986844f3bc5;hpb=62b4b777a68a99c8e446294f7e7e35ebd9668989;p=python_utils.git diff --git a/geocode.py b/geocode.py index 3959360..e9e5c35 100644 --- a/geocode.py +++ b/geocode.py @@ -3,10 +3,11 @@ # © Copyright 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 -https://geocoding.geo.census.gov/geocoder/Geocoding_Services_API.pdf -Also try: +* https://www2.census.gov/geo/pdfs/maps-data/data/Census_Geocoder_User_Guide.pdf +* https://geocoding.geo.census.gov/geocoder/Geocoding_Services_API.pdf + +Also try:: $ curl --form addressFile=@localfile.csv \ --form benchmark=2020 \ @@ -27,9 +28,24 @@ logger = logging.getLogger(__name__) def geocode_address(address: str) -> Optional[Dict[str, Any]]: - """Send a single address to the US Census geocoding API. The response - is a parsed JSON chunk of data with N addressMatches in the result - section and the details of each match within it. Returns None on error. + """Send a single address to the US Census geocoding API in order to + lookup relevant data about it (including, if possible, its + lat/long). The response is a parsed JSON chunk of data with N + addressMatches in the result section and the details of each match + within it. + + Args: + address: the full address to lookup in the form: "STREET + ADDRESS, CITY, STATE, ZIPCODE". These components may be + omitted and the service will make educated guesses but + the commas delimiting each component must be included. + + Returns: + A parsed json dict with a bunch of information about the + address contained within it. Each 'addressMatch' + in the JSON describes the details of a possible match. + Returns None if there was an error or the address is + not known. >>> json = geocode_address('4600 Silver Hill Rd,, 20233') >>> json['result']['addressMatches'][0]['matchedAddress'] @@ -37,7 +53,6 @@ def geocode_address(address: str) -> Optional[Dict[str, Any]]: >>> json['result']['addressMatches'][0]['coordinates'] {'x': -76.9274328556918, 'y': 38.845989080537514} - """ url = 'https://geocoding.geo.census.gov/geocoder/geographies/onelineaddress' url += f'?address={address}' @@ -58,15 +73,25 @@ def geocode_address(address: str) -> Optional[Dict[str, Any]]: return r.json() -def batch_geocode_addresses(addresses: List[str]): - """Send up to addresses for batch geocoding. Each line of the input - list should be a single address of the form: STREET ADDRESS, CITY, - STATE, ZIP. Components may be omitted but the commas may not be. - Result is an array of the same size as the input array with one - answer record per line. Returns None on error. +def batch_geocode_addresses(addresses: List[str]) -> Optional[List[str]]: + """Send a list of addresses for batch geocoding to a web service + operated by the US Census Bureau. + + Args: + addresses: a list of addresses to geocode. Each line of the + input list should be a single address in the form: "STREET + ADDRESS, CITY, STATE, ZIPCODE". Individual address components + may be omitted and the service will make educated guesses but + the commas delimiters between address components may not be + omitted. + + Returns: + An array of the same size as the input array with one + answer record per line. Returns None on error. - This code will deal with requests >10k addresses by chunking them - internally because the census website disallows requests > 10k lines. + Note: this code will deal with requests >10k addresses by chunking + them internally because the census website disallows requests > + 10k lines. >>> batch_geocode_addresses( ... [