def interpolate_times(time_point_1, time_point_2, blank_times):
    ''' Simple interpolation method. Assume stop times are evenly spaced between time points.'''
    # [arrival_time, departure_time, id]
    num_blank_stops = len(blank_times)
    # Find the total number of seconds between departing the first time point and arriving at the second time point.
    total_time_interval_secs = hms.hmsdiff(time_point_2[0], time_point_1[1])
    # Find the interval size that divides the total time evenly.
    time_between_stops_secs = total_time_interval_secs / float(num_blank_stops + 1)
    # Increment the interval and assign values to the blank stops
    current_secs = hms.str2sec(time_point_1[1])
    for blank_time in blank_times:
        current_secs += time_between_stops_secs
        blank_time[0] = hms.sec2str(current_secs)
        # Set arrival_time and departure_time to the same value.
        blank_time[1] = blank_time[0]
    return blank_times
Beispiel #2
0
def interpolate_times(time_point_1, time_point_2, blank_times):
    ''' Simple interpolation method. Assume stop times are evenly spaced between time points.'''
    # [arrival_time, departure_time, id]
    num_blank_stops = len(blank_times)
    # Find the total number of seconds between departing the first time point and arriving at the second time point.
    total_time_interval_secs = hms.hmsdiff(time_point_2[0], time_point_1[1])
    # Find the interval size that divides the total time evenly.
    time_between_stops_secs = total_time_interval_secs / float(
        num_blank_stops + 1)
    # Increment the interval and assign values to the blank stops
    current_secs = hms.str2sec(time_point_1[1])
    for blank_time in blank_times:
        current_secs += time_between_stops_secs
        blank_time[0] = hms.sec2str(current_secs)
        # Set arrival_time and departure_time to the same value.
        blank_time[1] = blank_time[0]
    return blank_times
def GetTransitTrips(row, end_time_sec_clean_1, end_time_sec_clean_2, SIDList):
    rows_to_insert = []
    row = list(row)

    # Time direction determines which time we should use
    if BackInTime:
        time_to_use = "start_time"
    else:
        time_to_use = "end_time"

    # Pull out the trip info from the TransitScheduleTable
    scheduleFetch = "SELECT trip_id, start_time, end_time FROM schedules WHERE SOURCEOID=%s AND %s=%s" % (
        row[0], time_to_use, end_time_sec_clean_1)
    c.execute(scheduleFetch)
    scheds = c.fetchall()
    EvalTableList = [stuff for stuff in scheds]

    if not EvalTableList:
        # Try to find trips after rounding in the other direction
        scheduleFetch = "SELECT trip_id, start_time, end_time FROM schedules WHERE SOURCEOID=%s AND %s=%s" % (
            row[0], time_to_use, end_time_sec_clean_2)
        c.execute(scheduleFetch)
        scheds = c.fetchall()
        EvalTableList = [stuff for stuff in scheds]

    if not EvalTableList:
        return rows_to_insert

    for trip in EvalTableList:
        trip_id = trip[0]
        service_id = trip_info_dict[trip_id][1]
        if service_id in SIDList:
            trip_start_time = trip[1]
            if trip_start_time > SecsInDay:
                trip_start_time = trip_start_time - SecsInDay
            trip_end_time = trip[2]
            if trip_end_time > SecsInDay:
                trip_end_time = trip_end_time - SecsInDay

            row[3] = trip_id  #trip_id
            route_id = trip_info_dict[trip_id][0]
            row[4] = RouteDict[route_id][0]  #agency_id
            row[5] = route_id  #route_id
            row[6] = RouteDict[route_id][4]  #route_type
            row[7] = RouteDict[route_id][8]  #route_type_text
            row[8] = RouteDict[route_id][1]  #route_short_name
            row[9] = RouteDict[route_id][2]  #route_long_name
            # Assign stop info
            try:
                start_stop = stop_junctionID_dict[row[24]]
            except KeyError:
                start_stop = "Unknown"
            try:
                end_stop = stop_junctionID_dict[row[25]]
            except KeyError:
                end_stop = "Unknown"
            row[14] = start_stop  #start_stop_id
            try:
                row[15] = stopid_dict[start_stop]  #start_stop_name
            except KeyError:
                row[15] = "Unknown"
            row[16] = end_stop  #end_stop_id
            try:
                row[17] = stopid_dict[end_stop]  #end_stop_name
            except KeyError:
                row[17] = "Unknown"
            # Calculate wait time and transit time
            row[12] = hms.sec2str(trip_start_time)  #depart_timeofday
            row[13] = hms.sec2str(trip_end_time)  #arrive_timeofday
            transit_time = float(
                (float(trip_end_time) - float(trip_start_time)) / 60.0)
            row[11] = round(transit_time, 2)  #transit_time
            leg_time = row[2]
            # Note: When travel is back in time, the wait time occurs after the
            # last transit leg instead of before the first one.
            row[10] = round(leg_time - transit_time, 2)  #wait_time
            if len(EvalTableList) > 1 and row[10] < 0:
                # If multiple route choices were returned, and if this one has a wait time < 0 (a very small <0 value occurs
                # occasionally), skip this because it's the wrong one.
                continue

            rows_to_insert.append(tuple(row))

    return rows_to_insert
        f = codecs.open(outFile, 'w', "utf-8-sig")

    with arcpy.da.SearchCursor(TransitLines, ["OID@", "route_type_text"]) as cur:
        for line in cur:
            SourceOID = line[0]
            route_type = line[1]
            prettyPrint = u"\n\n-- Schedule for TransitLine with ObjectID %s --\nRoute type: %s" % (SourceOID, route_type)
            prettyPrint += "\nstart_time  end_time  weekdays  trip_id  route_id  service_id"

            scheduleFetch = "SELECT trip_id, start_time, end_time from schedules WHERE SourceOID=%s" % SourceOID
            c.execute(scheduleFetch)
            schedules = c.fetchall()
            alltrips = [] # {route_id: {service_id: [trip, trip, trip]}}
            for sched in schedules:
                trip_id = sched[0]
                start_time = hms.sec2str(float(sched[1]))
                end_time = hms.sec2str(float(sched[2]))
                route_id = trip_info_dict[trip_id][0]
                service_id = trip_info_dict[trip_id][1]
                try:
                    weekdays = cal_info_dict[service_id]
                except KeyError:
                    # service_id wasn't present in calendar.txt. Presumably it's handled in calendar_dates.txt
                    weekdays = "-------"
                alltrips.append([start_time, end_time, weekdays, trip_id, route_id, service_id])
            alltrips.sort(key=operator.itemgetter(0))
            for trip in alltrips:
                prettyPrint += u"\n%s  %s  %s  %s  %s  %s" % (trip[0], trip[1], trip[2], trip[3], trip[4], trip[5])

            arcpy.AddMessage(prettyPrint)
            if outFile:
