-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplacerequest.py
174 lines (139 loc) · 5.16 KB
/
placerequest.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import urllib, json
from kmeans import kmean
AUTH_KEY = 'AIzaSyAG0fCOK8Qh9iODUjcK2TexVWVEBcw3MO4'
class FindRestaurants():
def __init__(self, origin, destination):
self.origin = origin
self.destination = destination
# get parameters to use Google Places
jsonData = FindDir(self)
if (jsonData == "No Walking Path Found"):
print(jsonData)
return
distance = jsonData["routes"][0]["legs"][0]["distance"]["value"]
decodedPoly = decode(jsonData["routes"][0]["overview_polyline"]["points"])
midPoint = decodedPoly[(int)(len(decodedPoly)/2)]
radius = distance*(3/2)
lat = midPoint[1]
lng = midPoint[0]
# run Google Places
open_restaurants = GoogPlac(lat, lng, radius)
latlong = []
for restaurant in open_restaurants["results"]:
latlong.append(PlaceDetails(restaurant["place_id"]))
# doing k means clustering
waypoints_limit = 23
k = min(int(len(latlong)/3), waypoints_limit)
centroids, clusters = kmean(latlong, k)
centroidString = ""
for centroid in centroids:
centroidString += str(centroid)+"|"
newDirection = NewDir(self, centroidString)
# find the shortest path between two points
def FindDir(self):
MyUrl = ('https://maps.googleapis.com/maps/api/directions/json'
'?origin=%s'
'&destination=%s'
'&mode=walking'
'&key=%s') % (self.origin, self.destination, AUTH_KEY)
#grabbing the JSON result
response = urllib.urlopen(MyUrl)
jsonRaw = response.read()
jsonData = json.loads(jsonRaw)
status = str((jsonData["status"]))
no_results = "ZERO_RESULTS"
if (status == no_results):
return ("No Walking Path Found")
return jsonData
# decode polyline
def decode(point_str):
'''Decodes a polyline that has been encoded using Google's algorithm
http://code.google.com/apis/maps/documentation/polylinealgorithm.html
This is a generic method that returns a list of (latitude, longitude)
tuples.
:param point_str: Encoded polyline string.
:type point_str: string
:returns: List of 2-tuples where each tuple is (latitude, longitude)
:rtype: list
'''
# sone coordinate offset is represented by 4 to 5 binary chunks
coord_chunks = [[]]
for char in point_str:
# convert each character to decimal from ascii
value = ord(char) - 63
# values that have a chunk following have an extra 1 on the left
split_after = not (value & 0x20)
value &= 0x1F
coord_chunks[-1].append(value)
if split_after:
coord_chunks.append([])
del coord_chunks[-1]
coords = []
for coord_chunk in coord_chunks:
coord = 0
for i, chunk in enumerate(coord_chunk):
coord |= chunk << (i * 5)
#there is a 1 on the right if the coord is negative
if coord & 0x1:
coord = ~coord #invert
coord >>= 1
coord /= 100000.0
coords.append(coord)
# convert the 1 dimensional list to a 2 dimensional list and offsets to
# actual values
points = []
prev_x = 0
prev_y = 0
for i in xrange(0, len(coords) - 1, 2):
if coords[i] == 0 and coords[i + 1] == 0:
continue
prev_x += coords[i + 1]
prev_y += coords[i]
# a round to 6 digits ensures that the floats are the same as when
# they were encoded
points.append((round(prev_x, 6), round(prev_y, 6)))
return points
# Get all the restaurants currently open within specified radius
def GoogPlac(lat,lng,radius):
LOCATION = str(lat) + "," + str(lng)
RADIUS = radius
MyUrl = ('https://maps.googleapis.com/maps/api/place/nearbysearch/json'
'?location=%s'
'&radius=%s'
'&types=restaurant'
'&opennow=true'
'&key=%s') % (LOCATION, RADIUS, AUTH_KEY)
#grabbing the JSON result
response = urllib.urlopen(MyUrl)
jsonRaw = response.read()
jsonData = json.loads(jsonRaw)
return jsonData
# get place details
def PlaceDetails(placeid):
PLACEID = placeid
MyUrl = ('https://maps.googleapis.com/maps/api/place/details/json'
'?placeid=%s'
'&key=%s') % (PLACEID, AUTH_KEY)
#grabbing the JSON result
response = urllib.urlopen(MyUrl)
jsonRaw = response.read()
jsonData = json.loads(jsonRaw)
return jsonData["result"]["geometry"]["location"]["lat"], jsonData["result"]["geometry"]["location"]["lng"]
# calculate new directions with waypoints
def NewDir(self, centroidString):
MyUrl = ('https://maps.googleapis.com/maps/api/directions/json'
'?origin=%s'
'&destination=%s'
'&mode=walking'
'&waypoint=optimize:true|%s'
'&key=%s') % (self.origin, self.destination, centroidString, AUTH_KEY)
#grabbing the JSON result
response = urllib.urlopen(MyUrl)
jsonRaw = response.read()
jsonData = json.loads(jsonRaw)
status = str((jsonData["status"]))
no_results = "ZERO_RESULTS"
if (status == no_results):
return ("No Walking Path Found")
return jsonData
FindRestaurants('Georgetown University', 'Union Station')