Search code examples
google-mapsgoogle-maps-api-3google-distancematrix-api

Why I keep getting an error of MAX_ELEMENTS_EXCEEDED when I don't overpass the daily free number of requests?


So I developed a code where I read a file with some locations (15 to be more precise), where the first one is the depot and the other 14 locations are the places where a bus needs to pass to collect patients. In order to do that, I use Google Maps API key in order to collect the real distances and write them in a .txt file in the end.

import pandas as pd
import googlemaps
from itertools import tee
import numpy as np

#Read CSV file into data frame named 'df'
#change seperator (sep e.g. ',') type if necessary
df = pd.read_csv("D:/Utilizadores/Documents/FEUP/2018-2019/1º Semestre/PDI/Relatório/locais2.txt", sep='\\t',
                       engine='python', skiprows=[0], names=["address", "latitude", "longitude"])

lat = np.expand_dims(np.array(df["latitude"]), 1)
lon = np.expand_dims(np.array(df["longitude"]), 1)
coordinates = np.concatenate((lat, lon), axis=1)

coordinates = list(coordinates)
coordinates = [list(coor) for coor in coordinates]


#Perform request to use the Google Maps API web service
#API_key = 'AIzaSyCi8DDz_CCiVwW2JtvT6i-XpJYiEwxFryI'
API_key = 'AIzaSyCpumDcRbbteV64xlGOUQ5_Bah8Ja5gdJ4'
gmaps = googlemaps.Client(key=API_key)

result = gmaps.distance_matrix(coordinates, coordinates, mode='driving')["rows"]
distance_matrix = np.zeros((len(result), len(result)))

for i in range(len(result)):
    for j in range(len(result)):
        distance_matrix[i, j] = result[i]["elements"][j]["distance"]["value"]

np.savetxt("D:/Utilizadores/Documents/FEUP/2018-2019/1º Semestre/PDI/Relatório/locais_output.txt", distance_matrix, delimiter=",", fmt='%d')
print(distance_matrix)

The distances I want are from one place to every place, so the result I want is a matrix of 15x15 where the diagonal is filled with 0's. But it keeps printing this error:

"googlemaps.exceptions.ApiError: MAX_ELEMENTS_EXCEEDED".

The only way to not have an error is to put a limit on the reading of the file of 10 locations, including the depot:

result = gmaps.distance_matrix(coordinates[0:10], coordinates[0:10], mode='driving')["rows"]

Why is that? Anyone?


Solution

  • from the documentation

    MAX_ELEMENTS_EXCEEDED indicates that the product of origins and destinations exceeds the per-query limit.

    from "usage and billing":

    Each query sent to the Distance Matrix API generates elements, where the number of origins times the number of destinations equals the number of elements.

    Other Usage Limits
    While you are no longer limited to a maximum number of elements per day (EPD), the following usage limits are still in place for the Distance Matrix API:

    Maximum of 25 origins or 25 destinations per request.
    Maximum 100 elements per server-side request.
    Maximum 100 elements per client-side request.
    1000 elements per second (EPS), calculated as the sum of client-side and server-side queries.

    15 x 15 = 225 which is greater than the maximum allowed (100)

    That you don't want or need some of the responses is not relevant.