예제 #1
0
def print_trip_story(device_id):
    bssid_map = {}
    has_unmapped_bssid = False
    with open(os.path.join(settings.BASE_DIR, 'algorithm', 'data', 'manual_bssid_map.txt'), 'r') as f:
        content = f.readlines()          
    for line in content:
        line = line.strip('\n')
        if line and line[0] != '#':
            bssid_map[line.split(' ', 1)[0]] = line.split(' ', 1)[1]
    reports3 = analysis.models.Report.objects.filter(device_id = device_id).order_by('timestamp')
    prev_loc = None
    prev_has_bssid = False
    for report in reports3:
        #has_bssid = bssid in [x.key for x in report.get_wifi_set_all()]
        has_bssid = 'S-ISRAEL-RAILWAYS' in [x.SSID for x in report.get_wifi_set_all()]
        if prev_has_bssid != has_bssid:
            if has_bssid:
                start_time = ot_utils.get_localtime(report.timestamp)
            else:
                print start_time.date(), start_time.strftime("%H:%M:%S"), ot_utils.get_localtime(report.timestamp).strftime("%H:%M:%S"), report.device_id, has_bssid
        stop_bssids = [x.key for x in report.get_wifi_set_all() if x.SSID == 'S-ISRAEL-RAILWAYS'] 
        if not all(x in bssid_map for x in stop_bssids):
            has_unmapped_bssid = True
        stop_bssids = [bssid_map[x] if x in bssid_map else x for x in stop_bssids]
        if stop_bssids:
            print ot_utils.get_localtime(report.timestamp).strftime("%H:%M:%S"), sorted(stop_bssids)
        #print ot_utils.get_localtime(report.timestamp), report.device_id, bssid, has_bssid, lat, lon, int(dist)
        #print report.timestamp, ot_utils.get_localtime(report.timestamp), report.device_id, bssid, has_bssid, lat, lon, int(dist)
        prev_loc = report.get_my_loc()
        prev_has_bssid = has_bssid 
    if has_unmapped_bssid:
        print 'HAS UNMAPPED BSSID!!!'
    else:
        print 'All bssids mapped'
예제 #2
0
def PrintBSSIDReportsOnMap(bssid):
    #display_utils.draw_map()
    wifi_reports = analysis.models.SingleWifiReport.objects.filter(key = bssid)
    reports = list(set(x.report for x in wifi_reports))
    
    #t = [((x.timestamp - x.get_my_loc().timestamp).total_seconds(), x.get_my_loc().accuracy) for x in reports]
    #t = sorted(t)
    #t = np.array(t)
    #a = [x.get_my_loc().accuracy for x in reports]
    ts = sorted([x.timestamp for x in reports])
    import common.ot_utils as ot_utils
    sorted_reports = sorted(zip(ts, reports))
    sorted_reports = [x[1] for x in sorted_reports]
    for x in sorted_reports:
        print x.id, x, ot_utils.get_localtime(x.timestamp), x.my_loc.lat, x.my_loc.lon
    
    ot_utils.get_localtime(ts[0])
    ot_utils.get_localtime(ts[-1])
    
    #plt.plot(t)
    #plt.plot(a)
    for i, report in enumerate(reports):
        print i
        if hasattr(report, 'my_loc'):
            plt.scatter(report.my_loc.lon, report.my_loc.lat, s=10)
            plt.show
    print 'done'
예제 #3
0
 def print_stoptimes(self):
     stop_times = self.get_stop_times()
     print 'trip ' + self.gtfs_trip_id
     for stop in stop_times:
         arrival_str = ot_utils.get_localtime(stop.exp_arrival).strftime('%H:%M:%S') if ot_utils.get_localtime(stop.exp_arrival) is not None else '--:--:--'
         departure_str = ot_utils.get_localtime(stop.exp_departure).strftime('%H:%M:%S') if ot_utils.get_localtime(stop.exp_departure) is not None else '--:--:--'
         #delta_str =  delta.strftime('%M:%S') if departure is not None else '--:--'
         print '%s %s %s' % (arrival_str, departure_str, stop.stop.stop_name)
