I am trying to create unique IDs for an attribute named "FACILITYID" (str) and have got a ton of help from related posts and online python tutorials. I selected the rows I wished to create unique ID's for, and used this autoIncrement() function in the field calculator which works fine.
def autoIncrement(pStart=1,pInterval=1): global rec if (rec == 0): rec = pStart return rec else: rec = rec + pInterval return rec
However, I would like to keep track of the variable rec so that I can make new unique IDs later. After concluding that I must move out of the field calculator, I have attempted to use cursors instead.
To update the maximum ID value, I created a data table in the environment with a single attribute "FACILITYID" that can be updated when the code runs. I have had success with this except for one small detail: I only want to update rows that do not already have a FacilityID. Specifically, I only want the code to work for rows in with the FACILITYID is equal to one space character.
FACILITYID == ' '
I have tried countless if statements, while loops, the getValue() function, and more, but can't seem to get it right. Can anyone help me make this code work for my target attribute fields?
#import modules import os, sys import arcpy #Set variables rec = 0 workspace = "Database Connections\FE.sde" arcpy.env.workspace = workspace feature_name = "FE.SManhole" #change feature class if required field = "FACILITYID" table_name = "FE.SWR_FacilityID" tRows = arcpy.SearchCursor(table_name) tRow = tRows.next() #Pull in the current highest value from Facility_ID table maxID = int(tRow.getValue(field)) # define automatic increment function def autoIncrement(pStart=maxID,pInterval=1): global rec if (rec == 0): rec = pStart return rec else: rec = rec + pInterval return rec #loop through table to update FacilityID if needed. cursor = arcpy.UpdateCursor(feature_name) #There is no guarantee of order here for row in cursor: #####location where I have been trying if statements.#### row.setValue(field, str(autoIncrement())) cursor.updateRow(row) #save new rec value to FE.SWR_FacilityID table maxID = rec tRows = arcpy.UpdateCursor(table_name) tRow = tRows.next() tRow.setValue(field, str(maxID)) tRows.updateRow(tRow) del tRows, tRow, cursor, row print 'Highest "FACILITYID" Value: ' + str(maxID)
I am very new to python and I got a majority of this code from previous posts including Auto-incrementing in Python Script with ArcPy cursor?