AnsweredAssumed Answered

Scripting a Sequential ID based on highest value

Question asked by buehlerd on Aug 25, 2016
Latest reply on Oct 12, 2016 by buehlerd

I what  i am trying to accomplish is a script that generates a concern ID that is based on a the last highest number that will run as a scheduled task.  An example would be C100, C101, (C102, C103, and C104 were deleted), C105  where the next ID would be C106 and not overwrite C105 as C102 and continue to C103.

 

I found main part of this code somewhere, and I like how it concatenates a letter along with a number. I am up for adding fields that store a number and then concatenates the final ID later, or whatever it takes so long as it quick.  Example would be: ConcernID, PrefixCode, LastNumber (all fields used in script). PrefixCode would be a default value of C and the LastNumber field would hold the number portion, and combine them to form the ConcernID.

 

This will be eventually used with an enterprise geodatabase feature class. Any suggestions would be greatly appreciated.

import arcpy

# Create update cursor for feature class
fc = r'C:\Requests\Test\Seq.gdb\GeoReportingManagement\GeoReporting'
fields = "ConcernPrefix; Point; ConcernID"
sort_fields="OBJECTID"

# Workspace is the connection to the database where fc is stored - DMB
workspace = r'C:\Requests\Test\Seq.gdb'

# Starting an Edit Session - DMB
edit = arcpy.da.Editor(workspace)
edit.startEditing(False, True)
edit.startOperation()

rows = arcpy.UpdateCursor(fc, fields=fields, sort_fields=sort_fields)
previousPoint = None
i = 0
for row in rows:
munic = row.getValue('ConcernPrefix')
# point = row.getValue('Point')
if not previousPoint:
i += 1
elif previousPoint <> point:
i = 1
else:
i += 1

row.setValue('ConcernID','-'.join([str(munic),str(i)]))
# previousPoint = point
rows.updateRow(row)

# Closing the Edit Session - DMB
edit.stopOperation()
edit.stopEditing(True)

Outcomes