googlemaps distance_matrix Creating a Matrix of Re

2020-04-02 06:09发布

问题:

So I am trying to do a project for work where I create a matrix where the x-axis is a list of stores and the y axis is another list of stores and the values are the distances returned see the link below for an example of what im looking for

Example Output

This is the code I am running with dummy addresses for lst_stores1 and lst_stores2

INPUT:

import googlemaps
lst_store1 = ['777 Brockton Avenue, Abington MA 2351',
            '30 Memorial Drive, Avon MA',
            '250 Hartford Avenue, Bellingham MA',
            '700 Oak Street, Brockton MA',
            '591 Memorial Dr, Chicopee MA']

lst_store2 = ['55 Brooksby Village Way, Danvers MA',
'137 Teaticket Hwy, East Falmouth MA',
'42 Fairhaven Commons Way, Fairhaven MA',
'374 William S Canning Blvd, Fall River MA',
'121 Worcester Rd, Framingham MA']

my_dist = gmaps.distance_matrix(lst_store1,lst_store2)
print(my_dist)

OUTPUT:

'destination_addresses': ['55 Brooksby Village Dr, Danvers, MA 01923, USA', '137 Teaticket Hwy, Teaticket, MA 02536, USA', '42 Fairhaven Commons Way, Fairhaven, MA 02719, USA', '374 William S Canning Blvd, Fall River, MA 02721, USA', '121 Worcester Rd, Framingham, MA 01701, USA'], 'origin_addresses': ['777 Brockton Ave, Abington, MA 02351, USA', '30 Memorial Dr, Avon, MA 02322, USA', '250 Hartford Ave, Bellingham, MA 02019, USA', '700 Oak St, Brockton, MA 02301, USA', '591 Memorial Dr, Chicopee, MA 01020, USA'], 'rows': [{'elements': [{'distance': {'text': '65.0 km', 'value': 65015}, 'duration': {'text': '1 hour 4 mins', 'value': 3860}, 'status': 'OK'}, {'distance': {'text': '89.0 km', 'value': 89014}, 'duration': {'text': '1 hour 14 mins', 'value': 4437}, 'status': 'OK'}, {'distance': {'text': '72.4 km', 'value': 72367}, 'duration': {'text': '56 mins', 'value': 3339}, 'status': 'OK'}, {'distance': {'text': '63.4 km', 'value': 63418}, 'duration': {'text': '51 mins', 'value': 3034}, 'status': 'OK'}, {'distance': {'text': '58.7 km', 'value': 58690}, 'duration': {'text': '50 mins', 'value': 2998}, 'status': 'OK'}]}, {'elements': [{'distance': {'text': '62.6 km', 'value': 62649}, 'duration': {'text': '53 mins', 'value': 3189}, 'status': 'OK'}, {'distance': {'text': '96.8 km', 'value': 96832}, 'duration': {'text': '1 hour 5 mins', 'value': 3889}, 'status': 'OK'}, {'distance': {'text': '70.4 km', 'value': 70413}, 'duration': {'text': '46 mins', 'value': 2788}, 'status': 'OK'}, {'distance': {'text': '61.5 km', 'value': 61463}, 'duration': {'text': '41 mins', 'value': 2483}, 'status': 'OK'}, {'distance': {'text': '50.5 km', 'value': 50512}, 'duration': {'text': '38 mins', 'value': 2273}, 'status': 'OK'}]}, {'elements': [{'distance': {'text': '95.3 km', 'value': 95321}, 'duration': {'text': '1 hour 2 mins', 'value': 3702}, 'status': 'OK'}, {'distance': {'text': '115 km', 'value': 115239}, 'duration': {'text': '1 hour 14 mins', 'value': 4436}, 'status': 'OK'}, {'distance': {'text': '90.2 km', 'value': 90161}, 'duration': {'text': '57 mins', 'value': 3427}, 'status': 'OK'}, {'distance': {'text': '81.2 km', 'value': 81211}, 'duration': {'text': '52 mins', 'value': 3122}, 'status': 'OK'}, {'distance': {'text': '39.8 km', 'value': 39785}, 'duration': {'text': '29 mins', 'value': 1710}, 'status': 'OK'}]}, {'elements': [{'distance': {'text': '65.5 km', 'value': 65521}, 'duration': {'text': '55 mins', 'value': 3323}, 'status': 'OK'}, {'distance': {'text': '91.4 km', 'value': 91450}, 'duration': {'text': '1 hour 2 mins', 'value': 3726}, 'status': 'OK'}, {'distance': {'text': '65.0 km', 'value': 65031}, 'duration': {'text': '44 mins', 'value': 2625}, 'status': 'OK'}, {'distance': {'text': '56.1 km', 'value': 56081}, 'duration': {'text': '39 mins', 'value': 2320}, 'status': 'OK'}, {'distance': {'text': '53.4 km', 'value': 53385}, 'duration': {'text': '40 mins', 'value': 2407}, 'status': 'OK'}]}, {'elements': [{'distance': {'text': '168 km', 'value': 167923}, 'duration': {'text': '1 hour 43 mins', 'value': 6187}, 'status': 'OK'}, {'distance': {'text': '227 km', 'value': 227118}, 'duration': {'text': '2 hours 17 mins', 'value': 8217}, 'status': 'OK'}, {'distance': {'text': '183 km', 'value': 183401}, 'duration': {'text': '1 hour 54 mins', 'value': 6818}, 'status': 'OK'}, {'distance': {'text': '163 km', 'value': 163452}, 'duration': {'text': '1 hour 43 mins', 'value': 6170}, 'status': 'OK'}, {'distance': {'text': '112 km', 'value': 112386}, 'duration': {'text': '1 hour 10 mins', 'value': 4196}, 'status': 'OK'}]}], 'status': 'OK'}

