Monday, 11 May 2015

arcgis desktop - Merging many to one data rows in arcpy




The dataset 'left_right_merge' at the bottom of my screen shot shows the dataset that I'm currently working with. The NODE values indicate a single point feature, but is duplicated with many attributes. I want to collapse the dataset so that it looks like the 'test_data_nodes' at the top of the screen shot. How would I do this in python?


enter image description here



Answer



There might be a more elegant way to do this, but here is a quick & dirty script base on my understanding of your problem:


import arcpy

arcpy.env.workspace = r'C:\Users\echiasson\Documents\ArcGIS\Default.gdb' # Replace correct geodatabase path
arcpy.env.overwriteOutput = True


# Make a copy of the original feature class
arcpy.Copy_management('left_right_merge', 'left_right_merge_collapse')

# Collapse the output feature class
nodeList = []
with arcpy.da.UpdateCursor('left_right_merge_collapse', ['NODE']) as cursor:
for row in cursor:
if row[0] in nodeList:
cursor.deleteRow()
else:

nodeList.append(row[0])

for node in nodeList:
# Compile the values in the original feature class
attList = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
with arcpy.da.SearchCursor('left_right_merge', ['NB_L','NB_R', 'SB_L', 'SB_R', 'NWB_L', 'NWB_R', 'NEB_L', 'NEB_R', 'SWB_L', 'SWB_R', 'SEB_L', 'SEB_R', 'EB_L', 'EB_R', 'WB_L', 'WB_R'], "NODE = " + str(node)) as cursor:
for row in cursor:
for i in range(0,15):
if row[i] != 0:
attList[i] = row[i]


# Update the values in the collapse feature class
with arcpy.da.UpdateCursor('left_right_merge_collapse', ['NB_L','NB_R', 'SB_L', 'SB_R', 'NWB_L', 'NWB_R', 'NEB_L', 'NEB_R', 'SWB_L', 'SWB_R', 'SEB_L', 'SEB_R', 'EB_L', 'EB_R', 'WB_L', 'WB_R'], "NODE = " + str(node)) as cursor:
for row in cursor:
for i in range(0,15):
row[i] = attList[i]
cursor.updateRow(row)

Please leave a comment if I am missing something.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...