Select and Concatenate using Model Builder

829
2
03-20-2020 07:41 AM
SarahHartholt
Occasional Contributor III

I would like to create a model (or other work flow) in ArcGIS Pro that iterates through a feature class, selects street names that are the same and concatenates gid cell numbers (grid cell that they fall into on a map) to the end.

for example, in the attachment there are 3 Bakker Rd features with unique Cell_ID's (A4, B4, B3). I want to create a single attribute for this street indicating the street name and grid cells that it falls into so that I can eventually create a clean legend for my map: 

Baker Rd - A4, B3, B4

0 Kudos
2 Replies
DavidPike
MVP Frequent Contributor
import arcpy

input_table = r'C:\'

#create a list of unique names
#a set has no duplicates
abbr_name_list = []
with arcpy.da.SearchCursor(input_table, "ABBR_NAME") as cursor:
    for row in cursor:
        abbr_name_list.append(row[0])
abbr_name_set = set(abbr_name_list)


#make a list of ABBR_NAMEs in the set along with the cell IDs
abbr_name_cell_id_list = []
for abbr_name in abbr_name_set:
    cell_id_list = []
    with arcpy.da.SearchCursor(input_table, ["ABBR_NAME", "CELL_ID"]) as cursor:
        
        cell_id_list = []
        for row in cursor:
            if abbr_name == row[0]:
                cell_id_list.append(row[1])
            
        
        if len(cell_id_list) != 0:
            cell_id_string = str(cell_id_list)
            cell_id_string = cell_id_string.replace("u","")
            cell_id_string = cell_id_string.replace("[","")
            cell_id_string = cell_id_string.replace("]","")
            cell_id_string = cell_id_string.replace("'","")
            cell_id_string = cell_id_string.replace(" ","")
            item = (abbr_name + "-" + cell_id_string)
            abbr_name_cell_id_list.append(item)

for x in abbr_name_cell_id_list:
    with arcpy.da.UpdateCursor(input_table, ["ABBR_NAME", "GridIndex"]) as cursor:
        for row in cursor:
            if str((x.split("-"))[0]) == str(row[0]):
                row[1] = (x.split("-"))[0] + " - " + (x.split("-"))[1]
            cursor.updateRow(row)
            
  
0 Kudos
curtvprice
MVP Esteemed Contributor

You could run this on the Pro command line and copy and paste from the results and paste into a text file or word doc.

dd = {}
with arcpy.da.SearchCursor("test", ["STREET", "GRID"]) as rows:
    for row in rows:
        print(row)
        if row[0] in dd: dd[row[0]].append(row[1])
        else: dd[row[0]] = [row[1]]
for k in dd: 
    print('{} - {}'.format(k, ",".join(sorted(list(set(dd[k]))))))

# prints:
# BAKKER RD - B1,B5,B6
# PRUDER ST - A2
0 Kudos