Esempio n. 1
0
def trip_purpose(abm, csv_path=False):
    ''' Export a CSV file containing the number of tours made for each
        purpose and, within them, the purpose of the individual trips. '''
    # Use default output location if none specified
    if not csv_path:
        csv_path = os.path.join(abm._output_dir, 'results_trip_purpose.csv')

    # Count person-tours by tour purpose
    tour_sql = ''' SELECT purpose, participants FROM Tours '''
    tour_counts = {}
    for tour_purpose, participants in abm.query(tour_sql):
        n = abm._unsample(len(participants.split()))
        tour_counts[tour_purpose] = tour_counts.get(tour_purpose, 0) + n

    # Count person-trips by tour purpose and trip destination purpose
    trip_counts = {tour_purpose: {} for tour_purpose in tour_counts.iterkeys()}
    trip_purposes = set()
    trip_sql = (
        ''' SELECT Tours.purpose, Tours.participants, Trips.purpose_d '''
        ''' FROM Trips LEFT JOIN Tours ON Trips.tour_id=Tours.tour_id '''
    )
    for tour_purpose, participants, trip_purpose in abm.query(trip_sql):
        trip_purposes.add(trip_purpose)
        n = abm._unsample(len(participants.split()))
        trip_counts[tour_purpose][trip_purpose] = trip_counts[tour_purpose].get(trip_purpose, 0) + n

    # Write results to CSV
    with open(csv_path, 'wb') as w:
        w.write('PERSON-TOUR PURPOSE,PERSON-TRIP PURPOSE,COUNT\n')
        for tour_purpose in sorted(tour_counts):
            w.write('{0},,{1}\n'.format(tour_purpose.upper(), tour_counts[tour_purpose]))
            for trip_purpose in sorted(trip_purposes):
                w.write(',{0},{1}\n'.format(trip_purpose, trip_counts[tour_purpose].get(trip_purpose, 0)))

    return csv_path
Esempio n. 2
0
def trip_tod(abm, csv_path=False):
    ''' Export a CSV file containing the number of person-trips in each
        time-of-day period, by broad trip purpose. '''
    # Use default output location if none specified
    if not csv_path:
        csv_path = os.path.join(abm._output_dir, 'results_trip_tod.csv')

    # Count person-trips by TOD and purpose
    trips = {i: {'H-W': 0, 'W-H': 0, 'H-O': 0, 'O-H': 0, 'NH': 0} for i in xrange(1,9)}
    trip_sql = (
        ''' SELECT Trips.purpose_o, Trips.purpose_d, Tours.participants, Trips.tod '''
        ''' FROM Trips LEFT JOIN Tours ON Trips.tour_id=Tours.tour_id ORDER BY Trips.trip_id '''
    )

    for purp_o, purp_d, participants, tod in abm.query(trip_sql):
        n = abm._unsample(len(participants.split()))
        if purp_d in ('work', 'school', 'university') and purp_o == 'home':
            purpose = 'H-W'
        elif purp_o in ('work', 'school', 'university') and purp_d == 'home':
            purpose = 'W-H'
        elif purp_o == 'home':
            purpose = 'H-O'
        elif purp_d == 'home':
            purpose = 'O-H'
        else:
            purpose = 'NH'
        trips[tod][purpose] += n

    # Write results to CSV
    with open(csv_path, 'wb') as w:
        w.write('TIME-OF-DAY,HOME-WORK,WORK-HOME,HOME-OTHER,OTHER-HOME,NON-HOME-BASED\n')
        for tod, trip_dict in sorted(trips.iteritems()):
            w.write('{0},{1},{2},{3},{4},{5}\n'.format(tod, trip_dict['H-W'], trip_dict['W-H'], trip_dict['H-O'], trip_dict['O-H'], trip_dict['NH']))

    return csv_path
Esempio n. 3
0
def od_matrix(abm, csv_path=False):
    ''' Export a CSV containing two matrices of person-trip O-D zone
        groups: one for autos and one for transit. '''
    mode_groups = {
        1: 'AUTO',      # Drive alone free
        2: 'AUTO',      # Drive alone pay
        3: 'AUTO',      # Shared ride 2 free
        4: 'AUTO',      # Shared ride 2 pay
        5: 'AUTO',      # Shared ride 3+ free
        6: 'AUTO',      # Shared ride 3+ pay
        7: None,        # Walk
        8: None,        # Bike
        #9: None,       # N/A (formerly 'Walk to local transit')
        10: 'TRANSIT',  # Walk to transit (formerly 'Walk to premium transit')
        11: 'TRANSIT',  # Kiss-n-ride (formerly 'Drive to local transit')
        12: 'TRANSIT',  # Park-n-ride (formerly 'Drive to premium transit')
        13: None,       # Taxi
        14: None,       # School bus
    }

    # Use default output location if none specified
    if not csv_path:
        csv_path = os.path.join(abm._output_dir, 'results_od_matrix.csv')

    # Initialize results matrices
    zngrp_order = ['Chicago', 'Cook Balance', 'DuPage', 'Kane', 'Kendall', 'Lake', 'McHenry', 'Will', 'IL Balance', 'Indiana', 'Wisconsin']
    dim = len(zngrp_order)
    od_mat = {
        'AUTO': [[0 for i in xrange(dim)] for j in xrange(dim)],
        'TRANSIT': [[0 for i in xrange(dim)] for j in xrange(dim)]
    }

    # Append person-trip counts to appropriate matrix cell
    trip_sql = (
        ''' SELECT Trips.zn_o, Trips.zn_d, Trips.mode, Tours.participants '''
        ''' FROM Trips LEFT JOIN Tours ON Trips.tour_id=Tours.tour_id '''
    )
    for zn_o, zn_d, mode, participants in abm.query(trip_sql):
        auto_or_transit = mode_groups[mode]
        if not auto_or_transit:
            continue  # Ignore non-motorized trips
        n = abm._unsample(len(participants.split()))
        for zngrp, zones in abm.zone_groups.iteritems():
            if zn_o in zones:
                zngrp_o = zngrp
                break
        for zngrp, zones in abm.zone_groups.iteritems():
            if zn_d in zones:
                zngrp_d = zngrp
                break
        i = zngrp_order.index(zngrp_o)
        j = zngrp_order.index(zngrp_d)
        od_mat[auto_or_transit][i][j] += n

    # Write results to CSV
    with open(csv_path, 'wb') as w:
        for mode, matrix in sorted(od_mat.iteritems()):
            w.write(mode + ',' + ','.join(zngrp + ' (D)' for zngrp in zngrp_order) + '\n')
            for i, zngrp in enumerate(zngrp_order):
                w.write(zngrp + ' (O),' + ','.join(str(n) for n in matrix[i]) + '\n')
            w.write('\n')

    return csv_path