Using OpenCage together with other geocoders


Traditional proprietary geocoding services like the Google Maps API are technically very good, but also very expensive.

Meanwhile OpenCage, which uses open data (and thus much more flexible terms and conditions), is much more affordable, but many potential customers worry that open sources like OpenStreetMap don't have the same comprehensivness as services like Google Maps

By using both services in combination customers can massively reduce their geocoding costs.

How it works

The solution is to try OpenCage first. If our answer meets your needs, you are done. If not you can then try Google or one of the others.

Our experience is that customers can typically reduce their Google (or other expensive service) bills by 80-90% by following this method.

Here's a simplified "algorithm" that explains how you should approach it:

For each location in your list
  1. send the location to the OpenCage geocoding API
  2. evaluate the response to see if it is "good enough"
  3. if yes, store the result, we are done
  4. if no, send the location to an expensive service like Google

Caveats / Points to Note

There are a few points you should note when working with multiple geocoders
  • You will need to follow the terms and conditions of each service you use. Some services have severe restrictions on how long you can store data returned by their API. Our recommendation is to always store the source of data when storing it so that it is easy to
  • The different geocoding services may return data in different formats and with different attributes. Make sure you understand the response format of each service you are using.. We have a guide to understanding geocoding accuracy. You will need to standardise the results to the specific needs of your project.
  • You should always consider the privacy implications of sending your data to any third-party service.

We have a detailed guide to comparing geocoding services where we discuss these and other points to consider.

Code Example

Below is very basic example python script that you can adapt. The script first makes a request to the OpenCage geocoding API (using our python module - see tutorial), determines the precision of the result by looking at the confidence, and if not then tries Google's API.

If python is not your preferred language, we also have libraries for over 30 other programming languages. including PHP, ruby, java, javascript, and more.

pip3 install opencage
pip3 install googlemaps
from opencage.geocoder import OpenCageGeocode
import googlemaps

# Replace 'YOUR_OPENCAGE_API_KEY' with your OpenCage Geocoding API key

# Replace 'YOUR_GOOGLE_MAPS_API_KEY' with your Google Maps API key

# Function to geocode a location using the OpenCage Geocoding API
def geocode_with_opencage(location):
    geocoder = OpenCageGeocode(OPENCAGE_API_KEY)
    results = geocoder.geocode(location)

    if results and len(results):
        return results[0]
        print(f"Geocoding with OpenCage failed for location: {location}")
        return None

# Function to geocode a location using the Google Maps Geocoding API
def geocode_with_google(location):
    gmaps = googlemaps.Client(key=GOOGLE_MAPS_API_KEY)
    result = gmaps.geocode(location)

    if result and len(result):
        return result[0]
        print(f"Geocoding with Google Maps failed for location: {location}")
        return None

# Input file containing locations, one per line
input_file = 'locations.txt'

# Output file to write geocoding results
output_file = 'geocoding_results.txt'

with open(input_file, 'r') as f_input, open(output_file, 'w') as f_output:
    for line in f_input:
        location = line.strip()  # Remove leading/trailing whitespace
        if location:
            opencage_result = geocode_with_opencage(location)
        if opencage_result and 'confidence' in opencage_result and opencage_result['confidence'] == 10:
            latitude = opencage_result['geometry']['lat']
            longitude = opencage_result['geometry']['lng']
            f_output.write(f"{location} (OpenCage): {latitude}, {longitude}\n")
            google_result = geocode_with_google(location)
            if google_result:
                latitude = google_result['lat']
                longitude = google_result['lng']
                f_output.write(f"{location} (Google Maps): {latitude}, {longitude}\n")

print("Geocoding completed. Results written to 'geocoding_results.txt'.")

Further Reading

Happy geocoding!

Start your free trial

2,500 geocoding API requests per day.

No credit card required.