I have built a model and within the model I perform a merge. that creates a table. I have a script that creates one record from that table. I want to make a route event layer from this table. The problem I have is the script is the last thing to run so the make route event layer fails. I thought setting the precondition on the created table would make the script run before the table is used by the Make Route Event tool. Is there a way to force the script to run on the table before the Make Route Event Layer? Here is the code in my script.
table = r"C:\mdtapps\interactive\test.gdb\combined"
ridList = []
with arcpy.da.SearchCursor(table, ["RID"]) as cursor:
for row in cursor:
ridList.append(row[0])
del cursor
ridList = set(ridList)
for RID in ridList:
toMeasList = []
with arcpy.da.SearchCursor(table, ["RID", "END"], "RID = '" + RID + "'") as cursor:
for row in cursor:
toMeasList.append(row[1])
del cursor
toMeasList.sort()
with arcpy.da.UpdateCursor(table, ["END"], "RID = '" + RID + "'") as cursor:
for row in cursor:
row[0] = toMeasList[-1]
cursor.updateRow(row)
del cursor
toMeasList.sort()
with arcpy.da.UpdateCursor(table, ["OBJECTID"]) as cursor:
for row in cursor:
if row[0] == 2:
cursor.deleteRow()
del cursor
This is what my model looks like.
Solved! Go to Solution.
Instead of precondition, I suggest setting up script tool input and output parameters like this
table = arcpy.GetParameterAsText(0) # parameter 0: input table (Table View)
arcpy.SetParameterAsText(1, True) # parameter 1: result (Boolean)
Setting script tool parameters—Help | ArcGIS Desktop
Then connect the combine table (data element in your model) to the script tool, and connect the script tool output to the Make Route Event Layer tool as a precondition. That should do it.
BTW, you need to format Python code for it to be readable here:
Another approach:
For a short bit if python snippet like this you can also use the Calculate Value tool and write a python function for your code, this can save you from the hassle of a separate script tool, passing parameters as text, and all that jazz.
# Calculate Value expression
proc(r"%combine%") # set combine as precondition to this CV tool
# Calculate Value code block
def proc(combine):
ridList = []
with arcpy.da.SearchCursor(table, ["RID"]) as cursor:
for row in cursor:
ridList.append(row[0])
del cursor
ridList = set(ridList)
for RID in ridList:
toMeasList = []
with arcpy.da.SearchCursor(table, ["RID", "END"], "RID = '{}'".format(RID)) as cursor:
for row in cursor:
toMeasList.append(row[1])
del cursor
toMeasList.sort()
with arcpy.da.UpdateCursor(table, ["END"], "RID = '{}'".format(RID)) as cursor:
for row in cursor:
row[0] = toMeasList[-1]
cursor.updateRow(row)
del cursor
toMeasList.sort()
with arcpy.da.UpdateCursor(table, ["OBJECTID"]) as cursor:
for row in cursor:
if row[0] == 2:
cursor.deleteRow()
del cursor
return True # data type: Boolean (use as precondition)
Instead of precondition, I suggest setting up script tool input and output parameters like this
table = arcpy.GetParameterAsText(0) # parameter 0: input table (Table View)
arcpy.SetParameterAsText(1, True) # parameter 1: result (Boolean)
Setting script tool parameters—Help | ArcGIS Desktop
Then connect the combine table (data element in your model) to the script tool, and connect the script tool output to the Make Route Event Layer tool as a precondition. That should do it.
BTW, you need to format Python code for it to be readable here:
Another approach:
For a short bit if python snippet like this you can also use the Calculate Value tool and write a python function for your code, this can save you from the hassle of a separate script tool, passing parameters as text, and all that jazz.
# Calculate Value expression
proc(r"%combine%") # set combine as precondition to this CV tool
# Calculate Value code block
def proc(combine):
ridList = []
with arcpy.da.SearchCursor(table, ["RID"]) as cursor:
for row in cursor:
ridList.append(row[0])
del cursor
ridList = set(ridList)
for RID in ridList:
toMeasList = []
with arcpy.da.SearchCursor(table, ["RID", "END"], "RID = '{}'".format(RID)) as cursor:
for row in cursor:
toMeasList.append(row[1])
del cursor
toMeasList.sort()
with arcpy.da.UpdateCursor(table, ["END"], "RID = '{}'".format(RID)) as cursor:
for row in cursor:
row[0] = toMeasList[-1]
cursor.updateRow(row)
del cursor
toMeasList.sort()
with arcpy.da.UpdateCursor(table, ["OBJECTID"]) as cursor:
for row in cursor:
if row[0] == 2:
cursor.deleteRow()
del cursor
return True # data type: Boolean (use as precondition)