calculate route duration
This commit is contained in:
parent
676a561578
commit
54c25e9f94
2 changed files with 21 additions and 3 deletions
|
@ -18,12 +18,13 @@ def describe_location(location, locations):
|
||||||
|
|
||||||
|
|
||||||
class Route:
|
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.router = router
|
||||||
self.origin = origin
|
self.origin = origin
|
||||||
self.destination = destination
|
self.destination = destination
|
||||||
self.distance = distance
|
|
||||||
self.path_nodes = path_nodes
|
self.path_nodes = path_nodes
|
||||||
|
self.options = options
|
||||||
self.origin_addition = origin_addition
|
self.origin_addition = origin_addition
|
||||||
self.destination_addition = destination_addition
|
self.destination_addition = destination_addition
|
||||||
|
|
||||||
|
@ -39,6 +40,8 @@ class Route:
|
||||||
last_node = None
|
last_node = None
|
||||||
last_item = None
|
last_item = None
|
||||||
distance = 0
|
distance = 0
|
||||||
|
duration = 0
|
||||||
|
walk_factor = self.options.walk_factor
|
||||||
for i, (node, edge) in enumerate(nodes):
|
for i, (node, edge) in enumerate(nodes):
|
||||||
if edge is None:
|
if edge is None:
|
||||||
edge = self.router.edges[last_node, node] if last_node else 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)
|
item = RouteItem(self, node_obj, edge, last_item)
|
||||||
if edge:
|
if edge:
|
||||||
distance += edge.distance
|
distance += edge.distance
|
||||||
|
duration += item.router_waytype.get_duration(edge, walk_factor)
|
||||||
items.append(item)
|
items.append(item)
|
||||||
last_item = item
|
last_item = item
|
||||||
last_node = node
|
last_node = node
|
||||||
|
@ -77,6 +81,7 @@ class Route:
|
||||||
('origin', describe_location(self.origin, locations)),
|
('origin', describe_location(self.origin, locations)),
|
||||||
('destination', describe_location(self.destination, locations)),
|
('destination', describe_location(self.destination, locations)),
|
||||||
('distance', round(distance, 2)),
|
('distance', round(distance, 2)),
|
||||||
|
('duration', round(duration)),
|
||||||
('items', tuple(item.serialize(locations=locations) for item in items)),
|
('items', tuple(item.serialize(locations=locations) for item in items)),
|
||||||
))
|
))
|
||||||
|
|
||||||
|
@ -94,6 +99,11 @@ class RouteItem:
|
||||||
if self.edge and self.edge.waytype:
|
if self.edge and self.edge.waytype:
|
||||||
return self.route.router.waytypes[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
|
@cached_property
|
||||||
def space(self):
|
def space(self):
|
||||||
return self.route.router.spaces[self.node.space]
|
return self.route.router.spaces[self.node.space]
|
||||||
|
|
|
@ -396,7 +396,7 @@ class Router:
|
||||||
origin_addition = origin.nodes_addition.get(origin_node)
|
origin_addition = origin.nodes_addition.get(origin_node)
|
||||||
destination_addition = destination.nodes_addition.get(destination_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)
|
origin_addition, destination_addition)
|
||||||
|
|
||||||
|
|
||||||
|
@ -543,6 +543,14 @@ class RouterWayType:
|
||||||
raise AttributeError
|
raise AttributeError
|
||||||
return getattr(self.src, name)
|
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:
|
class RouterLocation:
|
||||||
def __init__(self, locations=()):
|
def __init__(self, locations=()):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue