diff --git a/scripts/00-BCM_CreateNewNetwork.ipynb b/scripts/00-BCM_CreateNewNetwork.ipynb new file mode 100644 index 0000000..6b7e082 --- /dev/null +++ b/scripts/00-BCM_CreateNewNetwork.ipynb @@ -0,0 +1,923 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Read necessary packages" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import sys\n", + "import yaml\n", + "\n", + "import numpy as np\n", + "import pandas as pd\n", + "import geopandas as gpd\n", + "from network_wrangler import RoadwayNetwork\n", + "from network_wrangler import TransitNetwork\n", + "from network_wrangler import ProjectCard\n", + "from network_wrangler import Scenario\n", + "from network_wrangler import WranglerLogger\n", + "\n", + "from lasso import ModelRoadwayNetwork\n", + "from lasso import StandardTransit\n", + "from lasso import Parameters\n", + "from lasso import mtc\n", + "from lasso import util\n", + "\n", + "from lasso import Project\n", + "\n", + "from lasso import bicounty\n", + "\n", + "import pickle\n", + "\n", + "%load_ext autoreload\n", + "%autoreload 2\n", + "\n", + "import logging\n", + "logger = logging.getLogger(\"WranglerLogger\")\n", + "logger.setLevel(logging.INFO)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Specify Input Directories" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "pickle_dir = \"../../\" #Set to directory where the pickle files are stored\n", + "card_dir = \"../../ProjectCards\"#Set to directory where the cards are stored\n", + "transit_dir = \"../../Transit\"\n", + "lasso_dir = '..'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from lasso import Parameters\n", + "parameters = Parameters(lasso_base_dir = lasso_dir)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Read Scenario Pickle File\n", + "\n", + "#### There are two pickle files available: one for 2015 and one for 2020. To create the 2020 network:\n", + "##### Read the 2015 pickle file, apply the 2020 projects.\n", + "\n", + "#### To create the 2035 network, you can:\n", + "##### - Read the 2015 Pickle file, apply the 2020 and 2035 project cards or\n", + "##### - Read the 2020 Pickle file and apply the 2035 project cards only." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "pickle_file_name = os.path.join(pickle_dir, \"scenario_2015.pickle\")\n", + "curr_scenario = pickle.load(open(pickle_file_name, 'rb'))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Some of the link names are in a list format, they need to be updated" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "curr_scenario.road_net.links_df['name']=curr_scenario.road_net.links_df['name'].apply(lambda x: \"\" if type(x)==int else x)\n", + "curr_scenario.road_net.links_df['name']=curr_scenario.road_net.links_df['name'].apply(lambda x: util.shorten_name(x))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Create 2020 Network\n", + "### (Skip to list of projects for the 2035 scenario if you are starting from 2020 scenario)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## List of Project Cards to be Applied to the 2015 Scenario" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "project_card_list = []\n", + "\n", + "scenario_2015_cards = ['Added_networkforexternals_Sacto2_GN_11142022.yml',\n", + " 'assignable_and_ace_transit_connectors.yml',\n", + " 'CCTA.yml',\n", + " 'Externals_SJQ_Add_complete_network_10032022.yml',\n", + " 'Sonoma_Marin_Area_Rail_Transit_add_rail_link.yml',\n", + " 'Year 2015 Modify Express Lanes and USE on I-580 EB (segment 02) Hacienda Drive to Airway Blvd_v2.yml',\n", + " 'Year 2015 Modify HOV Lanes and USE on CA-237 EB US 101 to North 1st Street_v2.yml',\n", + " 'Year 2015 Modify HOV Lanes and USE on CA-237 WB North 1st Street to US 101_v2.yml',\n", + " 'Year 2015 Modify HOV Lanes and USE on I-680 NB from Alcosta Blvd to Livorna Road_v2.yml',\n", + " 'Year 2015 Modify HOV Lanes and USE on I-680 SB from Benicia Bridge to Alcosta Blvd_v2.yml',\n", + " 'year_2015_managed_lane_i680n_ca_242_to_benicia_bridge.yml']\n", + "\n", + "for card in scenario_2015_cards:\n", + " _filename = os.path.join(card_dir, '2020',card)\n", + " card = ProjectCard.read(_filename, validate = False)\n", + " project_card_list.append(card)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# for item in project_card_list:\n", + "# curr_scenario.apply_project(item)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# base_scenario_2035 = Scenario.create_scenario(base_scenario = curr_scenario)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# all_line_links_2020=pd.read_csv(os.path.join(transit_dir, \"2020\", \"all_transit_links.csv\"))\n", + "# all_line_links_2020.tail()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# model_2020_net = ModelRoadwayNetwork.from_RoadwayNetwork(\n", + "# roadway_network_object = curr_scenario.road_net, \n", + "# parameters = parameters\n", + "# )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# updated_2020_net = mtc.roadway_standard_to_mtc_network(model_2020_net, parameters=parameters)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# outpur_network_2020_dir = os.path.join(pickle_dir, '2020')\n", + "# os.makedirs(outpur_network_2020_dir, exist_ok=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# updated_2020_net.links_mtc_df=updated_2020_net.links_mtc_df[updated_2020_net.links_mtc_df['geometry'].notnull()]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# all_line_links_2020['has_transit'] = 1\n", + "# has_transit = pd.merge(all_line_links_2020, updated_2020_net.links_mtc_df[['A','B','model_link_id']])\n", + "# updated_2020_net.links_mtc_df['name']=updated_2020_net.links_mtc_df['name'].apply(lambda x: \"\" if type(x)==int else x)\n", + "# updated_2020_net.links_mtc_df['name']=updated_2020_net.links_mtc_df['name'].apply(lambda x: util.shorten_name(x))\n", + "# updated_2020_net.links_mtc_df['has_transit']=updated_2020_net.links_mtc_df['model_link_id'].map(dict(zip(has_transit['model_link_id'],has_transit['has_transit']))).fillna(0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# model_2020_net.links_mtc_df = updated_2020_net.links_mtc_df.copy()\n", + "# model_2020_net.nodes_mtc_df = updated_2020_net.nodes_mtc_df.copy()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# model_2020_net.links_mtc_df['BRT'] = 0" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# model_2020_net.write_roadway_as_fixedwidth(\n", + "# output_dir = outpur_network_2020_dir,\n", + "# output_link_txt = 'links.txt',\n", + "# output_node_txt = 'nodes.txt',\n", + "# output_link_header_width_txt = 'links_header_width.txt',\n", + "# output_node_header_width_txt = 'nodes_header_width.txt',\n", + "# output_cube_network_script = 'make_complete_network_from_fixed_width_file.s',\n", + "# #drive_only = True\n", + "# )" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# List of Project Cards for 2035 Scenario \n", + "## (skip here if you are starting from 2020 scenario)\n", + "These projects add new links/nodes to the network. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# project_card_list = []\n", + "set1_card_2035 = [ 'add_centroid_connectors.yml',\n", + " ## Projects in San Joaqun\n", + " 'san_joaquin_05_sj07_1006.yml',\n", + " 'san_joaquin_06_sj07_1005.yml',\n", + " 'san_joaquin_07_sj11_1001.yml',\n", + " 'san_joaquin_08_sj18_1001.yml',\n", + " 'san_joaquin_09_sj07_1014.yml',\n", + " 'san_joaquin_01_sj14_1001.yml',\n", + " 'san_joaquin_02_sj14_1002.yml',\n", + " 'san_joaquin_03_sj07_1003.yml',\n", + " 'san_joaquin_04_sj07_1008.yml',\n", + " ### Projects modifying highway related to transit improvements (BRT lines, Station/Stop Updates etc.)\n", + " '0_01_CongestionPricingDowntownSF_21-T10-091.yml',\n", + " # 'Sonoma_Marin_Area_Rail_Transit_add_rail_link.yml', #If you are starting from 2015 pickle file, comment this card out since it is already added on the 2015 network.\n", + " '0_02_CongestionPricingTreasureIsland_21-T10-092.yml',\n", + " '0_06_SR-237 Corridor Improvement 21-T06-043.wrangler',\n", + " '0_08_I-680 Corridor Improvement 21-T06-022.wrangler',\n", + " '0_10_1_21-T06-027-FBP_SM_027_US101_92.yml',\n", + " '0_10_2_21-T06-027-FBP_SM_033_US101_Holly_Interchange_add.yml',\n", + " '0_10_3_21-T06-027-FBP_SM_033_US101_Holly_Interchange_mod.yml',\n", + " '0_10_4_21-T06-027-FBP_SM_035_Peninsula_101_OnOffRamps.yml',\n", + " '0_10_5_21-T06-027-STIP_ProduceAve.yml',\n", + " '0_11_1_21-T06-028-FBP_SC_072_US101_Trimble_Interchange.yml',\n", + " '0_11_2_21-T06-028-FBP_SC_073_BlossomHill_101Wide.yml',\n", + " '0_11_5_21-T06-028-FBP_SC_081_US101_SR237.wrangler',\n", + " '0_11_7_21-T06-028-FBP_SC_083_US101_Zanker_Skyport_Interchange.yml',\n", + " '0_12_21-T06-015-MAJ_SOL070020_I80_I680_SR12_Int_2B_7_mod.yml',\n", + " '10_21-T11-114_Transform_Valley_Link_add_rail_link.yml',\n", + " '2_1_14_MAJ_MTC050027_Berkeley_Ferry_add_ferry_link.yml',\n", + " '2_4_15_21-T11-97-MAJ_MissionBay_SF_Ferry_add_ferry_link.yml',\n", + " '2_5_16_21_T11-98-MAJ_RedwoodCity_SF_Ferry_add_ferry_link.yml',\n", + " '3_21-T10-083-Central_Subway_ToChinaTown_add_rail_link.yml',\n", + " '5_1_Rex_Blue_add_highway_link.yml',\n", + " 'BART_2020_Fremont_to_Berryessa_add_rail_link.yml',\n", + " # 'BART_2036_Berryessa_to_Santa_Clara_add_rail_link.yml',\n", + " 'Caltrain_Peninsula_Corridor_Electrification_Expansion_add_rail_link.yml',\n", + " 'extra_brt_14th_st_mission_blvd_brt1.yml',\n", + " 'extra_brt_albany_to_oakland_brt2.yml',\n", + " 'extra_brt_broadway_brt1.yml',\n", + " 'extra_brt_grand_ave_brt1.yml',\n", + " 'extra_brt_lower_dimond_to_alameda_brt2.yml',\n", + " 'extra_brt_marin_ave_brt2.yml',\n", + " 'extra_brt_merritt_to_ashland_brt2.yml',\n", + " 'extra_brt_mission_grain_to_grimmer_brt2.yml',\n", + " 'extra_brt_oakland_brt2.yml',\n", + " 'extra_brt_ralph_appezzato_memorial_pwky_brt1.yml',\n", + " 'extra_brt_thomas_berkley_way_grand_ave_brt1.yml',\n", + " 'year_2020_better_market_st_eb.yml',\n", + " 'year_2020_better_market_st_wb.yml',\n", + " 'year_2020_broadway_brt.yml',\n", + " 'year_2030_i_80_wb_bus_only_hov_extension.yml',\n", + " 'year_2020_richmond_san_rafael_bridge_access_improvement.yml',\n", + " 'year_2020_tempo_brt.yml',\n", + " 'year_2021_geary_brt_phase1_eb.yml',\n", + " 'year_2021_geary_brt_phase1_wb.yml',\n", + " 'year_2021_sr4_wb_added_gp_lane_sr242_to_i680.yml',\n", + " 'year_2022_van_ness_brt_nb.yml',\n", + " 'year_2022_van_ness_brt_sb.yml',\n", + " ### Adding new express lanes\n", + " 'year_2023_us101_el_whipple_to_i380.yml',\n", + " 'year_2024_san_pablo_ave_rapid_corridors_project.yml',\n", + " 'year_2025_bay_bridge_forward_i580wb_hov_extension.yml',\n", + " 'year_2025_i680_sr237_to_sr_84_hov_lanes_new_construction.yml',\n", + " 'year_2025_i80_solano_managed_lanes.yml',\n", + " 'year_2025_sr84_widening_rubyhill_to_i680.yml',\n", + " 'year_2025_us101_marin_sonoma_narrows.yml',\n", + " 'year_2028_geary_brt_phase2_eb.yml',\n", + " 'year_2028_geary_brt_phase2_wb.yml',\n", + " 'year_2029_san_pablo_ave_bus_bike_project.yml',\n", + " 'year_2030_23rd_st_brt.yml',\n", + " 'year_2030_broadway_streetscape_improvement_project.yml',\n", + " 'year_2030_i280_us101_nb_hov.yml',\n", + " 'year_2030_i280_us101_sb_hov.yml',\n", + " 'year_2030_i580_wb_rsr_forward.yml',\n", + " 'year_2030_i680_express_lanes_new_roadway_nb_sb.yml',\n", + " 'year_2030_i680_nb_express_lane_completion_2.yml',\n", + " 'year_2030_i680_nb_pttl.yml',\n", + " 'year_2030_i680_sb_pttl.yml',\n", + " 'year_2030_sr37_sears_point_to_mareisland_hov.yml',\n", + " 'year_2030_sr4_wb_el_sr242_to_i680.yml',\n", + " 'year_2030_us101_ml_north_of_i380_to_sf_sm_countyline.yml',\n", + " 'year_2033_i680_el_calaveras_to_us101.yml',\n", + " 'year_2035_bike_walk_access_on_bridges.yml',\n", + " #####updated\n", + " '2028_i580_eb_express_lane_greenville_rd_to_alameda_county_line.yml',\n", + " '2028_i580_eb_express_lane_i238_to_i680.yml',\n", + " '2028_i580_nb_express_lane_i238_to_bay_bridge.yml',\n", + " '2028_i580_sb_express_lane_bay_bridge_to_i238.yml',\n", + " '2028_i580_wb_express_lane_alameda_county_line_to_greenville_rd.yml',\n", + " '2028_i580_wb_express_lane_i680_to_i238.yml',\n", + " 'CC-170016_year_2030_CaminoTassajara_WindemerePkwy_CityOfDublin.yml',\n", + " 'ebart_add_rail_link.yml',\n", + " 'year_2025_i880_nb_23rd_ave_29th_ave_reconfig_interchange.yml',\n", + " 'year_2025_scl101_el_sr237_to_i880.yml',\n", + " 'year_2025_us_101_el_scl_county_line_to_whipple.yml',\n", + " 'year_2030_brentwood_blvd_widening_phaseI.yml',\n", + " 'year_2030_hercules_train_station.yml',\n", + " 'year_2030_i880_industrial_pkwy_roadway_imp.yml',\n", + " 'year_2030_oakland_alameda_access_project.yml',\n", + " 'year_2030_sr4_roadway_improvement.yml',\n", + " 'year_2030_vta_light_rail_green_blue_add_rail_link.yml',\n", + " 'year_2030_vta_light_rail_orange_add_rail_link.yml'\n", + " ]\n", + "\n", + "for card in set1_card_2035:\n", + " _filename = os.path.join(card_dir, '2035','1-2',card)\n", + " card = ProjectCard.read(_filename, validate = False)\n", + " project_card_list.append(card)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "scenario_2050_cards = [\n", + " 'BART_2036_Berryessa_to_Santa_Clara_add_rail_link.yml',\n", + " '2038_i280_eb_express_lane_leland_ave_to_us101.yml',\n", + " # '2038_i280_eb_express_lane_magdalena_ave_to_leland_ave.yml',\n", + " # '2038_i280_wb_express_lane_leland_ave_to_magdalena_ave.yml', \n", + " '2038_i280_wb_express_lane_us101_to_leland_ave.yml',\n", + " 'ALA170045_year2040_I580_fallon_interchange.yml',\n", + " 'SON070004year_2040Novata_OldRedwoodHighway.yml',\n", + " 'year_2040_byron_vasco_connection.yml',\n", + " 'year_2040_i880_nb_hacienda_hegenberger_two_hov_lanes.yml',\n", + " 'year_2040_vta_light_rail_green_add_rail_link.yml'\n", + " ]\n", + "\n", + "for card in scenario_2050_cards:\n", + " _filename = os.path.join(card_dir, '2050',card)\n", + " card = ProjectCard.read(_filename, validate = False)\n", + " project_card_list.append(card)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Subarea Selection for Testing Cards Validity" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# temp_links = curr_scenario.road_net.links_df.copy()\n", + "# temp_nodes = curr_scenario.road_net.nodes_df.copy()\n", + "# temp_shapes = curr_scenario.road_net.shapes_df.copy()\n", + "\n", + "\n", + "# # filter out links, node, shapes for subarea\n", + "# subarea_links = temp_links[temp_links[\"county\"] == \"Contra Costa\"].copy()\n", + "\n", + "# subarea_nodes_list = list(set(subarea_links[\"A\"].unique().tolist() + subarea_links[\"B\"].unique().tolist()))\n", + "# subarea_nodes = temp_nodes[temp_nodes[\"model_node_id\"].isin(subarea_nodes_list)].copy()\n", + "\n", + "# subarea_shapes = temp_shapes[temp_shapes[\"id\"].isin(subarea_links[\"id\"].unique().tolist())].copy()\n", + "\n", + "# # create subarea roadway network\n", + "# subarea_road_net = RoadwayNetwork(links = subarea_links,\n", + "# nodes = subarea_nodes,\n", + "# shapes = subarea_shapes,\n", + "# # carry over other roadway network attributes from curr_scenario\n", + "# crs = curr_scenario.road_net.crs,\n", + "# node_foreign_key = curr_scenario.road_net.node_foreign_key,\n", + "# link_foreign_key = curr_scenario.road_net.link_foreign_key,\n", + "# shape_foreign_key = curr_scenario.road_net.shape_foreign_key,\n", + "# unique_link_ids = curr_scenario.road_net.unique_link_ids,\n", + "# unique_node_ids = curr_scenario.road_net.unique_node_ids,\n", + "# modes_to_network_link_variables = curr_scenario.road_net.modes_to_network_link_variables,\n", + "# modes_to_network_nodes_variables = curr_scenario.road_net.modes_to_network_nodes_variables)\n", + "\n", + "# subarea_scenario = Scenario.create_scenario(base_scenario= {\"road_net\": subarea_road_net})" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Apply project Cards (Phase 1)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for item in project_card_list:\n", + " curr_scenario.apply_project(item)\n", + "\n", + "\n", + "andrade_to_washington_links = [3001245, 3029924, 3123283, 3023846, 3064500, 3093454, 3030010, 3111078, 3116621]\n", + "mission_to_ca237_links = [2049448, 3147664, 3086017, 2009073]\n", + "washington_to_mission_links = [3062326, 3120126, 3065220, 3157167]\n", + " \n", + "i680s_selected_project_links = andrade_to_washington_links + mission_to_ca237_links + washington_to_mission_links\n", + "\n", + "\n", + "curr_scenario.road_net.links_df.loc[curr_scenario.road_net.links_df[\"model_link_id\"].isin(i680s_selected_project_links), \"ML_access\"] = \"all\"\n", + "curr_scenario.road_net.links_df.loc[curr_scenario.road_net.links_df[\"model_link_id\"].isin(i680s_selected_project_links), \"ML_egress\"] = \"all\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "roadway_filename = os.path.join(pickle_dir, 'base_2035.pickle')\n", + "pickle.dump(curr_scenario.road_net, open(roadway_filename, 'wb'))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "curr_scenario.road_net.links_df[curr_scenario.road_net.links_df.duplicated('model_link_id')].model_link_id.unique()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Create Model Network Object" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "model_net = ModelRoadwayNetwork.from_RoadwayNetwork(\n", + " roadway_network_object = curr_scenario.road_net, \n", + " parameters = parameters\n", + ")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Convert the model network to MTC specific format\n", + "This steps creates all the parallel links, adds new nodes and links and calculates the link attributes" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "updated_net = mtc.roadway_standard_to_mtc_network(model_net, parameters=parameters)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## One link in the network has a null geometry. Deleting it and adding it back again with a diferent project card" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "updated_net.links_mtc_df=updated_net.links_mtc_df[updated_net.links_mtc_df['geometry'].notnull()]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "curr_scenario.road_net.links_df = updated_net.links_mtc_df.copy()\n", + "curr_scenario.road_net.nodes_df = updated_net.nodes_mtc_df.copy()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "v01_scenario = Scenario.create_scenario(base_scenario = curr_scenario)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "v01_scenario.road_net.nodes_df['model_node_id'] = v01_scenario.road_net.nodes_df['N']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "project_card_list = []\n", + "set_2_2035 = [ #This one updates one link that was deleted in a previous step because of null geometry\n", + " 'add_extra_transit_lane.yml',\n", + " #Remaining cards were created from cube log files, so they modify existing links and their attributes\n", + " 'dublin_blvd_nparkway_extension.yml',\n", + " 'year_2021_i880_nb_el_dixon_landing_to_leweeling_blvd_hot_conversion.yml',\n", + " 'year_2021_i880_sb_el_hegenberger_rd_to_dixon_landing_hot_conversion.yml',\n", + " 'year_2025_i80_solano_managed_lanes_phase2.yml',\n", + " 'year_2025_sr85_el_i280_to_sr87_eb.yml',\n", + " 'year_2025_sr85_el_i280_to_sr87_wb.yml',\n", + " 'year_2025_sr85_el_sr87_us101_wb.yml',\n", + " 'year_2025_sr85_el_us101_to_i280_eb.yml',\n", + " 'year_2025_sr85_el_us101_to_i280_wb.yml',\n", + " 'year_2025_us101_el.yml',\n", + " 'year_2027_i880_el_sr237_to_us101.yml',\n", + " 'year_2030_el_sr4_hillcrestave_to_ca242.yml',\n", + " 'year_2030_i680_nb_express_lane_completion_1.yml',\n", + " 'year_2030_i80_carquinez_to_baybridge_el.yml',\n", + " 'year_2030_sr85_el_i280_to_sr87_eb.yml',\n", + " 'year_2030_sr85_el_i280_to_sr87_wb.yml',\n", + " 'year_2030_sr85_el_i280_to_sr87_wb_v2.yml',\n", + " 'year_2030_sr87_el_us101_to_sr85.yml',\n", + " 'year_2035_bus_on_shoulder_strategy.yml',\n", + " 'year_2030_us101_el_whipple_to_i380_missing.yml',\n", + " 'year_2035_scl101_el_i880_to_eastdunneave.yml',]\n", + "\n", + "for card in set_2_2035:\n", + " _filename = os.path.join(card_dir, '2035','3',card)\n", + " card = ProjectCard.read(_filename, validate = False)\n", + " project_card_list.append(card)\n", + "\n", + "scenario_2050_cards = [\n", + " #### year 2050 set 2\n", + " '2038_i280_wb_express_lane_leland_ave_to_magdalena_ave_v2.yml',\n", + " '2038_i280_eb_express_lane_magdalena_ave_to_leland_ave_v2.yml'\n", + " ]\n", + "\n", + "for card in scenario_2050_cards:\n", + " _filename = os.path.join(card_dir, '2050',card)\n", + " card = ProjectCard.read(_filename, validate = False)\n", + " project_card_list.append(card)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for item in project_card_list:\n", + " v01_scenario.apply_project(item)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "v01_scenario.road_net.links_df = gpd.GeoDataFrame(v01_scenario.road_net.links_df, geometry=v01_scenario.road_net.links_df.geometry)\n", + "v01_scenario.road_net.nodes_df = gpd.GeoDataFrame(v01_scenario.road_net.nodes_df, geometry=v01_scenario.road_net.nodes_df.geometry)\n", + "v01_scenario.transit_net.set_roadnet(v01_scenario.road_net, validate_consistency=False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "v01_scenario.road_net.links_df['model_link_id'] = np.where((v01_scenario.road_net.links_df['A']==1511000)&(v01_scenario.road_net.links_df['B']==6011000),\n", + " v01_scenario.road_net.links_df['model_link_id'].max()+1,\n", + " v01_scenario.road_net.links_df['model_link_id'])\n", + "\n", + "v01_scenario.road_net.links_df['model_link_id'] = np.where((v01_scenario.road_net.links_df['A']==3031452)&(v01_scenario.road_net.links_df['B']==3078980),\n", + " v01_scenario.road_net.links_df['model_link_id'].max()+1,\n", + " v01_scenario.road_net.links_df['model_link_id'])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Apply Transit Project Cards" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "transit_project_card_dir = os.path.join(card_dir, \"transit_2050\")\n", + "\n", + "v01_scenario_transit = Scenario.create_scenario(\n", + " base_scenario = v01_scenario,\n", + " card_directory = transit_project_card_dir,\n", + " project_cards_list=[],\n", + " validate_project_cards=False\n", + ")\n", + "v01_scenario_transit.apply_all_projects()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "v01_scenario_transit.road_net.links_df[(v01_scenario_transit.road_net.links_df['A']==4533856)&(v01_scenario_transit.road_net.links_df['B']==4535995)]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "v01_scenario_transit.update_transit_net_with_new_road_net()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "standard_transit_net = StandardTransit.fromTransitNetwork(v01_scenario_transit.transit_net, parameters=parameters)\n", + "mtc.write_as_cube_lin(standard_transit_net, parameters, outpath = os.path.join(transit_dir, \"2035\", \"transit2050.lin\"))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Read the Transit Lin File and Add the has_transit Attribute to the Highway Network" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "transit_file = os.path.join(transit_dir, \"2035\", \"transit2050.lin\")\n", + "# gather network links by parsing through the node sequences in transit.lin\n", + "with open(transit_file, \"r\") as f:\n", + " lines = f.readlines()\n", + "\n", + " all_line_links = pd.DataFrame()\n", + " curr_line = None\n", + " line_node_seq = None\n", + "\n", + " for txt in lines:\n", + " if txt.startswith(\"LINE NAME=\"):\n", + " # print (txt.split(\"=\")[1])\n", + " # store the current line name\n", + " curr_line = txt.split(\"\\\"\")[1]\n", + " # reset line_node_seq as an empty list\n", + " line_node_seq = []\n", + "\n", + " # add to node sequence if the first item of txt after split by \",\" and remove whitespace is digit\n", + " # if txt.strip().split(\",\")[0].replace(\" \", \"\").replace(\"-\", \"\").isdigit():\n", + " if txt.strip().split(\",\")[0].replace(\"-\", \"\").isdigit():\n", + " node = int(txt.strip().split(\",\")[0].replace(\"-\", \"\"))\n", + " line_node_seq.append(node)\n", + "\n", + " if curr_line and txt == \"\\n\":\n", + " print(f\"processed line: {curr_line}\")\n", + " # convert previous line_node_seq into df\n", + " line_links = pd.DataFrame({\"line\": curr_line, \"A\": line_node_seq[:-1], \"B\": line_node_seq[1:]})\n", + " # add to all_line_links\n", + " all_line_links = pd.concat([all_line_links, line_links]).reset_index(drop=True)\n", + "\n", + "all_line_links[\"A\"] = all_line_links[\"A\"].astype(int)\n", + "all_line_links[\"B\"] = all_line_links[\"B\"].astype(int)\n", + "all_line_links=all_line_links.drop_duplicates(['A','B'])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# transit_links=pd.read_csv(os.path.join(card_dir,'2035', \"all_transit_links.csv\"))\n", + "transit_all_line_linkslinks=all_line_links.rename(columns={'line':'has_transit'})\n", + "all_line_links['has_transit'] = 1\n", + "has_transit = pd.merge(all_line_links, v01_scenario.road_net.links_df[['A','B','model_link_id']])\n", + "\n", + "v01_scenario.road_net.links_df['name']=v01_scenario.road_net.links_df['name'].apply(lambda x: \"\" if type(x)==int else x)\n", + "v01_scenario.road_net.links_df['name']=v01_scenario.road_net.links_df['name'].apply(lambda x: util.shorten_name(x))\n", + "v01_scenario.road_net.links_df['has_transit']=v01_scenario.road_net.links_df['model_link_id'].map(dict(zip(has_transit['model_link_id'],has_transit['has_transit']))).fillna(0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "model_net_updated = ModelRoadwayNetwork.from_RoadwayNetwork(\n", + " roadway_network_object = v01_scenario.road_net, \n", + " parameters = parameters\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "model_net_updated.nodes_mtc_df = model_net_updated.nodes_df.copy()\n", + "model_net_updated.links_mtc_df = model_net_updated.links_df.copy()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Export Network" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Export as Shapefile" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "outpur_network_2050_dir = os.path.join(pickle_dir, '2050')\n", + "os.makedirs(outpur_network_2050_dir, exist_ok=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "model_net_updated.links_df[[ 'drive_access','name', 'roadway','walk_access', \n", + " 'county', 'model_link_id', 'A', 'B', 'rail_only',\n", + " 'ft',\n", + " 'assignable', 'transit', 'nmt2010',\n", + " 'nmt2020', 'tollbooth', 'bus_only', 'managed',\n", + " 'tollseg', 'cntype', 'useclass_AM', 'useclass_EA',\n", + " 'useclass_EV', 'useclass_MD', 'useclass_PM', 'BRT', 'distance',\n", + " 'lanes_EA', 'lanes_AM', 'lanes_MD', 'lanes_PM', 'lanes_EV','geometry', 'has_transit']].to_file(os.path.join(outpur_network_2050_dir,\"complete_network_2050.shp\"))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Export as Cube Network" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "model_net_updated.write_roadway_as_fixedwidth(\n", + " output_dir = outpur_network_2050_dir,\n", + " output_link_txt = 'links.txt',\n", + " output_node_txt = 'nodes.txt',\n", + " output_link_header_width_txt = 'links_header_width.txt',\n", + " output_node_header_width_txt = 'nodes_header_width.txt',\n", + " output_cube_network_script = 'make_complete_network_from_fixed_width_file.s',\n", + " #drive_only = True\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.7.12 ('bcm_network')", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.12" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "89f6b56da614f13c067a2c1fb1c7958fb30c34c880401865a109116841689d35" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}