1

python googlemaps.Client.distance_matrix returns a quite similar results with and without departure_time.

In my project, We are running the googlemaps.Client.distance_matrix function with and without departure time. Our main goal is see the connection between predictive travel, which is based on the time of day in which the travel is occurring, to street-level routing (SLR), which we thought was based on "empty streets" traffic.

Strangely, we see that the difference between the SLR and predictive is always less then 15 minutes (!!!), no matter if we compare them in rush hours or not.

import pandas as pd
import googlemaps
import numpy as np
import matplotlib.pyplot as plt
import datetime

google_key = "some_google_key_here"
google_client = googlemaps.Client(key=google_key)


def get_from_google(origin_string, dest_string, time):
    google_response = google_client.distance_matrix(origins=origin_string,
                                                    destinations=dest_string,
                                                    departure_time=time,
                                                    mode="driving")
    travel_data = google_response["rows"][0]["elements"][0]
    if travel_data["status"] != "OK":
        return np.nan
    return travel_data["duration"]["value"]


def get_google_results(origin_string, dest_string, time_string):
    time = datetime.datetime.strptime(time_string, '%m/%d/%Y %I:%M:%S %p')
    return [get_from_google(origin_string, dest_string, None),
            get_from_google(origin_string, dest_string, time)]


data_frame = pd.read_csv("path_to_csv_of_travels.csv").sample(1000, random_state=42)
data_frame[["google_slr", "google_predictive"]] = data_frame.apply(lambda x: get_google_results(x.origin, x.destination, x.time),
                                                                   axis=1,
                                                                   result_type="expand")
diff = (data_frame.google_predictive - data_frame.google_slr) / 60
plt.hist(diff, bins=10)
plt.title("Google Predictive and SLR comparison")
plt.xlabel("Difference between predictive and SLR in minutes")
plt.ylabel("Number of samples")
plt.show()

Output plot

Our main question is: How does SLR is calculated in google maps? Is it a 24 hours average, or is it a time-slot-based average over the day? Maybe we have some mistake in our code?

Sagi Shadur
  • 226
  • 1
  • 4
  • 14

0 Answers0