forked from CMAP-REPOS/mrn_programs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport_rail_coding.py
290 lines (253 loc) · 13.1 KB
/
import_rail_coding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
###############################################################################
# IMPORT_RAIL_CODING.PY #
# Craig Heither #
# Last revised 8/22/2017 #
# #
# This program is used to import new or revised rail route coding into #
# "railnet_route_rail_lines". The "xls" variable listed below should be #
# updated to identify the spreadsheet in the Import\ directory that #
# holds the coding. #
# #
# All routes are re-built based on the arc geometry to ensure they are #
# coincident with the underlying links. #
# ------------------------- #
# Revision summary: #
# 06-03-2010: added coding to update route geometry when run. #
# 09-14-2010: updated for ArcMap 10 (arcgisscripting replaced by arcpy & #
# revised cursor coding based on ESRI changes). #
# 04-05-2011: SAS call moved to sasrun.bat. #
# 09-26-2011: For Route table update: Index and Table join procedures #
# replaced by more efficient Search and Update cursor code. #
# 01-24-2012: Revised to accept GTFS or spreadsheet coding. #
# NRF 8/22/17: Updates new future coding TOD field. #
###############################################################################
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------
# Import System Modules
# ---------------------------------------------------------------
import sys, os, arcpy, subprocess, time, platform, datetime, fileinput
from datetime import date
from arcpy import env
arcpy.OverwriteOutput = 1
# ---------------------------------------------------------------
# Local variables
# ---------------------------------------------------------------
c = "V:\Secure\Master_Rail" # working directory
d = str.replace(c, "\\", '\\\\')
e = d + "\\Temp"
f = str.replace(c, "\\", '/') + "/mrn_programs"
rail_test = e
railnet_arc = "railnet_arc"
mrn_gdb = d + "\\mrn.gdb"
railnet = mrn_gdb + "\\railnet"
Temp = e
t = date.today()
x = date.__str__(t)
x1 = str.replace(x, "-", "")
new_segments_dbf = e + "\\new_segments.dbf"
temp_route_shp = e + "\\temp_route.shp"
outFl = e + "\\geom_out.txt"
infl = e + "\\geom_in.txt"
rte_updt = e + "\\rte_updt.dbf"
test = railnet + "\\test"
outRtFl = e + "\\rte_out.txt"
# ---------------------------------------------------------------
# Read Script Arguments: Coding Input Files
# ---------------------------------------------------------------
param1 = arcpy.GetParameterAsText(0)
param2 = arcpy.GetParameterAsText(1)
param3 = arcpy.GetParameterAsText(2)
param4 = arcpy.GetParameterAsText(3)
param5 = arcpy.GetParameterAsText(5)
rail_routes = param4
railrt = railnet + "\\" + param4
itinerary = d + "\\mrn.gdb\\" + param4 + "_itin"
orig_itinerary_dbf = d + "\\" + param4 + "_itin_" + x1 + ".dbf"
if param1 != '':
arcpy.AddMessage("---> Input Rail Coding Spreadsheet is " + param1 +" ..." )
flag = "1"
y = c + "$" + orig_itinerary_dbf + "$" + param1 + "$" + flag + "$X" # SAS -sysparm parameters
elif param2 != '' and param3 != '':
arcpy.AddMessage("---> Transit Feed Input Route File is " + param2 +" ..." )
arcpy.AddMessage("---> Transit Feed Input Itinerary File is " + param3 +" ..." )
flag = "2"
y = c + "$" + orig_itinerary_dbf + "$" + param2 + "$" + flag + "$" + param3 + "$" + param5 # SAS -sysparm parameters
else:
arcpy.AddMessage("---> You Must Enter the Appropriate Input File(s) to Run this Script!!!" )
sys.exit([1])
## -- set up to run SAS program --
bat = f + "/sasrun.bat" # batch file name
fl = "geometry_update" # SAS file name
z = f + "/" + fl + ".sas"
sas_log_file = d + "\\Temp\\" + fl + ".log"
sas_list_file = d + "\\Temp\\" + fl + ".lst"
cmd = [ bat, z, y, sas_log_file, sas_list_file ] # SAS call
# ---------------------------------------------------------------
# Cleanup files if needed
# ---------------------------------------------------------------
if os.path.exists(temp_route_shp):
arcpy.Delete_management(temp_route_shp, "ShapeFile")
if os.path.exists(orig_itinerary_dbf):
arcpy.Delete_management(orig_itinerary_dbf, "DbaseTable")
if os.path.exists(new_segments_dbf):
arcpy.Delete_management(new_segments_dbf, "DbaseTable")
if os.path.exists(rte_updt):
arcpy.Delete_management(rte_updt, "DbaseTable")
if os.path.exists(test):
arcpy.Delete_management(test)
if os.path.exists(sas_list_file):
os.remove(sas_list_file)
if os.path.exists(outFl):
os.remove(outFl)
if os.path.exists(infl):
os.remove(infl)
if os.path.exists(outRtFl):
os.remove(outRtFl)
# ---------------------------------------------------------------
# Store a Copy of Current Route and Itinerary Coding
# ---------------------------------------------------------------
arcpy.AddMessage("---> Getting Current Itinerary and Route Data")
arcpy.TableSelect_analysis(itinerary, orig_itinerary_dbf, "\"OBJECTID\" >= 1")
arcpy.SelectLayerByAttribute_management(rail_routes, "CLEAR_SELECTION", "")
arcpy.FeatureClassToFeatureClass_conversion(rail_routes, e, "temp_route.shp", "", "", "")
# ---------------------------------------------------------------
# Write Current Arc & Route Geometry to Files
# ---------------------------------------------------------------
arcpy.SelectLayerByAttribute_management(railnet_arc, "CLEAR_SELECTION", "")
outFile = open(outFl, "w")
f = 1 # row id number
for row in arcpy.SearchCursor(railnet_arc): # loop through rows (features)
for part in row.Shape: # loop through feature parts
pnt = part.next()
while pnt: # loop through vertices
outFile.write(str(row.getValue("Anode")) + ";" + str(row.getValue("Bnode")) + ";" + str(row.getValue("Directions")) + ";" + str(row.getValue("Miles")) + ";" + row.getValue("Modes1") + ";" + row.getValue("Modes2") + ";" + str(f) + ";" + str(pnt.X) + ";" + str(pnt.Y) + "\n")
pnt = part.next()
if not pnt:
pnt = part.next()
f += 1
f -= 1
arcpy.AddMessage("---> Geometry Written for " + str(f) + " Arcs")
outFile.close()
if param1 != '':
outFile = open(outRtFl, "w")
f = 1 # row id number
for row in arcpy.SearchCursor(rail_routes): # loop through rows (features)
for part in row.Shape: # loop through feature parts
pnt = part.next()
while pnt: # loop through vertices
outFile.write(str(f) + ";" + str(row.getValue("TR_LINE")) + ";" + str(pnt.X) + ";" + str(pnt.Y) + ";" + str(pnt.M) +"\n")
pnt = part.next()
if not pnt:
pnt = part.next()
f += 1
f -= 1
arcpy.AddMessage("---> Geometry Written for " + str(f) + " Future Routes")
outFile.close()
# ---------------------------------------------------------------
# Process Data to Create New Route Coding and Update Geometry
# ---------------------------------------------------------------
arcpy.AddMessage("---> Creating Route Coding")
subprocess.call(cmd)
if os.path.exists(sas_list_file):
arcpy.AddMessage("---> SAS Processing Error!! Review the List File: " + sas_list_file)
arcpy.AddMessage("---> If there is an Errorlevel Message, Review the Log File: " + sas_log_file)
arcpy.AddMessage("-------------------------------------------------------------------")
sys.exit([1])
# ---------------------------------------------------------------
# Rebuild All Routes with Updated Geometry and Coding
# ---------------------------------------------------------------
if os.path.exists(rte_updt):
arcpy.AddMessage("---> Writing New Route Geometry")
arcpy.DeleteRows_management(rail_routes)
cur = arcpy.InsertCursor(rail_routes)
lineArray = arcpy.Array()
pnt = arcpy.Point()
ID = -1
for line in fileinput.input(infl): # open geometry file
pnt.ID, pnt.X, pnt.Y, pnt.M = str.split(line,";") # assign point properties
if ID == -1:
ID = pnt.ID
if ID != pnt.ID:
feat = cur.newRow() # create a new feature if ID ne pnt.id
feat.shape = lineArray # set feature geometry to the array of points
cur.insertRow(feat) # insert the feature
lineArray.removeAll()
lineArray.add(pnt)
ID = pnt.ID
feat = cur.newRow() # add last feature
feat.shape = lineArray
cur.insertRow(feat)
lineArray.removeAll()
fileinput.close()
del cur # delete cursor to remove data locks
blankcur = arcpy.UpdateCursor(rail_routes)
datacur = arcpy.SearchCursor(rte_updt)
arcpy.AddMessage("---> Updating Rail Line Data")
for d_row in datacur:
#arcpy.AddMessage("'{}', '{}', '{}'".format(d_row.getValue("tipid1"), d_row.getValue("comp1"), d_row.getValue("rspid1")))
b_row = blankcur.next()
b_row.TR_LINE = d_row.getValue("line1")
b_row.DESCRIPTION = d_row.getValue("desc1")
b_row.MODE = d_row.getValue("mode1")
b_row.VEH_TYPE = d_row.getValue("type1")
b_row.HEADWAY = d_row.getValue("hdwy1")
b_row.SPEED = d_row.getValue("speed1")
if flag == "1": # update variables unique to future rail coding
b_row.TOD = str.strip(str(d_row.getValue("tod1")))
b_row.SCENARIO = str.strip(str(d_row.getValue("scen1")))
b_row.ACTION = d_row.getValue("action1")
b_row.NOTES = d_row.getValue("notes1")
b_row.TIP_ID = d_row.getValue("tipid1")
if d_row.getValue("comp1") == 0:
b_row.COMPLETION_YEAR = None
else:
b_row.COMPLETION_YEAR = d_row.getValue("comp1")
b_row.RSP_ID = d_row.getValue("rspid1")
elif flag == "2": # update variables unique to all_runs
b_row.FEEDLINE = d_row.getValue("fdline")
b_row.ROUTE_ID = d_row.getValue("r_id")
b_row.LONGNAME = d_row.getValue("rln")
b_row.DIRECTION = d_row.getValue("dir")
b_row.TERMINAL = d_row.getValue("term")
b_row.START = d_row.getValue("start")
b_row.STRTHOUR = d_row.getValue("strthour")
b_row.AM_SHARE = d_row.getValue("ampct")
b_row.CT_VEH = d_row.getValue("vehicle")
blankcur.updateRow(b_row)
del blankcur, datacur, b_row, d_row # delete cursor to remove data locks
arcpy.FeatureClassToFeatureClass_conversion(rail_routes, railnet, "test")
arcpy.Delete_management(railrt)
arcpy.FeatureClassToFeatureClass_conversion(test, railnet, rail_routes)
arcpy.Delete_management(test)
else:
arcpy.AddMessage("---> ERROR: Route Coding Not Updated!!")
sys.exit[1]
# ---------------------------------------------------------------
# Update Itinerary Table
# ---------------------------------------------------------------
if os.path.exists(new_segments_dbf):
arcpy.AddMessage("---> Updating Rail Itinerary Coding")
arcpy.DeleteRows_management(itinerary)
arcpy.Compact_management(mrn_gdb) # reset OBJECTID to start at 1
arcpy.Append_management(new_segments_dbf, itinerary, "NO_TEST")
arcpy.CalculateField_management(itinerary, "LAYOVER", "!LAYOVER!.strip()", "PYTHON")
else:
arcpy.AddMessage("---> ERROR: Itinerary Coding Not Updated!!")
sys.exit[1]
# ---------------------------------------------------------------
# Cleanup files if needed
# ---------------------------------------------------------------
if os.path.exists(temp_route_shp):
arcpy.Delete_management(temp_route_shp, "ShapeFile")
if os.path.exists(new_segments_dbf):
arcpy.Delete_management(new_segments_dbf, "DbaseTable")
if os.path.exists(rte_updt):
arcpy.Delete_management(rte_updt, "DbaseTable")
if os.path.exists(test):
arcpy.Delete_management(test)
if os.path.exists(outFl):
os.remove(outFl)
if os.path.exists(infl):
os.remove(infl)
if os.path.exists(outRtFl):
os.remove(outRtFl)