def GetTransitTrips(row, end_time_sec_clean_1, end_time_sec_clean_2, SIDList):
    rows_to_insert = []
    row = list(row)

    # Time direction determines which time we should use
    if BackInTime:
        time_to_use = "start_time"
    else:
        time_to_use = "end_time"

    # Pull out the trip info from the TransitScheduleTable
    scheduleFetch = "SELECT trip_id, start_time, end_time FROM schedules WHERE SOURCEOID=%s AND %s=%s" % (row[0], time_to_use, end_time_sec_clean_1)
    c.execute(scheduleFetch)
    scheds = c.fetchall()
    EvalTableList = [stuff for stuff in scheds]

    if not EvalTableList:
        # Try to find trips after rounding in the other direction
        scheduleFetch = "SELECT trip_id, start_time, end_time FROM schedules WHERE SOURCEOID=%s AND %s=%s" % (row[0], time_to_use, end_time_sec_clean_2)
        c.execute(scheduleFetch)
        scheds = c.fetchall()
        EvalTableList = [stuff for stuff in scheds]

    if not EvalTableList:
        return rows_to_insert

    for trip in EvalTableList:
        trip_id = trip[0]
        service_id = trip_info_dict[trip_id][1]
        if service_id in SIDList:
            trip_start_time = trip[1]
            if trip_start_time > SecsInDay:
                trip_start_time = trip_start_time - SecsInDay
            trip_end_time = trip[2]
            if trip_end_time > SecsInDay:
                trip_end_time = trip_end_time - SecsInDay

            row[3] = trip_id #trip_id
            route_id = trip_info_dict[trip_id][0]
            row[4] = RouteDict[route_id][0] #agency_id
            row[5] = route_id #route_id
            row[6] = RouteDict[route_id][4] #route_type
            row[7] = RouteDict[route_id][8] #route_type_text
            row[8] = RouteDict[route_id][1] #route_short_name
            row[9] = RouteDict[route_id][2] #route_long_name
            # Assign stop info
            try: start_stop = stop_junctionID_dict[row[24]]
            except KeyError: start_stop = "Unknown"
            try: end_stop = stop_junctionID_dict[row[25]]
            except KeyError: end_stop = "Unknown"
            row[14] = start_stop #start_stop_id
            try: row[15] = stopid_dict[start_stop] #start_stop_name
            except KeyError: row[15] = "Unknown"
            row[16] = end_stop #end_stop_id
            try: row[17] = stopid_dict[end_stop] #end_stop_name
            except KeyError: row[17] = "Unknown"
            # Calculate wait time and transit time
            row[12] = hms.sec2str(trip_start_time) #depart_timeofday
            row[13] = hms.sec2str(trip_end_time) #arrive_timeofday
            transit_time = float((float(trip_end_time) - float(trip_start_time)) / 60.0)
            row[11] = round(transit_time, 2) #transit_time
            leg_time = row[2]
            # Note: When travel is back in time, the wait time occurs after the
            # last transit leg instead of before the first one.
            row[10] = round(leg_time - transit_time, 2) #wait_time
            if len(EvalTableList) > 1 and row[10] < 0:
                # If multiple route choices were returned, and if this one has a wait time < 0 (a very small <0 value occurs
                # occasionally), skip this because it's the wrong one.
                continue

            rows_to_insert.append(tuple(row))

    return rows_to_insert