|
|
@@ -155,6 +155,10 @@ def load_graph():
|
|
|
def determine_fastest_paths(graph, source):
|
|
|
print('Determining the fastest paths from transit locations to %s...' % str(source))
|
|
|
|
|
|
+ links = {}
|
|
|
+ for link_dict in graph.values():
|
|
|
+ links.update(link_dict)
|
|
|
+
|
|
|
indices = {}
|
|
|
for loc in graph:
|
|
|
if loc not in indices:
|
|
|
@@ -175,7 +179,7 @@ def determine_fastest_paths(graph, source):
|
|
|
|
|
|
commutes[current_source] = {
|
|
|
'duration': current_duration,
|
|
|
- 'path': current_path,
|
|
|
+ 'path': [links[link] for link in current_path],
|
|
|
}
|
|
|
|
|
|
for key, dest_data in graph[current_source].items():
|
|
|
@@ -207,17 +211,17 @@ def log_graph(graph, commutes):
|
|
|
|
|
|
print('')
|
|
|
|
|
|
-def analyze_locations(commutes, num_locations=5):
|
|
|
+def analyze_locations(commutes, num_locations):
|
|
|
print('Loading new directions data for %i locations...' % num_locations)
|
|
|
|
|
|
with db.connect() as conn:
|
|
|
- rows = conn.select('''select p.id, p.lat, p.lon from properties p
|
|
|
+ rows = conn.select('''select p.lat, p.lon from properties p
|
|
|
where lat is not null and lon is not null''')
|
|
|
|
|
|
random.shuffle(rows)
|
|
|
to_load = []
|
|
|
|
|
|
- for property_id, lat, lon in rows:
|
|
|
+ for lat, lon in rows:
|
|
|
closest = None
|
|
|
closest_dist = None
|
|
|
|
|
|
@@ -229,7 +233,7 @@ def analyze_locations(commutes, num_locations=5):
|
|
|
|
|
|
target_loc = (lat, lon)
|
|
|
|
|
|
- if (geo_util.ggcd(target_loc, closest) > 200
|
|
|
+ if (geo_util.ggcd(target_loc, closest) > 300
|
|
|
and any(geo_util.inside(target_loc, bbox) for bbox in config.MONITORED_BBOXES)):
|
|
|
to_load.append(target_loc)
|
|
|
|
|
|
@@ -239,25 +243,49 @@ def analyze_locations(commutes, num_locations=5):
|
|
|
# scores = {loc: geo_util.ggcd(target, loc) for loc in to_load}
|
|
|
# to_load.sort(key=lambda loc: scores[loc])
|
|
|
|
|
|
- return
|
|
|
-
|
|
|
for loc in to_load[:num_locations]:
|
|
|
print('\tLoading %s...' % str(loc))
|
|
|
directions_call(loc, config.WORKPLACE, TransitMethod.TRANSIT)
|
|
|
|
|
|
def add_commute_data(properties, commutes):
|
|
|
+
|
|
|
+ cyclable = {k: v for k, v in commutes.items()
|
|
|
+ if not v['path'] or (v['path'][-1].get('desc') or '').startswith('HEAVY_RAIL')
|
|
|
+ or (v['path'][-1].get('desc') or '').startswith('HEAVY_RAIL')}
|
|
|
+
|
|
|
+ transport_options = [
|
|
|
+ (cyclable, 5.5, TransitMethod.CYCLING),
|
|
|
+ (commutes, 1, TransitMethod.WALKING),
|
|
|
+ ]
|
|
|
+
|
|
|
for p in properties.values():
|
|
|
+ best_path = None
|
|
|
+ best_duration = None
|
|
|
+
|
|
|
+ for data, speed, mode in transport_options:
|
|
|
+ for loc, commute_data in data.items():
|
|
|
+ if best_duration is not None and best_duration <= commute_data['duration']:
|
|
|
+ continue
|
|
|
+
|
|
|
+ duration = int(round(geo_util.ggcd(p['loc'], loc) / speed))
|
|
|
+ total_duration = commute_data['duration'] + duration
|
|
|
+
|
|
|
+ if best_duration is None or total_duration < best_duration:
|
|
|
+ best_duration = total_duration
|
|
|
+ best_path = commute_data['path'] + [{
|
|
|
+ 'source': p['loc'],
|
|
|
+ 'dest': loc,
|
|
|
+ 'duration': duration,
|
|
|
+ 'mode': mode,
|
|
|
+ }]
|
|
|
+
|
|
|
p.update({
|
|
|
- 'commute_duration': 1800,
|
|
|
+ 'commute_duration': best_duration,
|
|
|
+ 'commute_path': best_path,
|
|
|
})
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
-
|
|
|
- # _record_api_response(data.x)
|
|
|
- # exit()
|
|
|
-
|
|
|
- for _ in range(1):
|
|
|
+ while True:
|
|
|
graph = load_graph()
|
|
|
commutes = determine_fastest_paths(graph, config.WORKPLACE)
|
|
|
-
|
|
|
- # analyze_locations(commutes)
|
|
|
+ analyze_locations(commutes, 20)
|