### End of TOD loop ### # ------------------------------------------------------------------------- # Merge scenario highway and rail linkshape files into linkshape_X00.in. # ------------------------------------------------------------------------- arcpy.AddMessage('\nMerging Scenario {0} ({1}) highway & rail linkshape files...'.format(scen, str(scen_year))) linkshape_hwy = os.path.join(scen_hwy_path, 'highway.linkshape') linkshape_rail = os.path.join(scen_tran_path, 'rail.linkshape') linkshape_dir = MHN.ensure_dir(os.path.join(root_path, 'linkshape')) linkshape_in = os.path.join(linkshape_dir, 'linkshape_{0}.in'.format(scen)) w = open(linkshape_in, 'w') w.write('c HIGHWAY & RAIL LINK SHAPE FILE FOR SCENARIO {0}\n'.format(scen)) w.write('c {0}\n'.format(MHN.timestamp('%d%b%y').upper())) w.write('t linkvertices\n') with open(linkshape_hwy, 'r') as r: for line in r: if line.startswith('a ') or line.startswith('r '): w.write(line) with open(linkshape_rail, 'r') as r: for line in r: if line.startswith('a ') or line.startswith('r '): w.write(line) w.close() ### End of scenario loop ###
unaltered_routes_lyr = 'unaltered_routes_lyr' arcpy.MakeFeatureLayer_management(MHN.bus_future, unaltered_routes_lyr, unaltered_routes_query) updated_routes_fc = os.path.join(MHN.mem, 'updated_routes_fc') arcpy.Merge_management((unaltered_routes_lyr, temp_routes_fc), updated_routes_fc) unaltered_itin_view = 'unaltered_itin_view' arcpy.MakeTableView_management(MHN.route_systems[MHN.bus_future][0], unaltered_itin_view, unaltered_routes_query) updated_itin_table = os.path.join(MHN.mem, 'updated_itin_table') arcpy.Merge_management((unaltered_itin_view, temp_itin_table), updated_itin_table) # ----------------------------------------------------------------------------- # Commit the changes only after everything else has run successfully. # ----------------------------------------------------------------------------- backup_gdb = MHN.gdb[:-4] + '_' + MHN.timestamp() + '.gdb' arcpy.Copy_management(MHN.gdb, backup_gdb) arcpy.AddMessage('{0}Geodatabase temporarily backed up to {1}. (If import fails for any reason, replace {2} with this.)'.format('\n',backup_gdb, MHN.gdb)) arcpy.AddMessage('\nSaving changes to disk...') # Replace header feature class. arcpy.AddMessage('-- ' + MHN.bus_future + '...') arcpy.TruncateTable_management(MHN.bus_future) arcpy.Delete_management(MHN.bus_future) arcpy.CopyFeatures_management(updated_routes_fc, MHN.bus_future) arcpy.Delete_management(updated_routes_fc) # Replace itinerary table. itin_table = MHN.route_systems[MHN.bus_future][0] arcpy.AddMessage('-- ' + itin_table + '...')
updated_route_systems_list = [] for route_system in MHN.route_systems: header = route_system itin = MHN.route_systems[route_system][0] common_id_field = MHN.route_systems[route_system][1] order_field = MHN.route_systems[route_system][2] updated_route_system = update_route_system(header, itin, vertices_comprising, split_dict_ABB, new_ABB_values, common_id_field, order_field) updated_route_systems_list.append(updated_route_system) # ----------------------------------------------------------------------------- # Commit the changes only after everything else has run successfully. # ----------------------------------------------------------------------------- timestamp = MHN.timestamp() backup_gdb = MHN.gdb[:-4] + '_' + timestamp + '.gdb' arcpy.Copy_management(MHN.gdb, backup_gdb) arcpy.AddWarning('\nGeodatabase temporarily backed up to {0}. (If update fails for any reason, replace {1} with this.)'.format(backup_gdb, MHN.gdb)) arcpy.AddMessage('\nSaving changes to disk...') # Replace route system tables and line FCs. for updated_route_system in updated_route_systems_list: # Header feature class: header = updated_route_system[0][0] header_updated = updated_route_system[0][1] arcpy.AddMessage('-- ' + header + '...') arcpy.TruncateTable_management(header) arcpy.Delete_management(header) arcpy.CopyFeatures_management(header_updated, header) arcpy.Delete_management(header_updated)
unaltered_routes_query) updated_routes_fc = os.path.join(MHN.mem, 'updated_routes_fc') arcpy.Merge_management((unaltered_routes_lyr, temp_routes_fc), updated_routes_fc) unaltered_itin_view = 'unaltered_itin_view' arcpy.MakeTableView_management(MHN.route_systems[MHN.bus_future][0], unaltered_itin_view, unaltered_routes_query) updated_itin_table = os.path.join(MHN.mem, 'updated_itin_table') arcpy.Merge_management((unaltered_itin_view, temp_itin_table), updated_itin_table) # ----------------------------------------------------------------------------- # Commit the changes only after everything else has run successfully. # ----------------------------------------------------------------------------- backup_gdb = MHN.gdb[:-4] + '_' + MHN.timestamp() + '.gdb' arcpy.Copy_management(MHN.gdb, backup_gdb) arcpy.AddMessage( '{0}Geodatabase temporarily backed up to {1}. (If import fails for any reason, replace {2} with this.)' .format('\n', backup_gdb, MHN.gdb)) arcpy.AddMessage('\nSaving changes to disk...') # Replace header feature class. arcpy.AddMessage('-- ' + MHN.bus_future + '...') arcpy.TruncateTable_management(MHN.bus_future) arcpy.Delete_management(MHN.bus_future) arcpy.CopyFeatures_management(updated_routes_fc, MHN.bus_future) arcpy.Delete_management(updated_routes_fc) # Replace itinerary table.
# Insert matches from each dict into table. with arcpy.da.InsertCursor(match_table, [match_mhn_field, match_iris_field, 'FREQUENCY', 'FUZZ_SCORE', 'MHN_NAME', 'IRIS_NAME']) as cursor: # 1. Insert boulevard/divided arterial matches: for mhn_id in blvd_match_dict.keys(): cursor.insertRow([mhn_id] + list(blvd_match_dict[mhn_id])) # 2. Insert other arterial matches: for mhn_id in arts_match_dict.keys(): cursor.insertRow([mhn_id] + list(arts_match_dict[mhn_id])) # 3. Insert ramp matches: for mhn_id in ramp_match_dict.keys(): cursor.insertRow([mhn_id] + list(ramp_match_dict[mhn_id])) # 4. Insert expressway matches: for mhn_id in expy_match_dict.keys(): cursor.insertRow([mhn_id] + list(expy_match_dict[mhn_id])) table_name = table_name.format(MHN.timestamp('%Y%m%d')) output_table = arcpy.TableToTable_conversion(match_table, out_workspace, table_name) # --------------------------------------------------------------------- # Wrap up. # --------------------------------------------------------------------- arcpy.AddMessage('Cleaning up...') arcpy.Delete_management(MHN.mem) arcpy.AddMessage('All done! Correspondence table successfully written to {0}.\n'.format(output_table))
for mhn_id in blvd_match_dict.keys(): cursor.insertRow([mhn_id] + list(blvd_match_dict[mhn_id]) + ['BOULEVARD']) # 2. Insert other arterial matches: for mhn_id in arts_match_dict.keys(): cursor.insertRow([mhn_id] + list(arts_match_dict[mhn_id]) + ['ARTERIAL']) # 3. Insert ramp matches: for mhn_id in ramp_match_dict.keys(): cursor.insertRow([mhn_id] + list(ramp_match_dict[mhn_id]) + ['RAMP']) # 4. Insert expressway matches: for mhn_id in expy_match_dict.keys(): cursor.insertRow([mhn_id] + list(expy_match_dict[mhn_id]) + ['EXPRESSWAY']) table_name = table_name.format(MHN.timestamp('%Y%m%d')) output_table = arcpy.TableToTable_conversion(match_table, out_workspace, table_name) # --------------------------------------------------------------------- # Wrap up. # --------------------------------------------------------------------- arcpy.AddMessage('Cleaning up...') arcpy.Delete_management(MHN.mem) arcpy.AddMessage( 'All done! Correspondence table successfully written to {0}.\n'.format( output_table))
unaltered_routes_lyr = "unaltered_routes_lyr" arcpy.MakeFeatureLayer_management(MHN.bus_future, unaltered_routes_lyr, unaltered_routes_query) updated_routes_fc = os.path.join(MHN.mem, "updated_routes_fc") arcpy.Merge_management((unaltered_routes_lyr, temp_routes_fc), updated_routes_fc) unaltered_itin_view = "unaltered_itin_view" arcpy.MakeTableView_management(MHN.route_systems[MHN.bus_future][0], unaltered_itin_view, unaltered_routes_query) updated_itin_table = os.path.join(MHN.mem, "updated_itin_table") arcpy.Merge_management((unaltered_itin_view, temp_itin_table), updated_itin_table) # ----------------------------------------------------------------------------- # Commit the changes only after everything else has run successfully. # ----------------------------------------------------------------------------- backup_gdb = MHN.gdb[:-4] + "_" + MHN.timestamp() + ".gdb" arcpy.Copy_management(MHN.gdb, backup_gdb) arcpy.AddMessage( "{0}Geodatabase temporarily backed up to {1}. (If import fails for any reason, replace {2} with this.)".format( "\n", backup_gdb, MHN.gdb ) ) arcpy.AddMessage("\nSaving changes to disk...") # Replace header feature class. arcpy.AddMessage("-- " + MHN.bus_future + "...") arcpy.TruncateTable_management(MHN.bus_future) arcpy.Delete_management(MHN.bus_future) arcpy.CopyFeatures_management(updated_routes_fc, MHN.bus_future) arcpy.Delete_management(updated_routes_fc)
updated_route_systems_list = [] for route_system in MHN.route_systems: header = route_system itin = MHN.route_systems[route_system][0] common_id_field = MHN.route_systems[route_system][1] order_field = MHN.route_systems[route_system][2] updated_route_system = update_route_system(header, itin, vertices_comprising, split_dict_ABB, new_ABB_values, common_id_field, order_field) updated_route_systems_list.append(updated_route_system) # ----------------------------------------------------------------------------- # Commit the changes only after everything else has run successfully. # ----------------------------------------------------------------------------- timestamp = MHN.timestamp() backup_gdb = MHN.gdb[:-4] + '_' + timestamp + '.gdb' arcpy.Copy_management(MHN.gdb, backup_gdb) arcpy.AddWarning( '\nGeodatabase temporarily backed up to {0}. (If update fails for any reason, replace {1} with this.)' .format(backup_gdb, MHN.gdb)) arcpy.AddMessage('\nSaving changes to disk...') # Replace route system tables and line FCs. for updated_route_system in updated_route_systems_list: # Header feature class: header = updated_route_system[0][0] header_updated = updated_route_system[0][1] arcpy.AddMessage('-- ' + header + '...') arcpy.TruncateTable_management(header) arcpy.Delete_management(header)