I'm having trouble deciphering the output it's returning and figuring out how to convert it into the format I described above.

P.S. I have imported the appropriate libraries and set up my API key etc it is being referenced in a different cell in jupyter

回答1:

For what I understand, you want to get the corresponding distance of a store from x to each store in y. What I done to achieve this is to put a for loop to each store in both list and sending the distance matrix request for each combination. This is so,I can put the result of each combination to the array. I then use a for loop to with the range of length to lookup the index on my result array so that I can get each result from the array. Here is a sample code. Here is the breakdown of my code:

First, I used these lines from your code:

import googlemaps

lst_store1 = ['777 Brockton Avenue, Abington MA 2351',
            '30 Memorial Drive, Avon MA',
            '250 Hartford Avenue, Bellingham MA',
            '700 Oak Street, Brockton MA',
            '591 Memorial Dr, Chicopee MA']

lst_store2 = ['55 Brooksby Village Way, Danvers MA',
'137 Teaticket Hwy, East Falmouth MA',
'42 Fairhaven Commons Way, Fairhaven MA',
'374 William S Canning Blvd, Fall River MA',
'121 Worcester Rd, Framingham MA']

Then I added the line where I put the API Key. Note that To properly use the Distance Matrix API, you need to have an API Key.

gmaps = googlemaps.Client(key='YOUR_API_KEY_HERE') 

Then declare an empty array where I will put my results:

my_result= []

Then use a for loop for lst_store1 array and put a for loop inside it for lst_store2 array. Then append the result of the distance matrix between each combination of your array. Here are the code:

for x in lst_store1:
  for y in lst_store2:
    my_result.append(gmaps.distance_matrix(x,y))

You will get something like these combination:

orig: 777 Brockton Avenue, Abington MA 2351 dest: 55 Brooksby Village Way, Danvers MA
orig: 777 Brockton Avenue, Abington MA 2351 dest: 137 Teaticket Hwy, East Falmouth MA
orig: 777 Brockton Avenue, Abington MA 2351 dest:42 Fairhaven Commons Way, Fairhaven MA
orig: 777 Brockton Avenue, Abington MA 2351 dest:374 William S Canning Blvd, Fall River MA
orig: 777 Brockton Avenue, Abington MA 2351 dest: 121 Worcester Rd, Framingham MA
orig: 30 Memorial Drive, Avon MA dest: 55 Brooksby Village Way, Danvers MA
orig: 30 Memorial Drive, Avon MA 2351 dest: 137 Teaticket Hwy, East Falmouth MA

... and so on. You'll have 25 combinations from your list.

Then I put a for loop using range of length so that we can get the index. You can also edit the format on how you print the output of the array.

for z in range(len(my_result)):
    print("{}- from:{} to:{} distance:{}".format(z+1, my_result[z]['origin_addresses'],my_result[z]['destination_addresses'], my_result[z]['rows'][0]['elements'][0]['distance']['value'] ))

This will be the sample output of the print from the for loop which will be easier for you to sort:

1- from:['777 Brockton Ave, Abington, MA 02351, USA'] to:['55 Brooksby Village Dr, Danvers, MA 01923, USA'] distance:65015
2- from:['777 Brockton Ave, Abington, MA 02351, USA'] to:['137 Teaticket Hwy, Teaticket, MA 02536, USA'] distance:88198
3- from:['777 Brockton Ave, Abington, MA 02351, USA'] to:['42 Fairhaven Commons Way, Fairhaven, MA 02719, USA'] distance:72367
4- from:['777 Brockton Ave, Abington, MA 02351, USA'] to:['374 William S Canning Blvd, Fall River, MA 02721, USA'] distance:63418
5- from:['777 Brockton Ave, Abington, MA 02351, USA'] to:['121 Worcester Rd, Framingham, MA 01701, USA'] distance:58690
6- from:['30 Memorial Dr, Avon, MA 02322, USA'] to:['55 Brooksby Village Dr, Danvers, MA 01923, USA'] distance:62649
7- from:['30 Memorial Dr, Avon, MA 02322, USA'] to:['137 Teaticket Hwy, Teaticket, MA 02536, USA'] distance:96832

Hope this helps!