Commits

Rémy HUBSCHER  committed 60d4e44

dépile les arrêts pour former l'itinéraire

  • Participants
  • Parent commits 2b558f6

Comments (0)

Files changed (3)

File docs/beamer/tx52.tex

 
 \usepackage{natim-beamer}
 
-\title{Optymize, optimisation de parcours}
+\title{Optymize, optimisation de parcours dans Belfort}
 \author{Rémy HUBSCHER - Julien MOINE}
 \institute{Université de Technologie de Belfort-Montbéliard}
 \date{13 janvier 2011}%\date{\today}

File optymize/api/handlers.py

 
         return {'routes': list(routes)}
 
+def getDijkstra(graph, starting_point, ending_point):
+    """ Retourne le JSON résultat de l'algorithme de DIJKSTRA """
+    path_finder = PathFinder(graph, starting_point, ending_point)
+    final = path_finder.find()
+    path = final.path()
+    
+    query_routes = Route.objects.select_related()
+    routes = {}
+    for r in query_routes:
+        routes[r.id] = r
+
+    stops = []
+    json = []
+
+    last_was_bus = False
+    last_route = None
+    first = True
+
+    for point in path:
+       print unicode(point.geopoint), point.distance, point.type, 
+       if point.geopoint.station is not None:
+           print point.geopoint.station.id, 
+       if point.type == "bus":
+           print routes[point.route].line.number
+       else:
+           print
+
+    path.reverse()
+
+    for point in path:
+        # On saisie les informations sur le point dans result
+        result = {'latitude': point.geopoint.latitude, 'longitude': point.geopoint.longitude}
+
+        if hasattr(point.geopoint, 'station') and point.geopoint.station:
+            result['id'] = point.geopoint.station.id
+            result['name'] = smart_unicode(point.geopoint.station.name)
+
+        stops.insert(0, result)
+
+        if point.type == 'bus':
+            current_is_bus = True
+        else:
+            current_is_bus = False
+
+        if first:
+            last_was_bus = current_is_bus
+            last_route = getattr(point, 'route', None)
+            first = False
+        else:
+            # 3. Si le moyen de locomotion change :
+            if (current_is_bus != last_was_bus) or (current_is_bus and last_route != point.route):
+                if last_was_bus:
+                    # 3.1 C'était une ligne de bus, on ajoute {'line': line, 'stops': stops}
+                    line = routes[last_route].line
+                    json.insert(0, {'line': {'color': (len(COLORS) >= line.number and COLORS[line.number-1] or '000000'),
+                                             'number': line.number,
+                                             'name': smart_unicode(line.name)},
+                                    'stops': stops})
+
+                else:
+                   # 3.2 Si c'était un path, on ajoute {'path': stops} au json
+                   json.insert(0, {'path': stops})
+
+                stops = [result]
+
+            last_was_bus = current_is_bus
+            last_route = getattr(point, 'route', None)
+
+    # On ajoute le dernier trajet en testant le moyen de locomotion (bus ou path)
+    if last_was_bus:
+        # 3.1 Si c'était une ligne de bus, on ajoute {'line': line, 'stops': stops}
+        line = routes[last_route].line
+        json.insert(0, {'line': {'color': (len(COLORS) >= line.number and COLORS[line.number-1] or '000000'),
+                                 'number': line.number,
+                                 'name': smart_unicode(line.name)},
+                        'stops': stops})
+    else:
+       if len(stops) > 1:
+          json.insert(0, {'path': stops})
+         
+    return json
+
 class DirectionsHandler(BaseHandler):
    def read(self, request, from_id, to_id, emitter_format=None):
       graph = getBusGraph()
       if from_id not in graph.keys() or to_id not in graph.keys():
          return getErrorResponse(403, {'status': 1,'errMessage': "La station demandée est introuvable dans le graph.", 'station_ids': graph.keys()}, emitter_format)
 
