# Identify representative runs for bus_base and/or bus_current, as relevant. for bus_fc in bus_fc_dict: arcpy.AddMessage('\nIdentifying representative runs from {0}...'.format(bus_fc)) which_bus = bus_fc_dict[bus_fc] rep_runs_dict[which_bus] = {} for tod in out_tod_periods: arcpy.AddMessage('-- TOD {0}...'.format(tod.upper())) # Export header info of bus routes in current TOD. bus_id_field = MHN.route_systems[bus_fc][1] bus_route_attr = [bus_id_field, 'DESCRIPTION', 'MODE', 'VEHICLE_TYPE', 'HEADWAY', 'SPEED', 'ROUTE_ID', 'START'] bus_route_query = MHN.tod_periods[tod][1] bus_route_view = MHN.make_skinny_table_view(bus_fc, 'bus_route_view', bus_route_attr, bus_route_query) MHN.write_attribute_csv(bus_route_view, bus_route_csv, bus_route_attr) selected_bus_routes = MHN.make_attribute_dict(bus_route_view, bus_id_field, attr_list=[]) arcpy.Delete_management(bus_route_view) # Export itineraries for selected runs. bus_order_field = MHN.route_systems[bus_fc][2] bus_itin_attr = [bus_id_field, 'ITIN_A', 'ITIN_B', bus_order_field, 'LAYOVER', 'DWELL_CODE', 'ZONE_FARE', 'LINE_SERV_TIME', 'TTF'] bus_itin_query = ''' "{0}" IN ('{1}') '''.format(bus_id_field, "','".join((bus_id for bus_id in selected_bus_routes))) bus_itin_view = MHN.make_skinny_table_view(MHN.route_systems[bus_fc][0], 'bus_itin_view', bus_itin_attr, bus_itin_query) MHN.write_attribute_csv(bus_itin_view, bus_itin_csv, bus_itin_attr) arcpy.Delete_management(bus_itin_view) # Process exported route & itin tables with gtfs_reformat_feed.sas. sas1_sas = os.path.join(MHN.prog_dir, '{0}.sas'.format(sas1_name)) sas1_output = os.path.join(MHN.temp_dir, 'bus_{0}_runs_{1}.csv'.format(which_bus, tod)) sas1_args = [MHN.prog_dir, bus_route_csv, bus_itin_csv, oneline_itin_txt, feed_groups_txt, sas1_output, tod]
MHN.write_arc_flag_file(tollsys_flag, '"TOLLSYS" = 1') # ----------------------------------------------------------------------------- # Check for hwyproj_coding lane conflicts/reductions in future networks. # ----------------------------------------------------------------------------- arcpy.AddMessage( "\nChecking for conflicting highway project coding (i.e. lane reductions) and missing project years...\n" ) hwyproj_id_field = MHN.route_systems[MHN.hwyproj][1] # Export projects with valid completion years. overlap_year_attr = [hwyproj_id_field, "COMPLETION_YEAR"] overlap_year_query = '"COMPLETION_YEAR" NOT IN (0,9999)' overlap_year_view = MHN.make_skinny_table_view(MHN.hwyproj, "overlap_year_view", overlap_year_attr, overlap_year_query) MHN.write_attribute_csv(overlap_year_view, overlap_year_csv, overlap_year_attr) overlap_projects = [r[0] for r in arcpy.da.SearchCursor(overlap_year_view, [hwyproj_id_field])] arcpy.Delete_management(overlap_year_view) # Export coding for valid projects. overlap_transact_attr = [ hwyproj_id_field, "ACTION_CODE", "NEW_DIRECTIONS", "NEW_TYPE1", "NEW_TYPE2", "NEW_AMPM1", "NEW_AMPM2", "NEW_POSTEDSPEED1", "NEW_POSTEDSPEED2", "NEW_THRULANES1",
if invalid_hwyproj: MHN.die( 'The following highway projects have no completion year: {0}'.format( ', '.join(invalid_hwyproj))) # ----------------------------------------------------------------------------- # Export highway project coding info to determine future arc availability. # ----------------------------------------------------------------------------- hwyproj_id_field = MHN.route_systems[MHN.hwyproj][1] # Export projects with valid completion years. year_attr = (hwyproj_id_field, 'COMPLETION_YEAR') year_query = '{0} <= {1}'.format("COMPLETION_YEAR", MHN.max_year) year_view = MHN.make_skinny_table_view(MHN.hwyproj, 'year_view', year_attr, year_query) MHN.write_attribute_csv(year_view, year_csv, year_attr) projects = MHN.make_attribute_dict(year_view, hwyproj_id_field, attr_list=[]) arcpy.Delete_management(year_view) # Export coding for valid projects. transact_attr = (hwyproj_id_field, 'ABB', 'ACTION_CODE', 'NEW_DIRECTIONS') transact_query = ''' "{0}" IN ('{1}') '''.format( hwyproj_id_field, "','".join((hwyproj_id for hwyproj_id in projects))) transact_view = MHN.make_skinny_table_view(MHN.route_systems[MHN.hwyproj][0], 'transact_view', transact_attr, transact_query) MHN.write_attribute_csv(transact_view, transact_csv, transact_attr) project_arcs = MHN.make_attribute_dict(transact_view, 'ABB', attr_list=[]) arcpy.Delete_management(transact_view) # Export base year arc attributes.
MHN.delete_if_exists(network_csv) MHN.delete_if_exists(update_link_csv) MHN.delete_if_exists(flag_node_csv) # ----------------------------------------------------------------------------- # Write data relevant to specified year and pass to SAS for processing. # ----------------------------------------------------------------------------- arcpy.AddMessage('\nPreparing {0} network attributes...'.format(build_year)) # Export coding for highway projects completed by scenario year. hwyproj_id_field = MHN.route_systems[MHN.hwyproj][1] year_attr = [hwyproj_id_field,'COMPLETION_YEAR'] year_query = '"COMPLETION_YEAR" <= {0}'.format(build_year) year_view = MHN.make_skinny_table_view(MHN.hwyproj, 'year_view', year_attr, year_query) MHN.write_attribute_csv(year_view, year_csv, year_attr) hwy_projects = MHN.make_attribute_dict(year_view, hwyproj_id_field, attr_list=[]) arcpy.Delete_management(year_view) transact_attr = [hwyproj_id_field,'ACTION_CODE','NEW_DIRECTIONS','NEW_TYPE1','NEW_TYPE2','NEW_AMPM1','NEW_AMPM2','NEW_POSTEDSPEED1', 'NEW_POSTEDSPEED2','NEW_THRULANES1','NEW_THRULANES2','NEW_THRULANEWIDTH1','NEW_THRULANEWIDTH2','ADD_PARKLANES1', 'ADD_PARKLANES2','ADD_SIGIC','ADD_CLTL','ADD_RRGRADECROSS','NEW_TOLLDOLLARS','NEW_MODES','TOD','ABB','REP_ANODE','REP_BNODE'] transact_query = '"{0}" IN (\'{1}\')'.format(hwyproj_id_field, "','".join((hwyproj_id for hwyproj_id in hwy_projects))) transact_view = MHN.make_skinny_table_view(MHN.route_systems[MHN.hwyproj][0], 'transact_view', transact_attr, transact_query) MHN.write_attribute_csv(transact_view, transact_csv, transact_attr) hwy_abb = MHN.make_attribute_dict(transact_view, 'ABB', attr_list=[]) arcpy.Delete_management(transact_view) # Export arc & node attributes of all baselinks and skeletons used in # projects completed by scenario year. network_attr = ['ANODE','BNODE','ABB','DIRECTIONS','TYPE1','TYPE2','AMPM1','AMPM2','POSTEDSPEED1','POSTEDSPEED2',
# ----------------------------------------------------------------------------- # Check for hwyproj_coding lane conflicts/reductions in future networks. # ----------------------------------------------------------------------------- arcpy.AddMessage( '\nChecking for conflicting highway project coding (i.e. lane reductions) and missing project years...\n' ) hwyproj_id_field = MHN.route_systems[MHN.hwyproj][1] # Export projects with valid completion years. overlap_year_attr = [hwyproj_id_field, 'COMPLETION_YEAR'] overlap_year_query = '"COMPLETION_YEAR" NOT IN (0,9999)' overlap_year_view = MHN.make_skinny_table_view(MHN.hwyproj, 'overlap_year_view', overlap_year_attr, overlap_year_query) MHN.write_attribute_csv(overlap_year_view, overlap_year_csv, overlap_year_attr) overlap_projects = [ r[0] for r in arcpy.da.SearchCursor(overlap_year_view, [hwyproj_id_field]) ] arcpy.Delete_management(overlap_year_view) # Export coding for valid projects. overlap_transact_attr = [ hwyproj_id_field, 'ACTION_CODE', 'NEW_DIRECTIONS', 'NEW_TYPE1', 'NEW_TYPE2', 'NEW_AMPM1', 'NEW_AMPM2', 'NEW_POSTEDSPEED1', 'NEW_POSTEDSPEED2', 'NEW_THRULANES1', 'NEW_THRULANES2', 'NEW_THRULANEWIDTH1', 'NEW_THRULANEWIDTH2', 'ADD_PARKLANES1', 'ADD_PARKLANES2', 'ADD_SIGIC', 'ADD_CLTL', 'ADD_RRGRADECROSS', 'NEW_TOLLDOLLARS', 'NEW_MODES', 'ABB', 'REP_ANODE', 'REP_BNODE' ] overlap_transact_query = ''' "{0}" IN ('{1}') '''.format(
# Export highway project coding info to determine future arc availability. # ----------------------------------------------------------------------------- hwyproj_id_field = MHN.route_systems[MHN.hwyproj][1] # Identify highway projects to be completed by bus year. year_attr = (hwyproj_id_field, 'COMPLETION_YEAR') year_query = '{0} <= {1}'.format("COMPLETION_YEAR", network_year) year_view = MHN.make_skinny_table_view(MHN.hwyproj, 'year_view', year_attr, year_query) projects = MHN.make_attribute_dict(year_view, hwyproj_id_field, attr_list=[]) arcpy.Delete_management(year_view) # Export coding for identified projects. transact_attr = (hwyproj_id_field, 'ABB', 'ACTION_CODE', 'NEW_POSTEDSPEED1', 'NEW_POSTEDSPEED2', 'NEW_DIRECTIONS') transact_query = ''' "{0}" IN ('{1}') '''.format(hwyproj_id_field, "','".join((hwyproj_id for hwyproj_id in projects))) transact_view = MHN.make_skinny_table_view(MHN.route_systems[MHN.hwyproj][0], 'transact_view', transact_attr, transact_query) MHN.write_attribute_csv(transact_view, transact_csv, transact_attr[1:]) project_arcs = MHN.make_attribute_dict(transact_view, 'ABB', attr_list=[]) arcpy.Delete_management(transact_view) # Export arc attributes for bus year network. network_attr = ('ANODE', 'BNODE', 'BASELINK', 'ABB', 'DIRECTIONS', 'TYPE1', 'TYPE2', 'POSTEDSPEED1', 'POSTEDSPEED2', 'MILES') network_query = ''' "BASELINK" = '1' OR "ABB" IN ('{0}') '''.format("','".join((arc_id for arc_id in project_arcs if arc_id[-1] != '1'))) network_view = MHN.make_skinny_table_view(MHN.arc, 'network_view', network_attr, network_query) MHN.write_attribute_csv(network_view, network_csv, network_attr) arcpy.Delete_management(network_view) # Export node coordinates. nodes_attr = ('NODE', 'POINT_X', 'POINT_Y') nodes_view = MHN.make_skinny_table_view(MHN.node, 'nodes_view', nodes_attr) MHN.write_attribute_csv(nodes_view, nodes_csv, nodes_attr) arcpy.Delete_management(nodes_view)
# Clean up old temp files, if necessary. # ----------------------------------------------------------------------------- MHN.delete_if_exists(sas1_log) MHN.delete_if_exists(sas1_lst) MHN.delete_if_exists(mhn_links_csv) MHN.delete_if_exists(projects_csv) # ----------------------------------------------------------------------------- # Use SAS program to validate coding before import. # ----------------------------------------------------------------------------- arcpy.AddMessage('{0}Validating coding in {1}...'.format('\n', xls)) mhn_links_attr = ['ANODE', 'BNODE', 'BASELINK'] mhn_links_query = ''' "BASELINK" IN ('0', '1') ''' # Ignore BASELINK > 1 mhn_links_view = MHN.make_skinny_table_view(MHN.arc, 'mhn_links_view', mhn_links_attr, mhn_links_query) MHN.write_attribute_csv(mhn_links_view, mhn_links_csv, mhn_links_attr) sas1_sas = os.path.join(MHN.prog_dir, '{0}.sas'.format(sas1_name)) sas1_args = [xls, mhn_links_csv, projects_csv, sas1_lst] MHN.submit_sas(sas1_sas, sas1_log, sas1_lst, sas1_args) if not os.path.exists(sas1_log): MHN.die('{0} did not run!'.format(sas1_sas)) elif not os.path.exists(projects_csv): MHN.die('{0} did not finish successfully! Please see {1}'.format(sas1_sas, sas1_log)) elif os.path.exists(sas1_lst): MHN.die('Problems with project coding. Please see {0}.'.format(sas1_lst)) else: os.remove(sas1_log) os.remove(mhn_links_csv)
# Clean up old temp files, if necessary. # ----------------------------------------------------------------------------- MHN.delete_if_exists(sas1_log) MHN.delete_if_exists(sas1_lst) MHN.delete_if_exists(mhn_links_csv) MHN.delete_if_exists(projects_csv) # ----------------------------------------------------------------------------- # Use SAS program to validate coding before import. # ----------------------------------------------------------------------------- arcpy.AddMessage('{0}Validating coding in {1}...'.format('\n', xls)) mhn_links_attr = ['ANODE', 'BNODE', 'BASELINK'] mhn_links_query = ''' "BASELINK" IN ('0', '1') ''' # Ignore BASELINK > 1 mhn_links_view = MHN.make_skinny_table_view(MHN.arc, 'mhn_links_view', mhn_links_attr, mhn_links_query) MHN.write_attribute_csv(mhn_links_view, mhn_links_csv, mhn_links_attr) sas1_sas = os.path.join(MHN.prog_dir, '{0}.sas'.format(sas1_name)) sas1_args = [xls, mhn_links_csv, projects_csv, sas1_lst] MHN.submit_sas(sas1_sas, sas1_log, sas1_lst, sas1_args) if not os.path.exists(sas1_log): MHN.die('{0} did not run!'.format(sas1_sas)) elif not os.path.exists(projects_csv): MHN.die('{0} did not finish successfully! Please see {1}'.format( sas1_sas, sas1_log)) elif os.path.exists(sas1_lst): MHN.die('Problems with project coding. Please see {0}.'.format(sas1_lst)) else: os.remove(sas1_log) os.remove(mhn_links_csv)