0

There are various methods to reverse geocode from coordinates(latitudes, longitudes) to 5 digit zips or census tracts/blocks. For example geopy.geocoders.Nominatim and FCC census API. However it is possible to reverse geocode to zip+4 or even zip+2?

I see that some organization provides zip+4 centroids (Geolytics). If reverse geocode is not available, I can try to use that by finding closest zip+4, however a lookup will be better.

Then, there are APIs like one on zip-codes.com (API link) which takes address as input and provide zip+4 as output. Therefore some API or non-API lookup should exist which give zip+4 from coordinates. I work mainly in python with no access/experience to any GIS software.

Edit: Found that google map API does provide zip+4 for certain coordinates (and for rest only 5 digit zip comes up). Google API restricts usage to 3k calls per day and I need to make 100k calls.

5
  • Both Zip and Zip+4 are linear features (sometimes degenerate, i.e., points). There are polygons, but the closer you get to actual routes, the more likely that you will get a false match.
    – Vince
    Commented Apr 21, 2023 at 19:55
  • I agree, I am find with an approximation. For example: if a zip+4 is a street - and the corresponding coordinate has a (perpendicular) distance of <=100 meters from that street - it works for my use case. Commented Apr 21, 2023 at 20:39
  • 1
    It's more likely to be a few miles than <100 meters, though probably not more than 50 miles.
    – Vince
    Commented Apr 21, 2023 at 23:12
  • ok, may be I can just filter out the bad points as I have lot of data. My hope is, for many data points, I will get something within say 0.3 miles. (I can stretch till that). I also found that google map api does provide zip+4 when you lookup using coordinates. However, many of the coordinates end up getting only zip and not zip+4, probably due to the reason you mentioned. I am fine if I don't get it for all my lat-lon. My problem with google map API is that its restricted to 3k calls per day, and I want to make like 100 k calls. Commented Apr 22, 2023 at 1:48
  • I finally found two such APIs. one is google map API, other is API through geocodio library in python. Commented Apr 26, 2023 at 4:14

1 Answer 1

1

I finally found two ways to do it. Catch is you may not get zip+4 for all the lookups, so if you don't do some kind of exception handling your code may not work. Also, you need to register and get API keys in both the methods.

Google map API

import requests
def get_zip_code(latitude, longitude, api_key):
    # Set up Google Maps API endpoint and parameters
    endpoint = "https://maps.googleapis.com/maps/api/geocode/json"
    params = {
        "latlng": f"{latitude},{longitude}",
        "key": api_key
    }

    # Send GET request to Google Maps API
    response = requests.get(endpoint, params=params)
    data = response.json()

    # Extract zip9 (zip+4) from response
    zip9 = None
    if "results" in data and len(data["results"]) > 0:
        result = data["results"][0]
        address_components = result.get("address_components", [])
        for component in address_components:
            if "postal_code_suffix" in component["types"]:
                zip9 = component["long_name"]
    return zip9

Geocodio API

from geocodio import GeocodioClient
client = GeocodioClient(geocodio_api_key)

# Define a function to perform reverse geocoding
def get_zip_code (lat, lon):
    try:
        location = client.reverse((lat, lon), fields=["zip4"])
        zip9 = location['results'][0]['fields']['zip4']['zip9'][0]
    except Exception as e:
        zip9 = None
        print("Error occurred while reverse geocoding")
    return zip9

Also, these APIs not limited to zip+4 but actually provide many other fields like address, city, state etc. geocodio had much higher fill rate than google atleast for the coordinates I had.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.