calculate route duration

This commit is contained in:
Laura Klünder 2017-12-17 01:02:41 +01:00
parent 676a561578
commit 54c25e9f94
2 changed files with 21 additions and 3 deletions

View file

@ -18,12 +18,13 @@ def describe_location(location, locations):
class Route:
def __init__(self, router, origin, destination, distance, path_nodes, origin_addition, destination_addition):
def __init__(self, router, origin, destination, path_nodes, options,
origin_addition, destination_addition):
self.router = router
self.origin = origin
self.destination = destination
self.distance = distance
self.path_nodes = path_nodes
self.options = options
self.origin_addition = origin_addition
self.destination_addition = destination_addition
@ -39,6 +40,8 @@ class Route:
last_node = None
last_item = None
distance = 0
duration = 0
walk_factor = self.options.walk_factor
for i, (node, edge) in enumerate(nodes):
if edge is None:
edge = self.router.edges[last_node, node] if last_node else None
@ -46,6 +49,7 @@ class Route:
item = RouteItem(self, node_obj, edge, last_item)
if edge:
distance += edge.distance
duration += item.router_waytype.get_duration(edge, walk_factor)
items.append(item)
last_item = item
last_node = node
@ -77,6 +81,7 @@ class Route:
('origin', describe_location(self.origin, locations)),
('destination', describe_location(self.destination, locations)),
('distance', round(distance, 2)),
('duration', round(duration)),
('items', tuple(item.serialize(locations=locations) for item in items)),
))
@ -94,6 +99,11 @@ class RouteItem:
if self.edge and self.edge.waytype:
return self.route.router.waytypes[self.edge.waytype]
@cached_property
def router_waytype(self):
if self.edge:
return self.route.router.waytypes[self.edge.waytype]
@cached_property
def space(self):
return self.route.router.spaces[self.node.space]

View file

@ -396,7 +396,7 @@ class Router:
origin_addition = origin.nodes_addition.get(origin_node)
destination_addition = destination.nodes_addition.get(destination_node)
return Route(self, origin, destination, distances[origin_node, destination_node], path_nodes,
return Route(self, origin, destination, path_nodes, options,
origin_addition, destination_addition)
@ -543,6 +543,14 @@ class RouterWayType:
raise AttributeError
return getattr(self.src, name)
def get_duration(self, edge, walk_factor):
if edge.rise > 0:
duration = edge.distance / (float(self.speed_up) * walk_factor)
else:
duration = edge.distance / (float(self.speed) * walk_factor)
duration += self.extra_seconds
return duration
class RouterLocation:
def __init__(self, locations=()):