Hi,
I have a script, which works up but does not output the result that I am looking for, where some shared values don't get assigned the unique ID. Some portion of lines are getting separate IDs despite having a shared common point. Here is the script that I have working thus far:
import arcpy
import time
start_time = 'Process start time: {}'.format(time.strftime('%I:%M:%S'))
print (start_time)
#arcpy.env.overwriteOutput = True
#spointFc = r'*'
#LineFc = r'*'
spointFc = r'*'
LineFc = r'*'
workspace = r'*'
edit = arcpy.da.Editor(workspace)
# Gather fields for searching through
# and updating each feature class.
SelectspointFc_Fields = [field.name for field in arcpy.ListFields(spointFc) if field.name in ['OBJECTID', 'IsolationIDA', 'IsolationIDB']]
SelectLineFc_Fields = [field.name for field in arcpy.ListFields(LineFc) if field.name in ['OBJECTID', 'IsolationID']]
AddspointShape_Field = ['SHAPE@X'] # Set as last/or end of the list of fields
AddLineShape_Field = ['SHAPE@'] # Set as last/or end of the list of fields
spointFc_Fields = SelectspointFc_Fields + AddspointShape_Field
LineFc_Fields = SelectLineFc_Fields + AddLineShape_Field
# Define the update and search cursors for
# input features.
updatePoint = arcpy.da.UpdateCursor(spointFc, spointFc_Fields)
updateLines = arcpy.da.UpdateCursor(LineFc, LineFc_Fields)
searchpoints = arcpy.da.SearchCursor(spointFc, spointFc_Fields)
searchLines = arcpy.da.SearchCursor(LineFc, LineFc_Fields)
# Loop through the line feature class and
# update based on whether the IsolationID
# is null.
i = 10000000
Assigned_IDs = {}
assigned_uniqueIDs = []
LineValues = {}
IDValues = {}
shared_IDs = {}
UpdateIsolatedIDs_Lines = {}
valve_points = []
Non_valvePoints = []
ID_list = []
startloop = 'Starting point feature class at {}'.format(time.strftime('%I:%M:%S'))
print (startloop)
# Loop through the point feature class
# to get the Object ID and the
# floating X coordinate of the
# point feature class.
with searchpoints as cursor:
for point in cursor:
Vpoint = float(point[-1])
#print (int(Vpoint))
if Vpoint not in valve_points:
valve_points.append(Vpoint)
del cursor
stoploop ='Finished point feature class at {}\n'.format(time.strftime('%I:%M:%S'))
print (stoploop)
startloop = 'Starting line feature class at {}'.format(time.strftime('%I:%M:%S'))
print (startloop)
with searchLines as cursor:
for row in cursor:
# Loop through the line feature class to
# get the Object ID, starting and ending line
# coordinates. Extract only the floating X
# coordinates of the line features.
ID = row[0]
startpnt = row[2].firstPoint
start = float(startpnt.X)
endpnt = row[2].lastPoint
end = float(endpnt.X)
# Check if the ID is in the specified dictionary, then check
# if both the start and end of the line are in the specified
# list and assign as key and values to specified dictionary.
if ID not in UpdateIsolatedIDs_Lines:
if start in valve_points and end in valve_points:
UpdateIsolatedIDs_Lines[ID] = i
i += 1
# Check if the start of the line is in the specified list
# and assign key and values to specified dictionary.
elif start not in valve_points and end in valve_points:
IDValues[ID] = [start]
ID_list.append(ID)
# Check if the start of the line is in the specified dictionary
# and assign starting coordinate as key with ID as value.
# Update the values if the start of the line already exists
# as the key in dictionary.
if start not in LineValues:
LineValues[start] = [ID]
else:
update = LineValues[start] + [ID]
LineValues[start] = update
#print (ID, update)
# Check if the end of the line is in the specified list and set
# ID as key in dictionary with ending coordinate as the value.
elif start in valve_points and end not in valve_points:
IDValues[ID] = [end]
ID_list.append(ID)
# Check if the end of the line is not in the specified dictionary and
# assign ending coordinate as key with ID as value to specified dictionary.
if end not in LineValues:
LineValues[end] = [ID]
else:
# Update the values if the end coordinate of the line already
# exists in dictionary and update the same key in the dictionary.
#print (ID, update)
update = LineValues[end] + [ID]
LineValues[end] = update
else:
# Check if both the start and end of the line are in the specified dictionary
# and assign ID as key in with list of starting and ending coordinate as values.
# Then assign the starting and ending coordiates as keys with the line ID as
# the values.
if start not in LineValues and end not in LineValues:
ID_list.append(ID)
IDValues[ID] = [start, end]
LineValues[start] = [ID]
LineValues[end] = [ID]
# Check if the start of the line is in the specified dictionary and assign
# the ID as key with a list of starting and ending coordinate as value.
# Then assign the starting and ending coordinates as keys with the line ID as
# the values. Update the values if the end coordinate already exists in
# dictionary as the key in the dictionary.
elif start not in LineValues and end in LineValues:
ID_list.append(ID)
IDValues[ID] = [start, end]
LineValues[start] = [ID]
update = LineValues[end] + [ID]
LineValues[end] = update
#print (end, update)
# Check if the start of the line is in the specified dictionary and assign
# the ID as key with a list of starting and ending coordinate as value.
# Then assign the starting and ending coordinates as keys with the line ID as
# the values. Update the values if the start coordinate already exists in
# dictionary as the key in the dictionary.
elif start in LineValues and end not in LineValues:
ID_list.append(ID)
IDValues[ID] = [start, end]
LineValues[end] = [ID]
update = LineValues[start] + [ID]
LineValues[start] = update
#print (end, update)
else:
# Assign the starting and ending coordiates as keys with the line ID as
# the values. # Update the values if the start coordinate of the line
# already exists in the dictionary and the same key in the dictionary.
ID_list.append(ID)
IDValues[ID] = [start, end]
update = LineValues[start] + [ID]
LineValues[start] = update
update = LineValues[end] + [ID]
LineValues[end] = update
del cursor
stoploop = 'Finished line feature class at {}\n'.format(time.strftime('%I:%M:%S'))
print (stoploop)
startloop = 'Starting Compiling Main Update Dictionary At {}'.format(time.strftime('%I:%M:%S'))
print (startloop)
for ID in ID_list:
#print ('{} is the Object ID of the line'.format(ID))
unassigned_IDs = []
# Check if the ID is not in UpdateIsolatedIDs_Lines dictionary. Then check if ID is a key in the IDValues dictionary.
# If the ID is a key in dictionary, loop through all values in the dictionary. Check if the values of that dictionary
# are keys in LineValues dictionary. Loop through those values and append all values that are not equal to the ID to the
# the unassigned_values list.
if ID not in UpdateIsolatedIDs_Lines:
if IDValues[ID]:
print ('{} is key in {} dictionary with floating {} X coordinates as values'.format(ID, 'IDValues',IDValues[ID]))
for end in IDValues[ID]:
if LineValues[end]:
print ('The floating {} is the key in {} dictionary with {} line IDs as values'.format(end, 'LineValues', LineValues[end]))
for x in LineValues[end]:
if x not in UpdateIsolatedIDs_Lines:
unassigned_IDs.append(x)
sorted_list = sorted(unassigned_IDs)
#print (ID, sorted_list)
first = sorted_list[0]
if len(sorted_list) > 1:
if first == ID and first not in UpdateIsolatedIDs_Lines:
UpdateIsolatedIDs_Lines[first] = i
print ('{} has been assigned {} as value.'.format(first, UpdateIsolatedIDs_Lines[first]))
i += 1
for x in sorted_list[1:]:
UpdateIsolatedIDs_Lines[x] = UpdateIsolatedIDs_Lines[first]
print ('{} has been assigned {} as value.'.format(x, UpdateIsolatedIDs_Lines[first]))
elif first in UpdateIsolatedIDs_Lines:
UpdateIsolatedIDs_Lines[ID] = UpdateIsolatedIDs_Lines[first]
for x in sorted_list[1:]:
UpdateIsolatedIDs_Lines[x] = UpdateIsolatedIDs_Lines[first]
print ('{} has been assigned {} as value.'.format(x, UpdateIsolatedIDs_Lines[first]))
else:
UpdateIsolatedIDs_Lines[ID] = i
print ('{} has been assigned {} as value.'.format(first, UpdateIsolatedIDs_Lines[first]))
i += 1
else:
print ('{} is already assigned {} as isolation ID.'.format(ID, UpdateIsolatedIDs_Lines[ID]))
if IDValues[ID]:
print ('{} is key in {} dictionary with floating {} X coordinates as values'.format(ID, 'IDValues',IDValues[ID]))
for end in IDValues[ID]:
if LineValues[end]:
print ('The floating {} is the key in {} dictionary with {} line IDs as values'.format(end, 'LineValues', LineValues[end]))
for x in LineValues[end]:
UpdateIsolatedIDs_Lines[x] = UpdateIsolatedIDs_Lines[ID]
print ('{} has been assigned {} as value.'.format(x, UpdateIsolatedIDs_Lines[ID]))
#print ('\n\n')
finishloop = 'Finished Compiling Main Update Dictionary At {}\n'.format(time.strftime('%I:%M:%S'))
print (finishloop)
AssignedID_checklist = {}
for OID, AssignedID in UpdateIsolatedIDs_Lines.items():
if AssignedID not in AssignedID_checklist:
AssignedID_checklist[AssignedID] = [OID]
else:
AssignedID_checklist[AssignedID] += [OID]
startloop_updating = 'Starting ID Line Updates at {}'.format(time.strftime('%I:%M:%S'))
print (startloop_updating)
edit.startEditing(False, True)
edit.startOperation()
with updateLines as cursor:
for row in cursor:
if row[0] in UpdateIsolatedIDs_Lines:
IsolationID = UpdateIsolatedIDs_Lines[row[0]]
print (row)
print (IsolationID)
if IsolationID:
row[0] = row[0]
row[1] = IsolationID
row[2] = row[2]
updateLines.updateRow(row)
print (row)
print ('\n')
del cursor
edit.stopOperation()
edit.stopEditing(True)
finishloop_updating = 'Finished ID Line Updates at {}\n'.format(time.strftime('%I:%M:%S'))
print (finishloop_updating)
finish_time = 'Process finish time: {}'.format(time.strftime('%I:%M:%S'))
print (finish_time)