5

I am using the Python client library for Google Maps API (Github link).

I would like to get the duration_in_traffic figures for the following trips:

  • Address1 to Address2

  • Address2 to Address1

  • Address1 to Address3

  • Address3 to Address1

But the syntax of the Google Distance Matrix API (documentation) is problematic in its presumptuousness. It assumes that the user wants data for all conceivable pairings of origin and destination addresses.

The issue is that the following code (source)...

origins = [Address1, Address2, Address3]
destinations = [Address1, Address2, Address3]

matrix = client.distance_matrix(origins, destinations,
                                        mode="driving",
                                        language="en",
                                        units="imperial",
                                        departure_time=now,
                                        traffic_model="best_guess")

produces nine results (that is, every possible combination of origin-destination pairings), when I only need the previously-stated four.

I realize that I can parse these results to extract only the four durations that I need, but I don't want to needlessly double the execution time of my script by forcing it to download data that I do not want.

I also realize that I can send 4 separate requests to get just the durations that I want (that is, one pairing per request), but I am trying not to surpass the quota for my free, non-premium Distance Matrix API account.

So, is it possible to specify the origin-destination pairings within the same request?

M--
  • 25,431
  • 8
  • 61
  • 93
Crickets
  • 524
  • 1
  • 8
  • 23
  • 1
    I don't think it is because the only purpose of such a request would be to sidestep the free API limits (in the sense that anyone could construct their multiple A --> B requests as a partial matrix and make it one API call). I think you'll have to call the whole matrix each time. – roganjosh Jun 02 '18 at 12:50
  • 1
    If the API limit was the issue you could loop through your origin destination pairs with each API request having only one origin and one destination. This would be slower than the batch method though. – Chris Jun 02 '18 at 12:54
  • 1
    What problem are you actually trying to solve? A self-hosted OSRM instance is good for routing, and you learn traffic profiles over time to make corrections to its predictions. – roganjosh Jun 02 '18 at 12:56
  • @roganjosh, I'm sorry, but I genuinely do not understand this logic. I can create a massive matrix of 20 origins and 20 destinations per request, but Google doesn't want me to create specific pairings because this would be "sidestepping" the API limits? Isn't the current setup bad for Google, because now I am forced to download 2x more data per request than I actually need? I'm not disagreeing with you, I just don't understand it. If Google wanted to tighten the limit on requests, they would simply allow only 1 origin and 1 destination per request. – Crickets Jun 02 '18 at 13:07
  • @Chris, I think what you've described is exactly what I'm looking for? Can you please show me how to do this? – Crickets Jun 02 '18 at 13:16
  • No, because they can set the API limits separately. So if I can do what you're asking, I could make 100000 individual A --> B trips into a single matrix. If you were talking about a vehicle routing problem, then each problem gets its own matrix, but now you're opening a door where I could get, in effect, multiple square matrices from a single call. – roganjosh Jun 02 '18 at 13:17
  • In other words, if I wanted two 3x3 matrices, I could just send that request in your format (decomposing it into individual journeys) and get the result with a single query. – roganjosh Jun 02 '18 at 13:20
  • In any case, you didn't answer what your ultimate problem is, because there could be a better way than relying on the free Google API limits (unless you _need_ real-time traffic) – roganjosh Jun 02 '18 at 13:29
  • @roganjosh, The application is to display real-time, current traffic data so that one has a good idea of when they need to leave their origin point to arrive at their destination, as well as an idea of how much time is needed to return to the origin point. So, it's not so much about predicting traffic as it is about reliably reflecting the current traffic times. Of course, traffic does tend to follow patterns, but I am more concerned about the exceptions to the norms (because the exceptions are what catch you off-guard). I don't know of a free and accurate alternative to Google's DM API. – Crickets Jun 02 '18 at 14:05
  • 1
    Fair enough, it looks like you're tied to the API. I just don't think it's possible to do what you want to do. – roganjosh Jun 02 '18 at 14:07

1 Answers1

0

If I understood the question correctly I think you can solve it by using a dictionary:

dict_addresses= {Address1:Address2, Address2:Address1, Address1:Address3, Address3:Address1}

db= dict()

for origin in dict_addresses:
    duration_traffic= client.distance_matrix(origin, dict_addresses[origin], mode="driving", language="en", units="imperial", departure_time=now, traffic_model="best_guess")
    db[origin]= duration_traffic
    print (db)

For the last part you need to extract the duration_in_traffic from the Distance Matrix array but you should be getting only the 4 results you actually want.