コード例 #1
0
def start_time(a, r, dirTag, block, tftup, time_to_start, seq):
    """
    get the start time of each vehicle trip using block from next bus
    :param a: agency
    :param r: route
    :param dirTag: direction tag
    :param block: block number
    :param tftup: trip information
    :param time_to_start unix time stamp
    :return start time of the vehicle trip
    """
    idx = datetime.datetime.strptime(str(u.get_service_day()),
                                     '%Y%m%d').weekday()
    SC = serviceClass.get(a)[idx]
    start_time_list_per_block = schedule.get(a).get(r).get(SC).get(dirTag).get(
        block)
    #print start_time_list_per_block

    if start_time_list_per_block:  #in case start_time_list_per_block is none
        start_time = binary_search(start_time_list_per_block, time_to_start,
                                   seq)
        if a == 'dc-streetcar' and dirTag == 'west':
            start_time = start_time_special_streetcar(start_time)
        else:
            start_time = get_start_time_block(start_time, tftup)
        if int(start_time.split(':')[0]) < 5:
            start_time = str(int(start_time.split(':')[0]) +
                             24) + ':' + start_time.split(
                                 ':')[1] + ':' + start_time.split(':')[2]
        return start_time, start_time_list_per_block
    else:
        return 0, start_time_list_per_block
コード例 #2
0
def get_day_trips(agency):
    """
    Returns a dict where key is trip id active today and value is a tuple
    concatenation of tuple dict value generated by get_freq_dict and get_trips_dict.
    Assumes route is active in both directions if scheduled as such.
    
    @attention: Call before and pass it as an argument to get_trip_id to save time.
    
    :param agency: a str identifying the transit agency. Assumes a agency.zip GTFS archive.
    :param day: an int representing date, in the format yyyymmdd.
    :returns a dict of tuples representing trips active today.
    """
    day = get_service_day()

    d = {}
    activeservice = get_services(agency, day, exp=True)
    freqs = get_freq_dict(agency)
    trips = get_trips_dict(agency)
    # freqs[row['trip_id']] = (row['start_time'],row['end_time'],row['headway_secs'])
    # trips[row['trip_id']] = (row['route_id'],row['service_id'],row['direction_id'],row['trip_headsign'])

    for trip_id, ttup in trips.iteritems():
        if ttup.service_id in activeservice:
            tup = TripFreq(*(ttup + freqs[trip_id]))
            d[trip_id] = tup

    return d
コード例 #3
0
def get_datetime_blocktime(time_mid):
    """
    time_list[mid]
    return unix time
    """
    date = u.get_service_day()
    if int(time_mid.split(":")[0]) < 5:
        date = date + 1
    return datetime.datetime.strptime(str(date) + time_mid, "%Y%m%d%H:%M:%S")
コード例 #4
0
def get_in_feed(fm,trip_id,tftup,vehicle,agency,r,p,idx,stop_id,direction):
    myentity = fm.entity.add()
    myentity.id = vehicle.vehicle_id+'_'+vehicle.route_id+'_'+trip_id
    
    myentity.trip_update.timestamp = int(time.time())
    myentity.trip_update.trip.trip_id=trip_id
    myentity.trip_update.trip.route_id=vehicle.route_id
    myentity.trip_update.trip.direction_id=direction
    myentity.trip_update.trip.start_time=start_time(agency,r,p.get('dirTag'),p.get('block'),tftup)
    myentity.trip_update.trip.start_date=str(get_service_day())
    #myentity.trip_update.trip.schedule_relation
    myentity.trip_update.vehicle.id=myentity.trip_update.vehicle.label=vehicle.vehicle_id
    #myentity.trip_update.stop_time_update
    stu = myentity.trip_update.stop_time_update.add()
    stu.stop_sequence=idx
    stu.stop_id=str(stop_id)
    stu.arrival.time=int(int(p.get('epochTime'))/1000)
    #stu.schedule_relationship=
    print(myentity.id,stu.stop_sequence,myentity.trip_update.trip.start_time)
    st=myentity.id+' '+str(stu.stop_sequence)+' '+myentity.trip_update.trip.start_time
    logger.info(st)
