From 676a561578792aa355b938991cfad794e6b9ceba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Kl=C3=BCnder?= Date: Sun, 17 Dec 2017 00:41:18 +0100 Subject: [PATCH] add routing mode: fastest (respect speeds) --- src/c3nav/routing/models.py | 9 +++++++++ src/c3nav/routing/router.py | 31 ++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/c3nav/routing/models.py b/src/c3nav/routing/models.py index ba99bf7e..01c5c0a8 100644 --- a/src/c3nav/routing/models.py +++ b/src/c3nav/routing/models.py @@ -28,6 +28,11 @@ class RouteOptions(models.Model): @classmethod def build_fields(cls): fields = OrderedDict() + fields['mode'] = forms.ChoiceField( + label=_('Routing mode'), + choices=(('fastest', _('fastest')), ('shortest', _('shortest'))), + initial='fastest' + ) fields['walk_speed'] = forms.ChoiceField( label=_('Walk speed'), choices=(('slow', _('slow')), ('default', _('default')), ('fast', _('fast'))), @@ -137,6 +142,10 @@ class RouteOptions(models.Model): def __setitem__(self, key, value): self.update({key: value}) + @property + def walk_factor(self): + return {'slow': 0.8, 'default': 1, 'fast': 1.2}[self['walk_speed']] + def get(self, key, default): try: return self[key] diff --git a/src/c3nav/routing/router.py b/src/c3nav/routing/router.py index 5f65bb66..36e27334 100644 --- a/src/c3nav/routing/router.py +++ b/src/c3nav/routing/router.py @@ -313,10 +313,30 @@ class Router: return result graph = self.graph.copy() - graph[tuple(restrictions.spaces), :] = np.inf - graph[:, tuple(restrictions.spaces)] = np.inf - graph[restrictions.edges.transpose().tolist()] = np.inf + # speeds of waytypes, if relevant + if options['mode'] == 'fastest': + self.waytypes[0].speed = 1 + self.waytypes[0].speed_up = 1 + self.waytypes[0].extra_seconds = 0 + self.waytypes[0].walk = True + + for waytype in self.waytypes: + speed = float(waytype.speed) + speed_up = float(waytype.speed_up) + if waytype.walk: + speed *= options.walk_factor + speed_up *= options.walk_factor + + for indices, dir_speed in ((waytype.nonupwards_indices, speed), (waytype.upwards_indices, speed_up)): + indices = indices.transpose().tolist() + values = graph[indices] + values /= dir_speed + if waytype.extra_seconds: + values += int(waytype.extra_seconds) + graph[indices] = values + + # avoid waytypes as specified in settings for waytype in self.waytypes[1:]: value = options.get('waytype_%s' % waytype.pk, 'allow') if value in ('avoid', 'avoid_up'): @@ -324,6 +344,11 @@ class Router: if value in ('avoid', 'avoid_down'): graph[waytype.nonupwards_indices.transpose().tolist()] *= 100000 + # exclude spaces and edges + graph[tuple(restrictions.spaces), :] = np.inf + graph[:, tuple(restrictions.spaces)] = np.inf + graph[restrictions.edges.transpose().tolist()] = np.inf + result = shortest_path(graph, directed=True, return_predecessors=True) cache.set(cache_key, result, 600) return result