How do I geocode addresses?

There are many options for converting an address into longitude and latitude coordinates. Here we describe two options.

NYC Geoclient API

If your address is in New York City, you can use the NYC Geoclient API which, as of the time of this post, has a rate limit of 2,500 requests per minute and 500,000 requests per day.

  1. Create a NYC Geoclient account to get your own Geoclient API key.

  2. Call the Geoclient API using the House Number, Street Name, Borough Name for each address.

    import requests
    
    # Register for your own NYC Geoclient API key
    # https://developer.cityofnewyork.us/api/geoclient-api
    base_url = 'https://api.cityofnewyork.us/geoclient/v1/address.json'
    geoclient_id = 'YOUR-GEOCLIENT-ID'
    geoclient_key = 'YOUR-GEOCLIENT-KEY'
    
    house_number = '345'
    street_name = 'Chambers St'
    borough_name = 'Manhattan'
    
    response = requests.get(base_url, {
        'houseNumber': house_number,
        'street': street_name,
        'borough': borough_name,
        'app_id': geoclient_id,
        'app_key': geoclient_key,
    })
    response_json = response.json()
    d = response_json['address']
    print(d['longitude'], d['latitude'])       # EPSG:4326
    print(d['xCoordinate'], d['yCoordinate'])  # EPSG:2263
    

Google Geocoding API

If you want to convert a generic address into longitude and latitude coordinates, you can use the Google Geocoding API which, as of the time of this post, has a rate limit of 50 requests per second and 2,500 requests per day.

  1. Register for your own Google Geocoding API key.

  2. Call the Geocoding API.

     from geopy import GoogleV3
    
     geocode = GoogleV3('YOUR-API-KEY').geocode
    
     def get_lonlat(address):
         location = geocode(address)
         return location.longitude, location.latitude