Esempio n. 1
0
  def find_path(self, time, src_lat, src_lng, dest_lat, dest_lng, cb=None):
    # translate the time to an offset from the beginning of the day
    # and determine service period
    now = datetime.datetime.fromtimestamp(time)
    today_secs = (now.hour * 60 * 60) + (now.minute * 60) + (now.second)
    service_period = 'weekday'
    if now.weekday() == 5:
      service_period = 'saturday'
    elif now.weekday() == 6:
      service_period = 'sunday'
    
    # Find path
    visited_ids = {}
        
    uncompleted_paths = [ ]
    completed_paths = [ ]
    best_path = None
    start_node = self.get_nearest_osmstop(src_lat, src_lng)
    end_node = self.get_nearest_osmstop(dest_lat, dest_lng)
    print "Start: %s End: %s" % (start_node.id, end_node.id)

    # stupid case: start is equal to end
    if start_node == end_node:
      return None

    dist_from_start = latlng_ext.distance(src_lat, src_lng, start_node.lat, start_node.lng)
    today_secs += dist_from_start / 1.1

    heappush(uncompleted_paths, TripPath(int(today_secs), self.fastest_speed, 
                                         end_node, start_node))
    
    # then keep on extending paths until we've exhausted all possibilities
    # or we can't do any better
    num_paths_considered = 0
    while len(uncompleted_paths) > 0:
      trip_path = heappop(uncompleted_paths)
      #print "Extending path with weight: %s" % (trip_path.heuristic_weight)
      new_trip_paths = self.extend_path(trip_path, service_period, visited_ids, cb)
      num_paths_considered += len(new_trip_paths)
      for p in new_trip_paths:
        if p.last_stop.id == end_node.id:
          heappush(completed_paths, p)
        else:
          heappush(uncompleted_paths, p)

      # if we've still got open paths, but their weight exceeds that
      # of the weight of a completed path, break
      if len(uncompleted_paths) > 0 and len(completed_paths) > 0 and \
            uncompleted_paths[0].heuristic_weight > completed_paths[0].heuristic_weight:
        print "Breaking with %s uncompleted paths (paths considered: %s)." % (len(uncompleted_paths), num_paths_considered)
        return completed_paths[0]

      #if len(completed_paths) > 0 and len(uncompleted_paths) > 0:
      #  print "Weight of best completed path: %s, uncompleted: %s" % \
      #      (completed_paths[0].heuristic_weight, uncompleted_paths[0].heuristic_weight)

    if len(completed_paths) > 0:
      return completed_paths[0]
    else:
      return None      
Esempio n. 2
0
 def link_osm_gtfs(self):
   mylen = len(self.tripstops.values())
   for s1 in self.tripstops.values():
     if s1.type == "gtfs":
       nearest_osm = None
       min_dist = 0
       for s2 in self.tripstops.values():
         if s2.type == "osm":
           dist = latlng_ext.distance(s1.lat, s1.lng, s2.lat, s2.lng)
           if not nearest_osm or dist < min_dist:
             nearest_osm = s2
             min_dist = dist
       time = latlng_ext.distance(nearest_osm.lat, nearest_osm.lng, s1.lat, s1.lng) / 1.1
       print "Adding triplink %s->%s" %(nearest_osm.id, s1.id)
       self.add_walkhop(nearest_osm.id, s1.id)
       self.add_walkhop(s1.id, nearest_osm.id)
Esempio n. 3
0
 def add_triphop(self, start_time, end_time, src_id, dest_id, route_id, 
                 service_id):
   s1 = self.tripstops[src_id]
   s2 = self.tripstops[dest_id]
   dist = latlng_ext.distance(s1.lat, s1.lng, s2.lat, s2.lng)
   total_time = end_time - start_time
   speed = float(dist) / float(total_time)
   if total_time > 30 and dist > 0 and speed > self.fastest_speed:
     self.fastest_speed = speed
   self.tripstops[src_id].add_triphop(start_time, end_time, dest_id, 
                                      route_id, service_id)
Esempio n. 4
0
 def add_walkhop(self, src_id, dest_id):
   w1 = self.tripstops[src_id]
   w2 = self.tripstops[dest_id]
   time = latlng_ext.distance(w1.lat, w1.lng, w2.lat, w2.lng) / 1.1
   self.tripstops[src_id].add_walkhop(dest_id, time)