예제 #4
0
 def get_timestamp_israel_time(self):
     #local_time_delta = datetime.timedelta(0,2*3600)
     #return self.timestamp + local_time_delta
     from common.ot_utils import get_localtime
     timestamp = get_localtime(self.timestamp)
     timestamp = timestamp.replace(microsecond=0)
     return timestamp
예제 #5
0
def GetRedisData(redis_key, day=None):
  if day:
    redis_key += ':' + _DayToDayStr(day)
  if redis_key in redis_cache:
    return redis_cache[redis_key]
  
  data_json = load_by_key(redis_key)
  data = json.loads(data_json,object_hook=json_hook_dt)
  
  import dateutil.parser
  if type(data) == dict:
    for trip_id in data.keys():
      if type(data[trip_id]) == dict and 'start_time' in data[trip_id].keys():
        data[trip_id]['start_time'] = ot_utils.get_localtime(dateutil.parser.parse(data[trip_id]['start_time']))
        data[trip_id]['end_time'] = ot_utils.get_localtime(dateutil.parser.parse(data[trip_id]['end_time']))
        for stop_id in data[trip_id]['stops']:
          stop_data = data[trip_id]['stops'][stop_id]
          #stop_data[1] = ot_utils.get_localtime(dateutil.parser.parse(stop_data[1]))
          #stop_data[2] = ot_utils.get_localtime(dateutil.parser.parse(stop_data[2]))
          data[trip_id]['stops'][stop_id] = stop_data
  
  redis_cache[redis_key] = data
  return data
예제 #6
0
 def load_from_gtfs(gtfs_stop_time, date):
     arrival = ot_utils.get_localtime(gtfs_stop_time.exp_arrival)
     departure = ot_utils.get_localtime(gtfs_stop_time.exp_departure)
     return DetectedStopTime(gtfs_stop_time.stop.gtfs_stop_id, arrival, departure)
예제 #7
0
 def load_from_gtfs(gtfs_stop_time, date):
     arrival = ot_utils.db_time_to_datetime(gtfs_stop_time.arrival_time, date)
     arrival = ot_utils.get_localtime(arrival)
     departure = ot_utils.db_time_to_datetime(gtfs_stop_time.departure_time, date)
     departure = ot_utils.get_localtime(departure)        
     return DetectedStopTime(gtfs_stop_time.stop.stop_id, arrival, departure)        
