/
generateDistanceGrid.py
366 lines (276 loc) · 10.7 KB
/
generateDistanceGrid.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import json, requests
import time
import sys,os
import json
import datetime
import time
import argparse
sys.path.append("/Users/feeley19/coding/projects/googlemaps/directionsDatabase")
from pprint import pprint
os.environ["DJANGO_SETTINGS_MODULE"] = "directionsDatabase.settings"
# from django.contrib.auth.models import *
from directions.models import Area,LocationInArea,Location,Area,Departure,Path,PathGrid
import django
import random
import math
django.setup()
TEN_MINS_WALKING_Y=51.602626-51.595441
FIVE_MINS_WALKING_Y=TEN_MINS_WALKING_Y*0.5
TWENTY_MINS_WALKING_Y=TEN_MINS_WALKING_Y*2
FIFTEEN_MINS_WALKING_Y=TEN_MINS_WALKING_Y*1.5
TEN_MINS_WALKING_X=-0.076213+0.087382
FIVE_MINS_WALKING_X=TEN_MINS_WALKING_X*0.5
TWENTY_MINS_WALKING_X=TEN_MINS_WALKING_X*2
FIFTEEN_MINS_WALKING_X=TEN_MINS_WALKING_X*1.5
API_KEYS=[
'AIzaSyCewGtfayH4MKxjtHpqJjpol8Q2dcKCDW8',
'AIzaSyAV2BoxzjN22pTU6hqTKKfp4Y0t-i0pruo',
'AIzaSyDGzdGimAgbz1kVrcLFqpW4WA_KNyIMTQ8',
'AIzaSyDNqOz8L5kzDwPXVIaVm9Xf5_dujUNd1Y4',
'AIzaSyDSIrlHEsnnxYdRh8bFZLVqv4uzeRtl6CA'
]
def getNextDateTime(now,day,hour):
days={"monday":0,"tuesday":1,"wednesday":2,"thursday":3,"friday":4,"saturday":5,"sunday":6}
time = datetime.time(hour=hour)
for key in days.keys():
if key.startswith(day):
dayNo=days[key]
if now.time() < time:
now = now.combine(now.date(),time)
else:
now = now.combine(now.date(),time) + datetime.timedelta(days=1)
return now + datetime.timedelta((dayNo - now.weekday()) % 7)
def unixTime(datetime):
return int(time.mktime(datetime.timetuple()))
def printLoadingBar(percentageX, percentageY,eta):
max=100.0
printy= "["
for i in range(0,int(max)):
if float(i)/int(max)<percentageY:
printy+='I'
else:
printy+='-'
printy+="]"
if percentageX !=-1:
print printy[0:1+int(max*(percentageX))]+"O"+printy[1+int(max*(percentageX)):len(printy)]+eta,
else:
print printy+eta,
def loadFromJson(path):
json_data=open(path)
data = json.load(json_data)
return data
def getAllLocationsInArea(area):
return LocationInArea.objects.filter(area=area,valid=True)
def makeLocationInArea(loc,area):
locs=LocationInArea.objects.filter(area=area,latitude=loc[0],longitude=loc[1])
if len(locs)==1:
print "Already been here.",
return locs[0]
elif len(locs)==0:
print "New loc.",
inArea=isPointInArea(area,loc[0],loc[1])
loc= LocationInArea(area=area,latitude=loc[0],longitude=loc[1],valid=inArea)
loc.save()
return loc
def loadArea(name):
area=Area.objects.filter(name=name)[0]
return area
def loadPath(pathGrid,locationInArea):
paths=Path.objects.filter(pathGrid=pathGrid,locationInArea=locationInArea)
if len(paths)==1 and paths[0].seconds!=-1:
return paths[0]
else:
return None
def loadPlace(name):
location=Location.objects.filter(name=name)[0]
return location
def makeDeparture(time):
departs=Departure.objects.filter()
for departure in departs:
if departure.time.weekday() == time.weekday() and departure.time.time() == time.time():
return departure
depart= Departure(time=time)
depart.save()
return depart
def getApiKey():
return API_KEYS[int(random.random()*len(API_KEYS))]
def makePath(pathGrid,locationInArea,seconds,delay):
paths=Path.objects.filter(pathGrid=pathGrid,locationInArea=locationInArea)
if len(paths)==1:
path=paths[0]
path.seconds=seconds
path.delay=delay
path.save()
print "Update path, %s seconds long."%str(path.seconds),
return path
elif len(paths)==0:
path= Path(pathGrid=pathGrid,locationInArea=locationInArea,seconds=seconds,delay=delay)
path.save()
print "New path, %s seconds long."%str(path.seconds),
return path
def makePathGrid(location,area,fromLocationToArea,departure,mode):
pathGrids=PathGrid.objects.filter(location=location,area=area,fromLocationToArea=fromLocationToArea,departure=departure,mode=mode)
if len(pathGrids)==1:
return pathGrids[0]
elif len(pathGrids)==0:
pathGrid= PathGrid(location=location,area=area,fromLocationToArea=fromLocationToArea,departure=departure,mode=mode)
pathGrid.save()
return pathGrid
def secondsToGetTo(pathGrid,loc):
# return 999999,0
time.sleep(1)
seconds=-1
if pathGrid.fromLocationToArea:
fromLoc=pathGrid.location
destLoc=loc
else:
fromLoc=loc
destLoc=pathGrid.location
unix_time = unixTime(pathGrid.departure.time)
url="https://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&key=%s&&departure_time=%d&mode=%s"
url= url%(fromLoc.latitude,fromLoc.longitude,destLoc.latitude,destLoc.longitude,getApiKey(),unix_time,pathGrid.mode)
resp = requests.get(url=url)
data = json.loads(resp.text)
routes=data['routes']
try:
seconds=data['routes'][0]['legs'][0]['duration']['value']
if pathGrid.mode=='transit':
departure_time=data['routes'][0]['legs'][0]['departure_time']['value']
delay=departure_time - unix_time
else:
delay=0
return seconds,delay
except Exception, e:
if data.get('status') and data.get('status')=='OVER_QUERY_LIMIT':
print data.get('error_message')
sys.exit()
return -1,0
def getGrid(area, place,depart,mode,fromLocationToArea):
place=loadPlace(place)
area=loadArea(area)
pathGrid=makePathGrid(place,area,fromLocationToArea,depart,mode)
# radius=0.2
height=area.topLeftLat-area.bottomRightLat
width=area.bottomRightLong-area.topLeftLong
time_marker=time.time()
yGridDensity=int(math.fabs(height)/FIVE_MINS_WALKING_Y)
xGridDensity=int(math.fabs(width)/FIVE_MINS_WALKING_X)
previousLoc=None
previousPath=None
if not area.mapped:
# Go over the whole grid and figure out whats valid
for y in range(0,yGridDensity):
for x in range(0,xGridDensity):
eta=time.time()-time_marker
eta=eta*((xGridDensity*yGridDensity)-(1+(y*yGridDensity)+x))
if eta>(60*60):
mins=eta/60
remainingMins=mins%60
hours=(mins-remainingMins)/60
etaString="%d hours, %d mins to go (%d). "%(hours,remainingMins,mins)
elif eta>(60):
etaString="%d mins to go. "%(eta/60)
else:
etaString="%d seconds to go. "%(eta)
printLoadingBar(float(x)/float(xGridDensity),float(y)/float(yGridDensity),etaString)
time_marker=time.time()
loc=[area.topLeftLat-y*(height/(yGridDensity-1)),area.topLeftLong+x*(width/(xGridDensity-1))]
loc=makeLocationInArea(loc,area)
if loc.valid: print "Its valid.",
else:
print "Its not valid.",
previousLoc=loc
path=loadPath(pathGrid,loc)
if not path or path.seconds==0:
if loc.valid:
seconds,delay=secondsToGetTo(pathGrid,loc)
else:
seconds=0
delay=0
makePath(pathGrid,loc,seconds,delay)
else:
seconds = -1
delay=0
print ''
else:
# Itterate over every valid location
validLocations=getAllLocationsInArea(area)
count=0.0
for loc in validLocations:
eta=time.time()-time_marker
eta=eta*(len(validLocations)-count)
if eta>(60*60):
mins=eta/60
remainingMins=mins%60
hours=(mins-remainingMins)/60
etaString="%d hours, %d mins to go (%d). "%(hours,remainingMins,mins)
elif eta>(60):
etaString="%d mins to go. "%(eta/60)
else:
etaString="%d seconds to go. "%(eta)
printLoadingBar(-1,count/float(len(validLocations)),etaString)
time_marker=time.time()
path=loadPath(pathGrid,loc)
if not path or path.seconds==0:
seconds,delay=secondsToGetTo(pathGrid,loc)
makePath(pathGrid,loc,seconds,delay)
else:
seconds = -1
delay=0
print ''
count=count+1.0
def getGridAtTime(loc, area,day,hour,mode,toArea):
now = datetime.datetime.now()
then = getNextDateTime(now,day,hour)
depart=makeDeparture(then)
getGrid(area, loc,depart,mode,toArea)
def printGrid(grid):
for row in grid:
for item in row:
mins=item['seconds']/60
if mins>60:
hours=mins/60.0
print "%.2f hours"%hours,
else:
print mins,"mins ",
print
print
for row in grid:
for item in row:
print item['loc'].latitude,item['loc'].longitude," ",
print
def getAddress(lat,lon):
url="http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true"%(lat,lon)
resp = requests.get(url=url)
data = json.loads(resp.text)
return data
def isPointInArea(area,lat,lon):
time.sleep(.3)
data=getAddress(lat,lon)
if not data.get('error_message'):
for address_components in data['results']:
for addressComponent in address_components['address_components']:
if addressComponent['long_name']==area.longName:
return True
return False
else:
# if data.get('error_message'):
print "No results", data.get('error_message')
sys.exit()
# print data['results'],
# return False
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-day',default='friday')
parser.add_argument('-hour',type=int,default='17')
parser.add_argument('-area',default='london')
parser.add_argument('-loc',default='home')
parser.add_argument('-mode',default='transit')
parser.add_argument('-toLocation',action='store_true',default=False)
args = parser.parse_args()
if args.toLocation:
print "from %s to %s"%(args.area, args.loc)
else:
print "from %s to %s"%(args.loc, args.area)
print "at %d:00 this %s"%(args.hour, args.day)
getGridAtTime(args.loc, args.area,args.day,args.hour,args.mode,not(args.toLocation))