|
|
@@ -1,5 +1,8 @@
|
|
|
+import heapq
|
|
|
import hashlib
|
|
|
import json
|
|
|
+import random
|
|
|
+
|
|
|
import requests
|
|
|
|
|
|
import config
|
|
|
@@ -65,17 +68,23 @@ def directions_call(source, destination, mode):
|
|
|
config.WORKPLACE_TARGET_TIME,
|
|
|
)
|
|
|
|
|
|
- #resp = requests.get(url % params)
|
|
|
- # print(resp.status_code)
|
|
|
- # print(json.dumps(resp.json()))
|
|
|
+ resp = requests.get(url % params)
|
|
|
+ print(resp.status_code)
|
|
|
+ print(json.dumps(resp.json()))
|
|
|
|
|
|
- resp_data = data.x
|
|
|
+ resp_data = resp.json()
|
|
|
+ print('"%s"' % config.GCLOUD_API_KEY)
|
|
|
|
|
|
- _record_api_response(resp_data)
|
|
|
+
|
|
|
+ if not resp_data.get('routes'):
|
|
|
+ return
|
|
|
|
|
|
|
|
|
+ # resp_data = data.x
|
|
|
+
|
|
|
+ _record_api_response(resp_data)
|
|
|
|
|
|
-def load_data():
|
|
|
+def load_graph():
|
|
|
|
|
|
graph = {
|
|
|
config.WORKPLACE: {}
|
|
|
@@ -85,37 +94,149 @@ def load_data():
|
|
|
rows = conn.select('select id, mode, dest_lat, dest_lon, src_lat, src_lon, duration from transit')
|
|
|
|
|
|
for key, mode, src_lat, src_lon, dest_lat, dest_lon, duration in rows:
|
|
|
- loc = src_lat, src_lon
|
|
|
+ src = src_lat, src_lon
|
|
|
+ dest = dest_lat, dest_lon
|
|
|
|
|
|
- if loc not in graph:
|
|
|
- graph[loc] = {}
|
|
|
+ if dest not in graph:
|
|
|
+ graph[dest] = {}
|
|
|
+
|
|
|
+ if src not in graph:
|
|
|
+ graph[src] = {}
|
|
|
|
|
|
for lat, lon in graph:
|
|
|
- if 0 < geo_util.ggcd(lon, lat, src_lon, src_lat) < 200:
|
|
|
+ distance = int(geo_util.ggcd(lon, lat, src_lon, src_lat))
|
|
|
+ if 0 < distance < 500:
|
|
|
k = 'w-' + _get_key(lon, lat, src_lon, src_lat)
|
|
|
- graph[loc][k] = {
|
|
|
- 'mode': TransitMethod.WALKING,
|
|
|
+ est_walk_duration = distance
|
|
|
+ graph[src][k] = {
|
|
|
+ 'mode': TransitMethod.WALKING.upper(),
|
|
|
'dest': (lat, lon),
|
|
|
- 'duration': 0,
|
|
|
+ 'duration': est_walk_duration,
|
|
|
}
|
|
|
graph[lat,lon][k] = {
|
|
|
- 'mode': TransitMethod.WALKING,
|
|
|
- 'dest': loc,
|
|
|
- 'duration': 0,
|
|
|
+ 'mode': TransitMethod.WALKING.upper(),
|
|
|
+ 'dest': src,
|
|
|
+ 'duration': est_walk_duration,
|
|
|
}
|
|
|
|
|
|
- graph[loc][key] = {
|
|
|
+
|
|
|
+ graph[src][key] = {
|
|
|
'mode': mode,
|
|
|
'dest': (dest_lat, dest_lon),
|
|
|
'duration': duration,
|
|
|
}
|
|
|
|
|
|
- for k, v in graph.items():
|
|
|
- print((k,v))
|
|
|
+ # for k, v in graph.items():
|
|
|
+ # print((k,v))
|
|
|
|
|
|
- print(graph[config.WORKPLACE])
|
|
|
+ # print(graph[config.WORKPLACE])
|
|
|
|
|
|
+ return graph
|
|
|
|
|
|
# directions_call((53.388631,-6.071959), config.WORKPLACE, TransitMethod.TRANSIT)
|
|
|
|
|
|
-load_data()
|
|
|
+
|
|
|
+def determine_fastest_paths(graph, source):
|
|
|
+ indices = {}
|
|
|
+ for loc in graph:
|
|
|
+ if loc not in indices:
|
|
|
+ indices[loc] = len(indices)
|
|
|
+
|
|
|
+ # for loc in graph:
|
|
|
+ # print('%i: %s' % (indices[loc], loc))
|
|
|
+ # for k in graph[loc]:
|
|
|
+ # print('\t%i\t%i\t%s\t%s' % (indices[graph[loc][k]['dest']], graph[loc][k]['duration'], graph[loc][k]['mode'], k))
|
|
|
+
|
|
|
+ queue = [(0, source, [])]
|
|
|
+ heapq.heapify(queue)
|
|
|
+
|
|
|
+ current_min_durations = {}
|
|
|
+
|
|
|
+ # print('\n\n\n')
|
|
|
+
|
|
|
+ i = 0
|
|
|
+
|
|
|
+ while queue:
|
|
|
+ current_duration, current_source, current_path = heapq.heappop(queue)
|
|
|
+
|
|
|
+ if current_source in current_min_durations and current_min_durations[current_source] < current_duration:
|
|
|
+ continue
|
|
|
+
|
|
|
+ # print('%i\t%s\t%i\t%s' % (indices[current_source], current_source, current_duration, current_path))
|
|
|
+
|
|
|
+ current_min_durations[current_source] = current_duration
|
|
|
+
|
|
|
+ for key, dest_data in graph[current_source].items():
|
|
|
+ heapq.heappush(queue, (
|
|
|
+ current_duration + max(1, dest_data['duration']),
|
|
|
+ dest_data['dest'],
|
|
|
+ current_path + [key]
|
|
|
+ ))
|
|
|
+
|
|
|
+ i += 1
|
|
|
+
|
|
|
+ return current_min_durations
|
|
|
+
|
|
|
+def write_kml(commutes, fname):
|
|
|
+ with open(fname, 'w') as f:
|
|
|
+ f.write('''<?xml version="1.0" encoding="UTF-8"?>
|
|
|
+ <kml xmlns="http://www.opengis.net/kml/2.2">
|
|
|
+ <Document>
|
|
|
+ ''')
|
|
|
+
|
|
|
+ for i in range(256):
|
|
|
+ hexvalue = hex(255-i)[2:].lower()
|
|
|
+ if len(hexvalue) == 1:
|
|
|
+ hexvalue = '0' + hexvalue
|
|
|
+ f.write('<Style id="color-%i"><IconStyle><color>ff%s%sff</color></IconStyle></Style>\n' % (i, hexvalue, hexvalue))
|
|
|
+
|
|
|
+ max_duration = min(3600, max(commutes.values()))
|
|
|
+
|
|
|
+
|
|
|
+ f.write('<Folder>\n')
|
|
|
+ f.write('<name>Commutes</name>\n')
|
|
|
+ for loc, duration in commutes.items():
|
|
|
+ color = 20 + int((duration/float(max_duration)) * 236)
|
|
|
+ f.write('\t<Placemark><Point><coordinates>%f,%f</coordinates></Point><styleUrl>#color-%i</styleUrl></Placemark>\n' % (loc[1], loc[0], color))
|
|
|
+ f.write('</Folder>')
|
|
|
+
|
|
|
+
|
|
|
+ f.write('''
|
|
|
+ </Document>
|
|
|
+ </kml>
|
|
|
+ ''')
|
|
|
+
|
|
|
+def analyze_locations(commutes):
|
|
|
+ with db.connect() as conn:
|
|
|
+ rows = conn.select('select id, lat, lon from properties where lat is not null and lon is not null')
|
|
|
+
|
|
|
+
|
|
|
+ random.shuffle(rows)
|
|
|
+
|
|
|
+ to_load = []
|
|
|
+
|
|
|
+ for property_id, lat, lon in rows:
|
|
|
+ closest = None
|
|
|
+ closest_dist = None
|
|
|
+
|
|
|
+ for loc in commutes:
|
|
|
+ dist = abs(loc[0] - lat) ** 2 + abs(loc[1] - lon) ** 2
|
|
|
+ if closest_dist is None or dist < closest_dist:
|
|
|
+ closest = loc
|
|
|
+ closest_dist = dist
|
|
|
+
|
|
|
+ if geo_util.ggcd(lon, lat, closest[1], closest[0]) > 200:
|
|
|
+ to_load.append((lat,lon))
|
|
|
+
|
|
|
+ print('Remaining to load: %i' % len(to_load))
|
|
|
+
|
|
|
+ directions_call(to_load[0], config.WORKPLACE, TransitMethod.TRANSIT)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ graph = load_graph()
|
|
|
+ commutes = determine_fastest_paths(graph, config.WORKPLACE)
|
|
|
+ write_kml(commutes, 'vis.kml')
|
|
|
+
|
|
|
+ # analyze_locations(commutes)
|