-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_distance_matrix.py
120 lines (93 loc) · 4.63 KB
/
test_distance_matrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
This file focuses primarily on using the Distance Matrix API. There is currently
no compatibility with the 'subwayChallenge' or 'dataProcessing' repositories.
Therefore, this file works INDEPENDENTLY and can be run on its own (along with API key
text file).
Refer to the link below for details and an overview of how it works.
https://developers.google.com/maps/documentation/distance-matrix/overview
"""
import requests # Allows us to make HTTP requests to Google maps
"""
Given two addresses, computes the walking time between them.
The address should typically follow the parameter of:
1313 Disneyland Dr, Anaheim, CA 92802
[Street #] [Street Name], [City], [State Initials] [Zip Code]
This includes spacing and comma, but slight variations in syntax
should be acceptable in some cases.
@input: addressA - starting address
addressB - destination address
@output: URL link to the JSON-formatted result
@return: walking_time - Time in minutes it takes to walk between the two addresses
"""
def address_walking_time(addressA, addressB):
# Securely read the API key
api_file = open("api-key.txt", "r")
api_key = api_file.readline()
api_file.close()
# Starting address parsing
starting_point = addressA.replace(" ", "+")
# Destination address parsing
destination_point = addressB.replace(" ", "+")
# Base URL for accessing distance matrix API. Already set to walking directions!
url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&mode=walking&"
url_test = url + "origins=" + starting_point + "&destinations=" + destination_point + "&key=" + api_key
print(url_test)
# Sends a Get request to Google Maps API server
r = requests.get(url + "origins=" + starting_point + "&destinations=" +
destination_point + "&key=" + api_key)
# return time as text and seconds
walking_time = r.json()["rows"][0]["elements"][0]["duration"]["text"]
seconds = r.json()["rows"][0]["elements"][0]["duration"]["value"]
# Print the time
output_time(addressA, addressB, walking_time)
return walking_time
"""
Given two stations, computes the walking time between them.
The station input should be given by its default name
(i.e. "Astor Pl", "Canal St")
@input: stationA - starting station
stationB - destination station
@output: URL link to the JSON-formatted result
@return: walking_time - Time in minutes it takes to walk between the two stations
"""
def station_walking_time(stationA, stationB):
# Securely read the API key
api_file = open("api-key.txt", "r")
api_key = api_file.readline()
api_file.close()
# Starting address parsing
starting_point = stationA + " station"
starting_point = starting_point.replace("St", "Street")
starting_point = starting_point.replace(" ", "+")
# Destination address parsing
destination_point = stationB + " station"
destination_point = destination_point.replace("St", "Street")
destination_point= destination_point.replace(" ", "+")
# Base URL for accessing distance matrix API. Already set to walking directions!
url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&mode=walking&"
url_test = url + "origins=" + starting_point + "&destinations=" + destination_point + "&key=" + api_key
print(url_test)
# Sends a Get request to Google Maps API server
r = requests.get(url + "origins=" + starting_point + "&destinations=" +
destination_point + "&key=" + api_key)
# return time as text and seconds
walking_time = r.json()["rows"][0]["elements"][0]["duration"]["text"]
seconds = r.json()["rows"][0]["elements"][0]["duration"]["value"]
# Print the time
output_time(stationA, stationB, walking_time)
return walking_time
def output_time(origin, destination, time):
print(origin + " =====> " + destination + " " + time +"\n")
# Test cases
def main():
### ADDRESS TEST CASES ###
address_walking_time_test_A = address_walking_time("1761 15th St, Troy, NY 12180",
"124 4th St, Troy, NY 12180")
### STATION TEST CASES ###
# NOTE: "36th Street" should be in Queens, but the code treats it as Brooklyn
stations = ["Astor Pl", "Canal St", "50th St", "Bergen St", "Pennsylvania Ave",
"238th St", "Cathedral Pkwy", "Kingston - Throop Aves", "65th St",
"36th St", "Delancey St - Essex St", "Van Siclen Ave", "Norwood Ave"]
for i in range(0,len(stations)-1):
station_walking_time(stations[i], stations[i+1])
main()