add routing mode: fastest (respect speeds)
This commit is contained in:
parent
df6f5b5aff
commit
676a561578
2 changed files with 37 additions and 3 deletions
|
@ -28,6 +28,11 @@ class RouteOptions(models.Model):
|
||||||
@classmethod
|
@classmethod
|
||||||
def build_fields(cls):
|
def build_fields(cls):
|
||||||
fields = OrderedDict()
|
fields = OrderedDict()
|
||||||
|
fields['mode'] = forms.ChoiceField(
|
||||||
|
label=_('Routing mode'),
|
||||||
|
choices=(('fastest', _('fastest')), ('shortest', _('shortest'))),
|
||||||
|
initial='fastest'
|
||||||
|
)
|
||||||
fields['walk_speed'] = forms.ChoiceField(
|
fields['walk_speed'] = forms.ChoiceField(
|
||||||
label=_('Walk speed'),
|
label=_('Walk speed'),
|
||||||
choices=(('slow', _('slow')), ('default', _('default')), ('fast', _('fast'))),
|
choices=(('slow', _('slow')), ('default', _('default')), ('fast', _('fast'))),
|
||||||
|
@ -137,6 +142,10 @@ class RouteOptions(models.Model):
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
self.update({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):
|
def get(self, key, default):
|
||||||
try:
|
try:
|
||||||
return self[key]
|
return self[key]
|
||||||
|
|
|
@ -313,10 +313,30 @@ class Router:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
graph = self.graph.copy()
|
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:]:
|
for waytype in self.waytypes[1:]:
|
||||||
value = options.get('waytype_%s' % waytype.pk, 'allow')
|
value = options.get('waytype_%s' % waytype.pk, 'allow')
|
||||||
if value in ('avoid', 'avoid_up'):
|
if value in ('avoid', 'avoid_up'):
|
||||||
|
@ -324,6 +344,11 @@ class Router:
|
||||||
if value in ('avoid', 'avoid_down'):
|
if value in ('avoid', 'avoid_down'):
|
||||||
graph[waytype.nonupwards_indices.transpose().tolist()] *= 100000
|
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)
|
result = shortest_path(graph, directed=True, return_predecessors=True)
|
||||||
cache.set(cache_key, result, 600)
|
cache.set(cache_key, result, 600)
|
||||||
return result
|
return result
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue