def copy_route_hways(r_short_name, r_long_name, hways_dir_in, hways_dir_out): route_print_name = misc_utils.routeNameFileReady(r_short_name, r_long_name) route_hways_fnames = glob.glob( "%s%s%s-hways-*-all.csv" % (hways_dir_in, os.sep, \ route_print_name)) copy_path_out = misc_utils.get_win_safe_path(hways_dir_out) for hways_fname in route_hways_fnames: copy_path_in = misc_utils.get_win_safe_path(hways_fname) shutil.copy(copy_path_in, copy_path_out) return
def copy_route_speeds(r_short_name, r_long_name, speeds_dir_in, speeds_dir_out): route_print_name = misc_utils.routeNameFileReady( r_short_name, r_long_name) route_speeds_fnames = glob.glob( "%s%s%s-speeds-*-all.csv" % (speeds_dir_in, os.sep, \ route_print_name)) copy_path_out = misc_utils.get_win_safe_path(speeds_dir_out) for speeds_fname in route_speeds_fnames: copy_path_in = misc_utils.get_win_safe_path(speeds_fname) shutil.copy(copy_path_in, copy_path_out) return
def write_avg_speeds_on_segments(stop_gtfs_ids_to_names_map, period_avg_speeds, seg_distances, periods, csv_fname, round_places): # Use absolute path to deal with Windows issues with long paths. safe_path_fname = misc_utils.get_win_safe_path(csv_fname) if sys.version_info >= (3, 0, 0): csv_file = open(safe_path_fname, 'w', newline='') else: csv_file = open(safe_path_fname, 'wb') writer = csv.writer(csv_file, delimiter=';') period_names = misc_utils.get_time_period_name_strings(periods) writer.writerow(AVG_SPEED_HEADERS + period_names) s_id_pairs = period_avg_speeds.keys() for s_id_pair in s_id_pairs: avg_speeds_on_seg = period_avg_speeds[s_id_pair] if round_places: assert round_places >= 1 avg_speeds_on_seg_output = map(lambda x: round(x, round_places), avg_speeds_on_seg) else: avg_speeds_on_seg_output = avg_speeds_on_seg if seg_distances: dist = round(seg_distances[s_id_pair], round_places) else: dist = 0 writer.writerow( [s_id_pair[0], stop_gtfs_ids_to_names_map[int(s_id_pair[0])], \ s_id_pair[1], stop_gtfs_ids_to_names_map[int(s_id_pair[1])], \ dist] \ + avg_speeds_on_seg_output) csv_file.close() return
def write_headways_minutes(stop_gtfs_ids_to_names_map, period_headways, periods, csv_fname, stop_id_order=None): safe_path_fname = misc_utils.get_win_safe_path(csv_fname) if sys.version_info >= (3, 0, 0): csv_file = open(safe_path_fname, 'w', newline='') else: csv_file = open(safe_path_fname, 'wb') writer = csv.writer(csv_file, delimiter=';') period_names = misc_utils.get_time_period_name_strings(periods) writer.writerow(HWAYS_PER_STOP_HDRS + period_names) if stop_id_order is None: s_ids = list(period_headways.keys()) else: s_ids = stop_id_order for s_id in s_ids: period_headways_at_stop = period_headways[s_id] writer.writerow([s_id, stop_gtfs_ids_to_names_map[int(s_id)]] \ + period_headways_at_stop) csv_file.close() return
def read_route_hways_all_routes_all_stops(per_route_hways_fname): safe_path_fname = misc_utils.get_win_safe_path(per_route_hways_fname) csv_in_file = open(safe_path_fname, 'r') reader = csv.reader(csv_in_file, delimiter=';') avg_hways_all_stops = {} r_ids_to_names_map = {} r_id_i = AVG_HWAYS_ALL_STOPS_HDRS.index('route_id') r_s_name_i = AVG_HWAYS_ALL_STOPS_HDRS.index('route_short_name') r_l_name_i = AVG_HWAYS_ALL_STOPS_HDRS.index('route_long_name') sp_i = AVG_HWAYS_ALL_STOPS_HDRS.index('serv_period') td_i = AVG_HWAYS_ALL_STOPS_HDRS.index('trips_dir') headers = next(reader) n_base_cols = len(AVG_HWAYS_ALL_STOPS_HDRS) tp_strs = headers[n_base_cols:] tps = misc_utils.get_time_periods_from_strings(tp_strs) for row in reader: r_id = row[r_id_i] r_short_name = row[r_s_name_i] if not r_short_name: r_short_name = None r_long_name = row[r_l_name_i] if not r_long_name: r_long_name = None r_ids_to_names_map[r_id] = r_short_name, r_long_name serv_period = row[sp_i] trips_dir = row[td_i] avg_hways_in_tps = list(map(float, row[n_base_cols:])) if r_id not in avg_hways_all_stops: avg_hways_all_stops[r_id] = {} avg_hways_all_stops[r_id][(trips_dir, serv_period)] = avg_hways_in_tps csv_in_file.close() return avg_hways_all_stops, tps, r_ids_to_names_map
def write_avg_speeds_on_segments(stop_gtfs_ids_to_names_map, period_avg_speeds, seg_distances, periods, csv_fname, round_places): # Use absolute path to deal with Windows issues with long paths. safe_path_fname = misc_utils.get_win_safe_path(csv_fname) if sys.version_info >= (3,0,0): csv_file = open(safe_path_fname, 'w', newline='') else: csv_file = open(safe_path_fname, 'wb') writer = csv.writer(csv_file, delimiter=';') period_names = misc_utils.get_time_period_name_strings(periods) writer.writerow(AVG_SPEED_HEADERS + period_names) s_id_pairs = period_avg_speeds.keys() for s_id_pair in s_id_pairs: avg_speeds_on_seg = period_avg_speeds[s_id_pair] if round_places: assert round_places >= 1 avg_speeds_on_seg_output = map(lambda x: round(x, round_places), avg_speeds_on_seg) else: avg_speeds_on_seg_output = avg_speeds_on_seg if seg_distances: dist = round(seg_distances[s_id_pair], round_places) else: dist = 0 writer.writerow( [s_id_pair[0], stop_gtfs_ids_to_names_map[int(s_id_pair[0])], \ s_id_pair[1], stop_gtfs_ids_to_names_map[int(s_id_pair[1])], \ dist] \ + avg_speeds_on_seg_output) csv_file.close() return
def read_avg_speeds_on_segments(csv_fname, sort_seg_stop_id_pairs=False): safe_fpath = misc_utils.get_win_safe_path(csv_fname) csv_file = open(safe_fpath, 'r') reader = csv.reader(csv_file, delimiter=';') headers = reader.next() tperiod_strings = headers[len(AVG_SPEED_HEADERS):] time_periods = misc_utils.get_time_periods_from_strings(tperiod_strings) dist_row_i = AVG_SPEED_HEADERS.index('seg_dist_m') stop_a_id_i = AVG_SPEED_HEADERS.index('Stop_a_id') stop_b_id_i = AVG_SPEED_HEADERS.index('Stop_b_id') stop_a_name_i = AVG_SPEED_HEADERS.index('Stop_a_name') stop_b_name_i = AVG_SPEED_HEADERS.index('Stop_b_name') seg_distances = {} r_avg_speeds_on_segs = {} stop_gtfs_ids_to_names_map = {} for row in reader: stop_a_id = row[stop_a_id_i] stop_b_id = row[stop_b_id_i] stop_a_name = row[stop_a_name_i] stop_b_name = row[stop_b_name_i] if not stop_a_name: stop_a_name = None if not stop_b_name: stop_b_name = None stop_gtfs_ids_to_names_map[int(stop_a_id)] = stop_a_name stop_gtfs_ids_to_names_map[int(stop_b_id)] = stop_b_name stop_ids = stop_a_id, stop_b_id if sort_seg_stop_id_pairs: stop_ids = tuple(map(str, sorted(map(int, stop_ids)))) seg_distances[stop_ids] = float(row[dist_row_i]) speeds_in_tps = map(float, row[len(AVG_SPEED_HEADERS):]) r_avg_speeds_on_segs[stop_ids] = speeds_in_tps csv_file.close() return time_periods, r_avg_speeds_on_segs, seg_distances, \ stop_gtfs_ids_to_names_map
def write_route_hways_all_routes_all_stops(r_ids_to_names_map, time_periods, avg_hways_all_stops, output_fname, round_places=2): print("Writing all route average headways in TPs to file %s ..." \ % output_fname) safe_path_fname = misc_utils.get_win_safe_path(output_fname) if sys.version_info >= (3, 0, 0): csv_file = open(safe_path_fname, 'w', newline='') else: csv_file = open(safe_path_fname, 'wb') writer = csv.writer(csv_file, delimiter=';') period_names = misc_utils.get_time_period_name_strings(time_periods) route_ids_sorted = sorted(list(avg_hways_all_stops.keys()), key=lambda x: int(x)) writer.writerow(AVG_HWAYS_ALL_STOPS_HDRS + period_names) for route_id in route_ids_sorted: avg_hways_all_stops_by_dir_period_pairs = avg_hways_all_stops[route_id] r_short_name, r_long_name = r_ids_to_names_map[route_id] avg_hways_all_stops_by_dpps_sorted = \ sorted(list(avg_hways_all_stops_by_dir_period_pairs.items()), key=lambda x: (x[0][1], x[0][0])) for dir_period_pair, avg_hways_in_tps in \ avg_hways_all_stops_by_dpps_sorted: trips_dir = dir_period_pair[0] serv_period = dir_period_pair[1] avg_hways_in_tps_rnd = [ round(x, round_places) for x in avg_hways_in_tps ] writer.writerow([route_id, r_short_name, r_long_name, \ serv_period, trips_dir] + avg_hways_in_tps_rnd) csv_file.close() print("... done writing.") return
def read_headways_minutes(csv_fname): safe_fpath = misc_utils.get_win_safe_path(csv_fname) csv_file = open(safe_fpath, 'r') reader = csv.reader(csv_file, delimiter=';') headers = next(reader) tperiod_strings = headers[len(HWAYS_PER_STOP_HDRS):] time_periods = misc_utils.get_time_periods_from_strings(tperiod_strings) stop_id_i = HWAYS_PER_STOP_HDRS.index('Stop_id') stop_name_i = HWAYS_PER_STOP_HDRS.index('Stop_name') seg_distances = {} headways_at_stops_in_tps = {} stop_gtfs_ids_to_names_map = {} for row in reader: stop_id = row[stop_id_i] stop_name = row[stop_name_i] if not stop_name: stop_name = None stop_gtfs_ids_to_names_map[int(stop_id)] = stop_name hways_in_tps = list(map(float, row[len(HWAYS_PER_STOP_HDRS):])) headways_at_stops_in_tps[stop_id] = hways_in_tps csv_file.close() return time_periods, headways_at_stops_in_tps, \ stop_gtfs_ids_to_names_map