Hi,
I have been working on this python script for a good while. For the most part, I am able to get it to work on a smaller set of data. However, I have noticed that it takes an exceptionally long time to execute for the entire database. I am not sure if I wrote this in an efficient manner, but I am quite perplexed as to how long it takes. I have tried splitting up the dictionaries to simplify matters and to have smaller sections of code execute. I am hoping this will speed up the process a bit but at this point I have run out of options.
import arcpy
import time
spointFc = r'*'
LineFc = r'*'
workspace = r'*'
edit = arcpy.da.Editor(workspace)
startloop_pointFC = 'Starting point feature class at {}'.format(time.strftime('%I:%M:%S'))
print (startloop_pointFC)
with searchpoints as cursor:
for point in searchpoints:
#Vpoint = [x for x in point[-1]]
Vpoint = int(point[-1])
#print (int(Vpoint))
valve_points.append(Vpoint)
del cursor
stoploop_pointFC ='Finished point feature class at {}\n'.format(time.strftime('%I:%M:%S'))
print (stoploop_pointFC)
startloop_lineFC = 'Starting line feature class at {}'.format(time.strftime('%I:%M:%S'))
print (startloop_lineFC)
edit.startEditing(False, True)
edit.startOperation()
with updateLines as cursor:
for row in cursor:
ID = row[0]
startpnt = row[2].firstPoint
#start = [startpnt.X, startpnt.Y]
start = int(startpnt.X)
endpnt = row[2].lastPoint
#end = [endpnt.X, endpnt.Y]
end = int(endpnt.X)
start_end = [start, end]
# Check if start or end coordinates are shared
# with any of the point coordinates (X)
A = 0
B = 0
for valve in valve_points:
if start/valve == 1:
A += 1
elif end/valve == 1:
B += 1
#if start in valve_points and end in valve_points:
if A == 1 and B == 1:
#print (row)
row[0] = row[0]
row[1] = i
row[2] = row[2]
updateLines.updateRow(row)
#print (row)
i += 1
#elif start in valve_points and end not in valve_points:
if A == 1 and B == 0:
#if end not in NonValvePoints:
NonValvePoints.append(end)
LinesWithValve[ID] = end
#elif end in valve_points and start not in valve_points:
if A == 0 and B == 1:
#if start not in NonValvePoints:
NonValvePoints.append(start)
LinesWithValve[ID] = start
else:
# If neither start or end coordinates are
# in the list of non-point coordinates
C = 0
D = 0
for Nvalve in NonValvePoints:
if start/Nvalve == 1:
C += 1
elif end/Nvalve == 1:
D += 1
# Check if C and D change in vales.
# If C and/or D change values, add ID as
# key with line ends as assigned values
if C == 1 and D == 1:
LinesWithoutValve[ID] = start_end
elif C == 1 and D == 0:
NonValvePoints.append(end)
LinesWithoutValve[ID] = start_end
elif C == 0 and D == 1:
NonValvePoints.append(start)
LinesWithoutValve[ID] = start_end
else:
NonValvePoints + start_end
LinesWithoutValve[ID] = start_end
del cursor
edit.stopOperation()
edit.stopEditing(True)
stoploop_lineFC = 'Finished line feature class at {}\n'.format(time.strftime('%I:%M:%S'))
print (stoploop_lineFC)
startloop_nonvalves = 'Starting dictionary at {}'.format(time.strftime('%I:%M:%S'))
print (startloop_nonvalves)
# Run through dictionary and run through same dictionary
# to find any coinciding values for lines without valves.
for init_ID, End in LinesWithoutValve.items():
CoincidingIDs = []
for other_ID, OtherEnd in LinesWithoutValve.items():
if init_ID != other_ID:
isMatch = [e for e in End if e in OtherEnd]
if isMatch:
CoincidingIDs.append(other_ID)
if CoincidingIDs:
SharedIDs[init_ID] = CoincidingIDs
#print (init_ID, CoincidingIDs)
# Run through dictionary and run through other dictionary
# to find any coinciding values for lines with valves.
for init_ID, End in LinesWithoutValve.items():
CoincidingIDs = []
for lv_ID, line_valveEnd in LinesWithValve.items():
for E in End:
if E == line_valveEnd:
CoincidingIDs.append(lv_ID)
if init_ID in SharedIDs:
shared_updates = SharedIDs[init_ID] + CoincidingIDs
SharedIDs[init_ID] = shared_updates
finishloop_dict = 'Finished dictionary at {}\n'.format(time.strftime('%I:%M:%S'))
print (finishloop_dict) If anyone can provide any advice or somehow show a more efficient way to construct this; I would be greatly appreciated.
Thanks