コード例 #5
0
def recording(today_trip,previous):
    #returnlist={}
    trip_start_time={}
    vehicle_list = get_vehicle()
    count=0
    filename = 'dc-circulator-'+'tripupdates'+ext
    fm = g.FeedMessage()
    fm.header.gtfs_realtime_version = '2.0'
    fm.header.incrementality = g._FEEDHEADER_INCREMENTALITY.values_by_name['FULL_DATASET'].number
    

    for ve in vehicle_list:
        schedule_relation = 0
        if ve.tripID==0:
            #skip the trip ID = 0 records
            continue
        
        route = routes_dict[ve.route_id]
        
        if get_gtfsid(ve.nextStopID):
            stop_ID = get_gtfsid(ve.nextStopID)
        else:
            #skip the next stop ID not in ddot list record
            continue

        eta = get_eta(ve.route_id,ve.nextStopID)
        if eta: eta2= time.strftime('%H:%M:%S', time.localtime(int(eta)))
        else:
            #skip no eta records
            continue
        
        if ve_to_garage_distance(ve.lat,ve.lon):
            #skip vehicles that are in/near garage
            continue
        
        start_time, start_time2 = get_trip_info(ve.tripID,ve.nextStopID) # start_time is the raw start time align wit optibus system, start_time2 is the one round to 10 mins
        if start_time2=='0':
            start_time = start_time2 = previous[ve.vehicle_id][-1]
        start_date = str(get_service_day())
        #get rid of the toooo old eta time # wait for bishop peak responds
        t = datetime.datetime.strptime(start_date+start_time, '%Y%m%d%H:%M:%S')
        if time.mktime(t.timetuple())>eta:
            continue
        
        #if it's terminal stops and not sure about the trip id, reocrd the trip id serve the route
        trip_list=[]
        for trip_id,tup in today_trip.iteritems():
            if tup.route_id == route:
                trip_list.append(trip_id)

        if int(stop_ID) in seq_dict[trip_list[0]] and int(stop_ID) in seq_dict[trip_list[1]]:
            trip_id_final=str(trip_list[0])+" "+str(trip_list[1])
            seq = str(seq_dict[trip_list[0]].index(int(stop_ID))) + " " + str(seq_dict[trip_list[1]].index(int(stop_ID)))
            
            #write to protobuf as start of next trip but do no return
            seq_to_proto='0'
            for i in trip_list:
                if seq_dict[i].index(int(stop_ID))==0:
                    trip_id_to_proto = str(i)
                    direction_proto = today_trip[i].direction_id
                    
            if trip_id_to_proto not in trip_start_time.keys():
                trip_start_time[trip_id_to_proto] = [start_time2]
            else:
                if start_time2 in trip_start_time[trip_id_to_proto]:#which is duplicate
                    schedule_relation=1
                else:
                    trip_start_time[trip_id_to_proto].append(start_time2)
                    
            tuplelist[ve.vehicle_id] = VehicleInfo(ve.vehicle_id, route, direction_proto, \
                                        trip_id_to_proto, ve.tripID, start_time,start_time2,  start_date,\
                                        ve.lat, ve.lon, schedule_relation,\
                                        stop_ID,  seq_to_proto, eta, ve.timestamp)
            
        elif int(stop_ID) in seq_dict[trip_list[0]]:

            trip_id_final=str(trip_list[0])
            direction = today_trip[int(trip_id_final)].direction_id
            seq = get_seq(trip_id_final, stop_ID)
            
            if ve.vehicle_id in previous.keys():
                if previous[ve.vehicle_id].trip_id == trip_id_final: 
                    start_time2 = previous[ve.vehicle_id].start_time2 
            
            if trip_id_final not in trip_start_time.keys():
                trip_start_time[trip_id_final] = [start_time2]
            else:
                if start_time2 in trip_start_time[trip_id_final]:#which is duplicate
                    schedule_relation=1
                else:
                    trip_start_time[trip_id_final].append(start_time2)
            
          tuplelist[ve.vehicle_id] = VehicleInfo(ve.vehicle_id, route, direction, \
                                        trip_id_final, ve.tripID, start_time, start_time2,  start_date,\
                                        ve.lat, ve.lon, schedule_relation, \
                                        stop_ID,  seq, eta, ve.timestamp)  
        
        elif int(stop_ID) in seq_dict[trip_list[1]]:

            trip_id_final=str(trip_list[1])
            direction = today_trip[int(trip_id_final)].direction_id
            seq = get_seq(trip_id_final, stop_ID)
            
            if ve.vehicle_id in previous.keys():
                if previous[ve.vehicle_id].trip_id == trip_id_final: 
                    start_time2 = previous[ve.vehicle_id].start_time2
       
            if trip_id_final not in trip_start_time.keys():
                trip_start_time[trip_id_final] = [start_time2]
            else:
                if start_time2 in trip_start_time[trip_id_final]:#which is duplicate
                    schedule_relation=1
                else:
                    trip_start_time[trip_id_final].append(start_time2)
                    

            tuplelist[ve.vehicle_id] = VehicleInfo(ve.vehicle_id, route, direction, \
                                        trip_id_final, ve.tripID, start_time,start_time2,  start_date,\
                                        ve.lat, ve.lon, schedule_relation,\
                                        stop_ID,  seq, eta, ve.timestamp) 

        #keep the start_time consistant through out the whole trip instead of updating based on input
        if ve.vehicle_id in previous.keys():
            if previous[ve.vehicle_id].trip_id!= trip_id_final and len(trip_id_final) >2:#9 to 9 10
                if previous[ve.vehicle_id].seq == 0:
                    
                    if trip_id_final not in trip_start_time.keys():
                        trip_start_time[trip_id_final] = [start_time2]
                    else:
                        if start_time2 in trip_start_time[trip_id_final]:#which is duplicate
                            schedule_relation=1
                        else:
                            trip_start_time[trip_id_final].append(start_time2)

                    tuplelist[ve.vehicle_id] = previous[ve.vehicle_id]._replace(bishop_trip = ve.tripID, start_time=start_time, start_time2=start_time2,lat=ve.lat,lon=ve.lon, eta=eta, timestamp=ve.timestamp, schedule_relation=schedule_relation)#relation??
                else:
                    if ve.tripID == previous[ve.vehicle_id].bishop_trip:
                        seq = get_seq(previous[ve.vehicle_id].trip_id, stop_ID)
                        tuplelist[ve.vehicle_id] = previous[ve.vehicle_id]._replace(lat=ve.lat,lon=ve.lon,seq=seq,eta=eta,timestamp=ve.timestamp)
                    else:
                        seq='0'
                        trip_list.remove(int(previous[ve.vehicle_id].trip_id)) # renew  the trip id # trip_list.remove(int(previous[ve.vehicle_id].trip_id_final))
                        trip_id_final = str(trip_list[0])
                        direction = today_trip[int(trip_id_final)].direction_id

                        if trip_id_final not in trip_start_time.keys():
                            trip_start_time[trip_id_final] = [start_time2]
                        else:
                            if start_time2 in trip_start_time[trip_id_final]:#which is duplicate
                                schedule_relation=1
                            else:
                                trip_start_time[trip_id_final].append(start_time2)

                        tuplelist[ve.vehicle_id] = VehicleInfo(ve.vehicle_id, route, direction, \
                                        trip_id_final, ve.tripID, start_time,start_time2,  start_date,\
                                        ve.lat, ve.lon, schedule_relation,\
                                        stop_ID,  seq, eta, ve.timestamp) 

        if ve.vehicle_id in tuplelist.keys():
            write_protobuf(tuplelist[ve.vehicle_id],fm)
            count+=1
