|
|
@@ -0,0 +1,121 @@
|
|
|
+import hashlib
|
|
|
+import json
|
|
|
+import requests
|
|
|
+
|
|
|
+import config
|
|
|
+import data
|
|
|
+import db
|
|
|
+import geo_util
|
|
|
+
|
|
|
+class TransitMethod:
|
|
|
+ DRIVING = 'driving'
|
|
|
+ WALKING = 'walking'
|
|
|
+ CYCLING = 'bicycling'
|
|
|
+ TRANSIT = 'transit'
|
|
|
+
|
|
|
+def _get_key(*args):
|
|
|
+ k = ','.join([str(x) for x in args]).encode('utf-8')
|
|
|
+ return hashlib.md5(k).hexdigest()[-12:]
|
|
|
+
|
|
|
+def _record_api_response(response):
|
|
|
+
|
|
|
+ stmt = 'replace into transit (id, mode, submode, src_lat, src_lon, dest_lat, dest_lon, distance, duration, raw_json) VALUES'
|
|
|
+ params = []
|
|
|
+
|
|
|
+ for route in response.get('routes', []):
|
|
|
+ for leg in route.get('legs', []):
|
|
|
+ for step in leg.get('steps', []):
|
|
|
+ distance = step['distance']['value']
|
|
|
+ duration = step['duration']['value']
|
|
|
+ source = round(step['start_location']['lat'], 10), round(step['start_location']['lng'], 10)
|
|
|
+ destination = round(step['end_location']['lat'], 10), round(step['end_location']['lng'], 10)
|
|
|
+ mode = step['travel_mode']
|
|
|
+ submode = step.get('transit_details', {}).get('line', {}).get('vehicle', {}).get('type')
|
|
|
+
|
|
|
+ stmt += ' (?, ?, ?, ?, ?, ?, ?, ?, ?, ?),'
|
|
|
+ params.extend([
|
|
|
+ _get_key(source, destination, mode, submode),
|
|
|
+ mode,
|
|
|
+ submode,
|
|
|
+ source[0],
|
|
|
+ source[1],
|
|
|
+ destination[0],
|
|
|
+ destination[1],
|
|
|
+ distance,
|
|
|
+ duration,
|
|
|
+ json.dumps(step),
|
|
|
+ ])
|
|
|
+
|
|
|
+ with db.connect() as conn:
|
|
|
+ conn.execute(stmt[:-1], *params)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+def directions_call(source, destination, mode):
|
|
|
+ # doc: https://developers.google.com/maps/documentation/directions/intro
|
|
|
+
|
|
|
+ url = 'https://maps.googleapis.com/maps/api/directions/json?origin=%s,%s&destination=%s,%s&key=%s&mode=%s&arrival_time=%i&alternatives=true'
|
|
|
+ params = (
|
|
|
+ source[0],
|
|
|
+ source[1],
|
|
|
+ destination[0],
|
|
|
+ destination[1],
|
|
|
+ config.GCLOUD_API_KEY,
|
|
|
+ mode,
|
|
|
+ config.WORKPLACE_TARGET_TIME,
|
|
|
+ )
|
|
|
+
|
|
|
+ #resp = requests.get(url % params)
|
|
|
+ # print(resp.status_code)
|
|
|
+ # print(json.dumps(resp.json()))
|
|
|
+
|
|
|
+ resp_data = data.x
|
|
|
+
|
|
|
+ _record_api_response(resp_data)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+def load_data():
|
|
|
+
|
|
|
+ graph = {
|
|
|
+ config.WORKPLACE: {}
|
|
|
+ }
|
|
|
+
|
|
|
+ with db.connect() as conn:
|
|
|
+ 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
|
|
|
+
|
|
|
+ if loc not in graph:
|
|
|
+ graph[loc] = {}
|
|
|
+
|
|
|
+ for lat, lon in graph:
|
|
|
+ if 0 < geo_util.ggcd(lon, lat, src_lon, src_lat) < 200:
|
|
|
+ k = 'w-' + _get_key(lon, lat, src_lon, src_lat)
|
|
|
+ graph[loc][k] = {
|
|
|
+ 'mode': TransitMethod.WALKING,
|
|
|
+ 'dest': (lat, lon),
|
|
|
+ 'duration': 0,
|
|
|
+ }
|
|
|
+ graph[lat,lon][k] = {
|
|
|
+ 'mode': TransitMethod.WALKING,
|
|
|
+ 'dest': loc,
|
|
|
+ 'duration': 0,
|
|
|
+ }
|
|
|
+
|
|
|
+ graph[loc][key] = {
|
|
|
+ 'mode': mode,
|
|
|
+ 'dest': (dest_lat, dest_lon),
|
|
|
+ 'duration': duration,
|
|
|
+ }
|
|
|
+
|
|
|
+ for k, v in graph.items():
|
|
|
+ print((k,v))
|
|
|
+
|
|
|
+ print(graph[config.WORKPLACE])
|
|
|
+
|
|
|
+
|
|
|
+# directions_call((53.388631,-6.071959), config.WORKPLACE, TransitMethod.TRANSIT)
|
|
|
+
|
|
|
+load_data()
|