Hello everyone,
I'm building a script to calculate offset values for linearly referenced data based on how many times distance values occur in a dataset. This is the script I've come up with and it works how I'd like it to, though once applied to the 6,000+ piece dataset it takes hours to complete.
Any thoughts on how I might be able to speed it up?
#pt table
pt_tbl = 'ArcPRO_MasterQuery_EXT_pt'
with arcpy.da.SearchCursor(pt_tbl, "NUM_SEC") as cursor2:
traillist = []
tns = set(cursor2)
for thing in tns:
for d in thing:
traillist.append(d)
traillistsortpt = sorted(traillist)
print("Calculating Point Table Offsets...")
traillistsortpt = ["11-1", "11-2"]
i = 0
for num in traillistsortpt:
arcpy.management.SelectLayerByAttribute(pt_tbl, "NEW_SELECTION", "NUM_SEC = '%s'" %(num))
oor = {}
oo = {}
with arcpy.da.UpdateCursor(pt_tbl, ["OID@", "DISTANCE", "DESCRIPTION_1", "DESCRIPTION_2", "DESCRIPTION_3", "Offset"]) as cursor1:
for row in cursor1:
distance_value = row[1]
oo[distance_value] = oo.get(distance_value, 0) +1
if "RHS" in str(row[2]) or "RHS" in str(row[3]) or "RHS" in str(row[4]):
oor[distance_value] = oor.get(distance_value, 0) + 1
orh = (oo[distance_value] - 1) * -1
row[5] = orh
elif row[1] in oor:
o = (oo[distance_value] - (oor[distance_value]+1))
row[5] = o
else:
o = (oo[distance_value] - 1)
row[5] = o
cursor1.updateRow(row)
i+=1
print(i, " of ", len(traillistsortpt))
arcpy.management.SelectLayerByAttribute(pt_tbl, "CLEAR_SELECTION")
print("Point Table Offsets Calculated")