-      path_finder = PathFinder(graph, graph[from_id], graph[to_id])
-      final = path_finder.find()
-
-      path = final.path()      
-
-      query_routes = Route.objects.select_related()
-      routes = {}
-      for r in query_routes:
-         routes[r.id] = r
-
-      # 1. Si c'est le premier, on l'ajoute à la liste des arrêts
-      stops = []
-      json = []
-
-      last_was_bus = False
-      last_route = None
-      first = True
-
-      for point in path:
-         result = {'latitude': point.geopoint.latitude, 'longitude': point.geopoint.longitude}
-         if point.type == 'bus':
-            current_is_bus = True
-         else:
-            current_is_bus = False
-
-         if hasattr(point.geopoint, 'station') and point.geopoint.station:
-            result['id'] = point.geopoint.station.id
-            result['name'] = smart_unicode(point.geopoint.station.name)
-
-         # On a un result avec les caractèristiques de l'arrêt
-
-         stops.append(result)
-
-         # 3. Si le moyen de locomotion change :
-         if current_is_bus != last_was_bus or (current_is_bus and last_route != point.route):
-            if current_is_bus:
-               # 3.1 Si c'était une ligne de bus, on ajoute {'line': line, 'stops': stops}
-               line = routes[point.route].line
-               json.append({'line': {'color': (len(COLORS) >= line.number and COLORS[line.number-1] or '000000'),
-                                     'number': line.number,
-                                     'name': smart_unicode(line.name)},
-                            'stops': stops})
-            else:
-               # 3.2 Si c'était un path, on ajoute {'path': stops} au json
-               json.append({'path': stops})
-
-            stops = [result]
-
-         last_was_bus = current_is_bus
-         last_route = getattr(point, 'route', None)
-
-      # On ajoute le dernier trajet en testant le moyen de locomotion (bus ou path)
-      if current_is_bus:
-         # 3.1 Si c'était une ligne de bus, on ajoute {'line': line, 'stops': stops}
-         line = routes[point.route].line
-         json.append({'line': {'color': (len(COLORS) >= line.number and COLORS[line.number-1] or '000000'),
-                               'number': line.number,
-                               'name': smart_unicode(line.name)},
-                      'stops': stops})
-      else:
-         # 3.2 Si c'était un path, on ajoute {'path': stops} au json
-         if len(stops) > 1:
-            json.append({'path': stops})
+      json = getDijkstra(graph, graph[from_id], graph[to_id])
 
       return {'result': json}
 
          graph[station_id].neighbours['end'] = [{'type': 'path', 'duration': footTimeDistanceEnding[0]}]
          graph['end'] = ending_point
 
-      path_finder = PathFinder(graph, starting_point, ending_point)
-      final = path_finder.find()
+      json = getDijkstra(graph, starting_point, ending_point)
 
-      path = final.path()
-
-      query_routes = Route.objects.select_related()
-      routes = {}
-      for r in query_routes:
-         routes[r.id] = r
-
-      # 1. Si c'est le premier, on l'ajoute à la liste des arrêts
-      stops = []
-      json = []
-
-      last_was_bus = False
-      last_route = None
-      first = True
-
-
-      for point in path:
-         result = {'latitude': point.geopoint.latitude, 'longitude': point.geopoint.longitude}
-
-         if point.type == 'bus':
-            current_is_bus = True
-         else:
-            current_is_bus = False
-
-         if hasattr(point.geopoint, 'station') and point.geopoint.station:
-            result['id'] = point.geopoint.station.id
-            result['name'] = smart_unicode(point.geopoint.station.name)
-
-         # On a un result avec les caractèristiques de l'arrêt
-
-         stops.append(result)
-
-         # 3. Si le moyen de locomotion change :
-         if current_is_bus != last_was_bus or (current_is_bus and last_route != point.route):
-            if current_is_bus:
-               # 3.1 Si c'était une ligne de bus, on ajoute {'line': line, 'stops': stops}
-               line = routes[point.route].line
-               json.append({'line': {'color': (len(COLORS) >= line.number and COLORS[line.number-1] or '000000'),
-                                     'number': line.number,
-                                     'name': smart_unicode(line.name)},
-                            'stops': stops})
-            else:
-               # 3.2 Si c'était un path, on ajoute {'path': stops} au json
-               json.append({'path': stops})
-
-            stops = [result]
-
-         last_was_bus = current_is_bus
-         last_route = getattr(point, 'route', None)
-
-      # On ajoute le dernier trajet en testant le moyen de locomotion (bus ou path)
-      if current_is_bus:
-         # 3.1 Si c'était une ligne de bus, on ajoute {'line': line, 'stops': stops}
-         line = routes[point.route].line
-         json.append({'line': {'color': (len(COLORS) >= line.number and COLORS[line.number-1] or '000000'),
-                               'number': line.number,
-                               'name': smart_unicode(line.name)},
-                      'stops': stops})
-      else:
-         # 3.2 Si c'était un path, on ajoute {'path': stops} au json
-         if len(stops) > 1:
-            json.append({'path': stops})
-         
       return {'result': json}

File optymize/dijkstra/utils.py

     def weight(self, geopoint):
         if geopoint.station is not None:
             neighbours = self.geopoint.neighbours[geopoint.station.id]
+
+            for n in neighbours:
+                route = n.get('route', None)
+                if self.route is not None and route is not None and route != self.route:
+                    n['duration'] += 5
+
             self.geopoint.neighbours[geopoint.station.id] = sorted(neighbours, key=lambda k: k['duration'])
 
             info = [d for d in self.geopoint.neighbours[geopoint.station.id] if d['duration'] == min([p['duration'] for p in self.geopoint.neighbours[geopoint.station.id]])][0]