예제 #8
0
def get_matched_trips(tracker_id, detected_stop_times, day):
    if len(detected_stop_times) == 0:
        return None
    detected_stop_ids = [x.stop_id for x in detected_stop_times]   
    trip_datastore = TripDatastore(day)
    costops = trip_datastore.costop_matrix # beseder
    
    stop_ids = [x.stop_id for x in detected_stop_times]
    stop_ids_inds = [all_stops.id_list.index(x) for x in stop_ids]

    trips_with_visited_stops = trip_datastore.GetTripsByStops(stop_ids_inds) # beseder
    
    trip_in_right_direction = []
    arrival_delta_abs_means_seconds = []
    trips_filtered_by_stop_order = []
    trips_filtered_by_stop_order_detected_stop_inds = []
    for i, t in enumerate(trips_with_visited_stops):
        stops_dict = trip_datastore.trip_datastore[t]['stops']

        start_time = trip_datastore.trip_datastore[t]['start_time']
        end_time = trip_datastore.trip_datastore[t]['end_time']

        detected_stop_times_in_time_range = []
        for i, x in enumerate(detected_stop_times):
            arrival = x.arrival
            if start_time <= arrival and arrival <= end_time:
                detected_stop_times_in_time_range.append(x)
        filtered_detected_stop_times_stop_inds = [all_stops.id_list.index(x.stop_id) for x in detected_stop_times_in_time_range]
        impossible_stops_inds = trip_datastore.GetImpossibleCostops(filtered_detected_stop_times_stop_inds) # beseder
        impossible_stops_ids = [all_stops.id_list[x] for x in impossible_stops_inds]
        has_impossible_stops = bool([x for x in impossible_stops_ids if x in stops_dict])
        if has_impossible_stops:
            continue

        gtfs_sequence_of_detected_stops = []
        filtered_detected_stop_times = []
        filtered_detected_stop_times_inds = []
        for i, x in enumerate(detected_stop_times):
            # if checks for:
            # - stops that are detected and are in trip:
            # - stops that are in trip time range:
            arrival = x.arrival
            if ((x.stop_id in stops_dict and arrival <= end_time and 
                 ((stops_dict[x.stop_id][0] != 1 and start_time <= arrival) or (stops_dict[x.stop_id][0] == 1 and x.departure)))):  # first stop in trip
                gtfs_sequence_of_detected_stops.append(stops_dict[x.stop_id][0])
                filtered_detected_stop_times.append(x)
                filtered_detected_stop_times_inds.append(i)
        # filter by stop order and at least two detected stops:    
        #4900 Tel Aviv HaHagana
        #3600 Tel Aviv - University
        #3700 Tel Aviv Center - Savidor
        #4600 Tel Aviv HaShalom        
        has_tel_aviv_stop = bool([x for x in filtered_detected_stop_times if x.stop_id in [4900, 3600, 3700, 4600]])
        non_tel_aviv_stops = len([x for x in filtered_detected_stop_times if x.stop_id not in [4900, 3600, 3700, 4600]])
        stop_count_with_tel_aviv_as_one_stop = non_tel_aviv_stops + int(has_tel_aviv_stop)
        if stop_count_with_tel_aviv_as_one_stop >= 2:
            if is_increasing(gtfs_sequence_of_detected_stops):
                trips_filtered_by_stop_order.append(t)
                trips_filtered_by_stop_order_detected_stop_inds.append(filtered_detected_stop_times_inds)
                # calc deviation from gtfs arrival time:
                arrival_delta_abs_sum = 0
                for detected_stop_time in filtered_detected_stop_times:
                    stop_and_arrival_gtfs = stops_dict.get(detected_stop_time.stop_id)
                    if stop_and_arrival_gtfs:
                        exp_arrival = ot_utils.get_localtime(dateutil.parser.parse(stop_and_arrival_gtfs[1]))
                        if stop_and_arrival_gtfs[0] == 1:  # first stop in trip - compare departures
                            arrival_delta_seconds = exp_arrival - detected_stop_time.departure
                        else:
                            arrival_delta_seconds = exp_arrival - detected_stop_time.arrival
                        arrival_delta_abs_sum += abs(arrival_delta_seconds).total_seconds()
                arrival_delta_abs_mean = arrival_delta_abs_sum/len(filtered_detected_stop_times_inds)
                arrival_delta_abs_means_seconds.append(arrival_delta_abs_mean)                
    
    trip_delays_ids = sorted(zip(arrival_delta_abs_means_seconds, trips_filtered_by_stop_order, trips_filtered_by_stop_order_detected_stop_inds))
    
    trip_delays_ids_temp = trip_delays_ids
    trip_delays_ids_list_of_lists = []
    while len(trip_delays_ids_temp) > 0:
        trip_delay_id_root = trip_delays_ids_temp[0]
        del trip_delays_ids_temp[0]
        if trip_delay_id_root[0] > 1800:  # half an hour
            continue
        trip_delays_ids_list = [trip_delay_id_root]
        intersecting_trip_delays_ids = [x for x in trip_delays_ids_temp if trip_datastore.DoTripsIntersect(trip_delay_id_root[1], x[1])]
        trip_delays_ids_list += intersecting_trip_delays_ids
        trip_delays_ids_list_of_lists.append(trip_delays_ids_list)
        trip_delays_ids_temp = [x for x in trip_delays_ids_temp if x not in intersecting_trip_delays_ids]
    print trip_delays_ids_list_of_lists
    return trip_delays_ids_list_of_lists