Creating isolated line ids for lines separated by point feature class?

161
0
11-29-2021 09:23 AM
RPGIS
by
Regular Contributor

Hi,

I am trying to populate a field for lines that are separated by another point feature class. However, some lines shares common vertex coordinates but not with the point feature class. So the number of shared vertices can range anywhere from 2 - 5 lines. I have a script that generates a unique ID based on a random number, but what I am struggling with is how to go about it. I am also trying to keep it as light as possible without needing to create another feature class.

Here is what I have thus far. (also, I keep getting the error message 'dictionary changed size during iteration' error message)

import arcpy
import random
import time

start_time =  'Process start time: {}'.format(time.strftime('%I:%M:%S'))
print (start_time)
#arcpy.env.overwriteOutput = True
# Input parameters
#InputFolder = r'*'
spointFc = r'*'
LineFc = r'*'

# Describe input features
##desc = arcpy.Describe(LineFc)
##Geometry = desc.shapeType

# Create separate folder and database
#- Create folder if folder does not
#- exist.
##Folder_Name = 'Storage Folder'        
##Folder = os.path.join(Folder_Name, InputFolder)
##
##Created_Folder = ''
##for root, directory, folders in os.walk(InputFolder):
##    for folder in folders:
##        if folder == Folder_Name:
##            Created_Folder = os.path.join(folder, root)
##        else:
##            Created_Folder = os.mkdir(Folder_Location)

#- Create geodatabase if geodatabase does
#- not exist.
##Created_GDB_Name = 'DataHolding.gdb'
##Created_GDB = os.path.join(Created_GDB_Name, Created_Folder)
##
##Created_Geodatabase = ''
##for root, directory, folders in os.walk(Created_Folder):
##    for folder in folders:
##        if folder == Created_GDB_Name:
##            Created_Geodatabase = os.path.join(folder, root)
##        else:
##            Created_Geodatabase = arcpy.CreateFileGDB_management(Created_Folder, Created_GDB_Name)

# Set newly created file geodatabase
# as the new workspace.
##arcpy.env.workspace = Created_Geodatabase

# 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@XY']
AddLineShape_Field = ['SHAPE@']

spointFc_Fields = SelectspointFc_Fields + AddspointShape_Field
LineFc_Fields = SelectLineFc_Fields + AddLineShape_Field

# Define the update and search cursors for
# input features.
updatepoints = 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.
#LineID = {}
IsolatedIDs = {}
#SharedCounts = {}
UpdateIsolatedIDs = {}

valve_points = []
NoneValvePoints = []

startloop_pointFC = 'Starting point feature class loop at {}'.format(time.strftime('%I:%M:%S'))
print (startloop_pointFC)

for point in searchpoints:
    if point[-1] not in  valve_points:
        Vpoint = [x for x in point[-1]]
        #print (Vpoint)
        valve_points.append(Vpoint)

stoploop_pointFC ='Finished point feature class loop at {}'.format(time.strftime('%I:%M:%S'))
print (stoploop_pointFC)

startloop_lineFC = 'Starting line feature class loop at {}'.format(time.strftime('%I:%M:%S'))
print (startloop_lineFC)


for line in searchLines:
    i = 0
    
    IsolationID = random.randint(10000000, 99999999)
    
    ID = line[0]
    
    startpnt = line[2].firstPoint
    start = [startpnt.X, startpnt.Y]

    endpnt = line[2].lastPoint
    end = [endpnt.X, endpnt.Y]

    start_end = [start, end]

    UID = {ID: start_end}
    
    if start in valve_points and end in valve_points:
        UpdateIsolatedIDs[ID] = IsolationID
        #print (IsolationID, ID)
        
    elif start in valve_points and end not in valve_points:
        if end not in NoneValvePoints:
            NoneValvePoints.append(end)
                
    elif end in valve_points and start not in valve_points:
        if start not in NoneValvePoints:
            NoneValvePoints.append(start)

    else:
        if start in NoneValvePoints and end in NoneValvePoints:
            if IsolatedIDs:
                for A, B in IsolatedIDs.items():
                    if start not in B and end not in B:
                        IsolatedIDs[IsolationID] = start_end
            else:
                IsolatedIDs[IsolationID] = start_end
                print (IsolationID, start_end)
        elif start in NoneValvePoints and end not in NoneValvePoints:
            NoneValvePoints.append(end)
            if IsolatedIDs:
                for A, B in IsolatedIDs.items():
                    if start in B and end not in B:
                        update = B + [end]
                        IsolatedIDs[A] = update
                    elif start not in B and end in B:
                        update = B + [start]
                        IsolatedIDs[A] = update
            else:
                IsolatedIDs[IsolationID] = start_end
                print (IsolationID, start_end)
        elif end in NoneValvePoints and start not in NoneValvePoints:
            NoneValvePoints.append(start)
            if IsolatedIDs:
                for A, B in IsolatedIDs.items():
                    if start in B and end not in B:
                        update = B + [end]
                        IsolatedIDs[A] = update
                    elif start not in B and end in B:
                        update = B + [start]
                        IsolatedIDs[A] = update
            else:
                IsolatedIDs[IsolationID] = start_end
                print (IsolationID, start_end)
            
        else:
            NoneValvePoints + start_end
            if IsolatedIDs:
                for A, B in IsolatedIDs.items():
                    if start not in B and end not in B:
                            IsolatedIDs[IsolationID] = start_end
            else:
                IsolatedIDs[IsolationID] = start_end
                print (IsolationID, start_end)

stoploop_lineFC = 'Finished line feature class loop at {}'.format(time.strftime('%I:%M:%S'))
print (stoploop_lineFC)

startloop_dict = 'Starting dictionary loop at {}'.format(time.strftime('%I:%M:%S'))
print (startloop_dict)

for A, B in IsolatedIDs.items():
    print (A, B)
    
finishloop_dict = 'Finished dictionary loop at {}'.format(time.strftime('%I:%M:%S'))
print (finishloop_dict)
    
finish_time = 'Process finish time: {}'.format(time.strftime('%I:%M:%S'))
print (finish_time)

I have also tried creating dictionaries within a single dictionary using the isolatedID as the key with another dictionary, created using the line ID and start and end vertices, as the value.

Any help on this would be greatly appreciated. I will keep on trying but I have been struggling for quite some time with this.

0 Kudos
0 Replies