コード例 #6
0
def write_to_pb(ve_tr):
    """
    write trip update information into protobuf entity
    1.find the start time of trip
    :param ve_tr: dictionary of vehicle_trip, key is vehicle_trip,value is the dictionary, 
                    keys inside are eta and other,  value of eta is a list of namedtuple of eta
                    value of 'other' includes vehicle_id, route_id, direction_id, trip_id,
                                        timestamp, agency, route_NB, dirtag, block, tftup
    :param vehicle_dict: dictionary of vehicle info
    :return: feedmessage
    """
    fm = g.FeedMessage()

    ## Feed Header
    fm.header.gtfs_realtime_version = '2.0'
    fm.header.incrementality = g._FEEDHEADER_INCREMENTALITY.values_by_name[
        'FULL_DATASET'].number  #enum: full dataset
    fm.header.timestamp = int(time.time())
    check_multiple = {}
    count = 0
    for key, value in ve_tr.iteritems():

        sorted_eta = sorted(value['eta'], key=attrgetter('seq'))

        #get start time of current trip
        if sorted_eta[0].seq == 0:
            time_to_start = int(int(sorted_eta[0].eta) / 1000)
        else:
            time_to_start = int(time.time())
        start_time_value, block = start_time(value['other'].agency, value['other'].route_NB, \
                                value['other'].dirtag, value['other'].block, \
                                value['other'].tftup, time_to_start, sorted_eta[0].seq)
        if sorted_eta[0].seq != 0:
            logger.info(block)
        if start_time_value == 0:  #start time cannot be identified, pass this record
            continue

        #entity
        myentity = fm.entity.add()
        myentity.id = key
        myentity.trip_update.timestamp = value['other'].timestamp
        myentity.trip_update.trip.trip_id = value['other'].trip_id
        myentity.trip_update.trip.route_id = value['other'].route_id
        myentity.trip_update.trip.direction_id = value['other'].direction_id
        myentity.trip_update.trip.start_time = start_time_value
        myentity.trip_update.trip.start_date = str(get_service_day())
        myentity.trip_update.vehicle.id = myentity.trip_update.vehicle.label = value[
            'other'].vehicle_id
        count += 1

        st2 = myentity.id.replace(
            "_", " ") + ' ' + myentity.trip_update.trip.start_time

        #write eta for each stop
        last = int(sorted_eta[0].eta) / 1000
        for i in range(len(sorted_eta)):
            #filter eta eariler than previous stop, STOP_TIME_UPDATE_PREMATURE_ARRIVAL
            if i != 0 and int(sorted_eta[i].eta) / 1000 < last:
                continue
            #write eta for each stop
            i_eta = sorted_eta[i]
            stu = myentity.trip_update.stop_time_update.add()
            stu.stop_sequence = i_eta.seq
            stu.stop_id = str(i_eta.nextstop)
            last = stu.arrival.time = int(int(i_eta.eta) / 1000)
            eta2 = time.strftime('%H:%M:%S', time.localtime(stu.arrival.time))
            if i == 0:
                st2 = st2 + ' ' + str(stu.stop_sequence) + ' ' + eta2

        logger.info(st2)

        #avoid the multiple entity per trip warning
        if value['other'].trip_id not in check_multiple.keys():
            check_multiple[value['other'].trip_id] = [
                myentity.trip_update.trip.start_time
            ]
        elif myentity.trip_update.trip.start_time in check_multiple[
                value['other'].trip_id]:
            myentity.trip_update.trip.schedule_relationship = 1
        else:
            check_multiple[value['other'].trip_id].append(
                myentity.trip_update.trip.start_time)

        # vehicle position feed
        '''
        if myentity.trip_update.vehicle.id not in vehicle_dict.keys():
            continue
        myentity.vehicle.vehicle.id = myentity.vehicle.vehicle.label = myentity.trip_update.vehicle.id
        pos = vehicle_dict[myentity.vehicle.vehicle.id]
        myentity.vehicle.position.latitude = pos.lat
        myentity.vehicle.position.longitude = pos.lon
        myentity.vehicle.position.speed = pos.speed
        myentity.vehicle.timestamp = pos.timestamp
        myentity.vehicle.trip.trip_id = myentity.trip_update.trip.trip_id
        myentity.vehicle.trip.route_id = myentity.trip_update.trip.route_id
        myentity.vehicle.trip.direction_id = myentity.trip_update.trip.direction_id
        myentity.vehicle.trip.start_time = myentity.trip_update.trip.start_time
        myentity.vehicle.trip.start_date = myentity.trip_update.trip.start_date
        '''
    return fm, count