22'''
33 update_highway_project_years.py
44 Author: npeterson
5- Revised: 12/18/17
5+ Revised: 5/9/18
66 ---------------------------------------------------------------------------
77 This script updates the completion years of projects to be included in
88 Conformity analyses. The final completion year file is received from the
6060# -----------------------------------------------------------------------------
6161tipid_all_csv = os .path .join (MHN .temp_dir , 'tipid_all.csv' )
6262early_scenarios_csv = os .path .join (MHN .out_dir , 'early_transit_scenarios.csv' )
63+ late_scenarios_csv = os .path .join (MHN .out_dir , 'late_transit_scenarios.csv' )
64+ unknown_trans_ids_csv = os .path .join (MHN .out_dir , 'unknown_transit_tipids.csv' )
6365in_year_not_mhn_txt = os .path .join (MHN .out_dir , 'in_year_not_mhn.txt' )
6466in_mhn_not_year_txt = os .path .join (MHN .out_dir , 'in_mhn_not_year.txt' )
6567
111113# -----------------------------------------------------------------------------
112114arcpy .AddMessage ('{0}Checking future transit projects...' .format ('\n ' ))
113115
114- def clear_transit_project_years (proj_years_dict , rail_fc , bus_fc , mover_table , early_scenarios_csv ):
116+ def clear_transit_project_years (proj_years_dict , hwyproj_ids , rail_fc , bus_fc , mover_table ,
117+ early_scenarios_csv , late_scenarios_csv , unknown_trans_ids_csv ):
115118 ''' Remove transit project TIPIDs from the dict after verifying that their
116119 scenarios specified in the MHN/MRN are no earlier than their completion
117120 years. '''
@@ -132,21 +135,55 @@ def clear_transit_project_years(proj_years_dict, rail_fc, bus_fc, mover_table, e
132135
133136 # Compare transit project scenarios against project completion years.
134137 # If any errors exist, write them to file and stop processing.
135- early_scenarios = []
138+ early_scenarios = set ()
139+ late_scenarios = set ()
140+ unknown_tipids = set ()
136141 for tipid , scen in trans_proj_scens .items ():
137- if tipid in proj_years_dict and MHN .scenario_years [(str (scen ))] < proj_years_dict [tipid ]:
138- early_scenarios .append (tipid )
142+ if tipid in proj_years_dict and MHN .scenario_years [str (scen )] < proj_years_dict [tipid ]:
143+ early_scenarios .add (tipid )
144+ elif tipid in proj_years_dict and MHN .scenario_years [str (scen - 100 )] > proj_years_dict [tipid ]:
145+ late_scenarios .add (tipid )
146+ elif tipid not in proj_years_dict and MHN .scenario_years [str (scen )] < MHN .max_year :
147+ unknown_tipids .add (tipid )
148+
139149 if early_scenarios :
140150 with open (early_scenarios_csv , 'w' ) as w :
141151 w .write ('TIPID,COMPLETION_YEAR,FIRST_SCENARIO\n ' )
142- for tipid in early_scenarios :
152+ for tipid in sorted ( early_scenarios ) :
143153 w .write ('{0},{1},{2}\n ' .format (MHN .tipid_from_int (tipid ), proj_years_dict [tipid ], trans_proj_scens [tipid ]))
144154 MHN .die ((
145155 '''ERROR: Some transit projects (future bus, rail and/or people '''
146156 '''mover) reference a scenario that is earlier than their TIPID's '''
147157 '''specified completion year. See {0} for details.'''
148158 ).format (early_scenarios_csv ))
149159
160+ if late_scenarios :
161+ with open (late_scenarios_csv , 'w' ) as w :
162+ w .write ('TIPID,COMPLETION_YEAR,FIRST_SCENARIO\n ' )
163+ for tipid in sorted (late_scenarios ):
164+ w .write ('{0},{1},{2}\n ' .format (MHN .tipid_from_int (tipid ), proj_years_dict [tipid ], trans_proj_scens [tipid ]))
165+ MHN .die ((
166+ '''ERROR: Some transit projects (future bus, rail and/or people '''
167+ '''mover) reference a scenario that is much later than their TIPID's '''
168+ '''specified completion year. See {0} for details.'''
169+ ).format (late_scenarios_csv ))
170+
171+ if unknown_tipids :
172+ with open (unknown_trans_ids_csv , 'w' ) as w :
173+ w .write ('TIPID,FIRST_SCENARIO\n ' )
174+ for tipid in sorted (unknown_tipids ):
175+ w .write ('{0},{1}\n ' .format (MHN .tipid_from_int (tipid ), trans_proj_scens [tipid ]))
176+ MHN .die ((
177+ '''ERROR: Some transit projects (future bus, rail and/or people '''
178+ '''mover) have a TIPID that is not present in {0} or {1}. See {2} '''
179+ '''for details.'''
180+ ).format (tipid_conformed_csv , tipid_exempt_csv , unknown_trans_ids_csv ))
181+
182+ # Ignore transit projects that also have a highway component (e.g. new busway links)
183+ trans_proj_with_hwy_component = set (hwyproj_ids ) & set (trans_proj_scens .keys ())
184+ for tipid in trans_proj_with_hwy_component :
185+ del trans_proj_scens [tipid ]
186+
150187 # Remove transit TIPIDs from project years dictionary
151188 hwyproj_years_dict = proj_years_dict .copy ()
152189 for tipid in trans_proj_scens .keys ():
@@ -183,25 +220,28 @@ def get_trans_proj_scens(table):
183220
184221 return tipid_scens
185222
186-
187- hwyproj_years = clear_transit_project_years (proj_years , mrn_future_fc , MHN .bus_future , people_mover_table , early_scenarios_csv )
223+ # Remove transit-only (bus or rail) projects after checking their scenario codes
224+ common_id_field = MHN .route_systems [MHN .hwyproj ][1 ]
225+ coded_hwyproj = [int (r [0 ]) for r in arcpy .da .SearchCursor (MHN .hwyproj , [common_id_field ])]
226+ hwyproj_years = clear_transit_project_years (
227+ proj_years , coded_hwyproj , mrn_future_fc , MHN .bus_future , people_mover_table ,
228+ early_scenarios_csv , late_scenarios_csv , unknown_trans_ids_csv
229+ )
188230
189231
190232# -----------------------------------------------------------------------------
191233# Read uncodable projects into dictionary.
192234# -----------------------------------------------------------------------------
193- uncodable_proj = []
235+ uncodable_proj = set () # []
194236with open (tipid_uncodable_csv , 'r' ) as no_code :
195237 for row in no_code :
196238 tipid = int (row .strip ().split (',' )[0 ])
197- if tipid not in uncodable_proj :
198- uncodable_proj .append (tipid )
239+ uncodable_proj .add (tipid )
199240
200241
201242# -----------------------------------------------------------------------------
202243# Check for inappropriately coded projects.
203244# -----------------------------------------------------------------------------
204- common_id_field = MHN .route_systems [MHN .hwyproj ][1 ]
205245hwyproj_view = 'hwyproj_view'
206246arcpy .MakeTableView_management (MHN .hwyproj , hwyproj_view )
207247
@@ -239,7 +279,6 @@ def get_trans_proj_scens(table):
239279# -----------------------------------------------------------------------------
240280# Check for still-uncoded projects.
241281# -----------------------------------------------------------------------------
242- coded_hwyproj = [int (r [0 ]) for r in arcpy .da .SearchCursor (MHN .hwyproj , [common_id_field ])]
243282uncoded_hwyproj = [tipid for tipid in hwyproj_years if tipid not in coded_hwyproj and tipid not in uncodable_proj ]
244283if len (uncoded_hwyproj ) == 0 :
245284 arcpy .AddMessage ((
0 commit comments