I have a feature class layer that comprise of line elements. For my purpose, I needed the start and end point coordinates of each line, so I added new columns with add field and use calculate geometry in ArcGIS Pro to assign the start/end coordinates to each line (below table show the attributes).
What I want to do is that to write a python code that compare lines’ first and end coordinates. Then return a list
of ID
s for those that their start/end points are equal, and append all those as nested lists in a main list. For instance, if object ID
s 2, 5 and 6 have same start and end point, and if object ID
s 8, 12, 15 an 22 have the same condition, the code return a nested list like this: main_list = [[2, 5, 6], [8, 12, 15, 22]]
. Finally, I want to edit the var
column with this outcome. I want var
to be equal to length of each corresponding nested list minus one. For instance for those example above, var
values for object ID
s 2, 5 and 6 will be 2 (length of nested list minus one), and for ID
s 8, 12, 15 and 22 will be 3.
ID | x_start | y_start | x_end | y_end | var |
---|---|---|---|---|---|
1 | |||||
2 | |||||
. | |||||
. |
fields = ['ID', 'x_start', 'y_start', 'x_end', 'y_end']
MainList = [[]]
NestedList = []
ID = [row[0] for row in arcpy.da.SearchCursor(Redundancy_one, ('ID'))]
x_start = [row[0] for row in arcpy.da.SearchCursor(Redundancy_one, ('x_start'))]
y_start = [row[0] for row in arcpy.da.SearchCursor(Redundancy_one, ('y_start'))]
x_end = [row[0] for row in arcpy.da.SearchCursor(Redundancy_one, ('x_end'))]
y_end = [row[0] for row in arcpy.da.SearchCursor(Redundancy_one, ('y_end'))]
i = 0
while i<len(ID):
with arcpy.da.SearchCursor[Redundancy_one, fields] as cursor:
for row in cursor:
if x_start[0] == row[1] and y_start[2] == row[2] and x_end[2] == row[3] and y_end == row[4] and ID[0] =! row[0]:
NestedList.append(ID[0])
NestedList.append (row[0])
MainList.append(NestedList)
NestedList=[]
i +=1