Auto Generate Sequential Asset IDs

2244
2
09-01-2016 12:53 PM
MitchellBate1
New Contributor

I am looking for a python script that will automatically assign a unique sequential value to a field every time a new feature (asset) is created. This ID will also need the abbreviated name prepended to the identifier.

So for example, if I add a new sign (SN) to the signs feature class, I'd like that sign to have an Asset ID of SN235.

Alternatively, I could field calculate each time a new asset or series of assets is added so long as the next feature is given the next sequential value.

I have seen scripts such as this: How can you auto-increment in a Python Script (not Field Calculator)?

But it isn't quite what I need.

Has anyone created or come across a script that will do this? Any insight on this is much appreciated!

Thanks!

0 Kudos
2 Replies
ClintonDow1
Occasional Contributor II

Two quick ways off the top of my head: First, if the AssetID field isn't yet populated you could use a cursor to fill it in:

prefix = 'SN'

with arcpy.da.UpdateCursor(file, ['AssetId']) as cursor:

   for i, row in enumerate(cursor):

      row[0] = prefix + str(i)

 cursor.updateRow(row)

prefix could be a parameter from your tool interface, or output from a function which returns the first two letters of the feaure type, or anything else for that matter, I'll leave that up to you though

If AssetID field is already populated and you're just filling it in for a new row:

prefix = 'SN'

cursor = arcpy.da.UpdateCursor(file, ['AssetId']) 

no_asset_id = [r for r in cursor if r[0] is None][0]

no_asset_id[0] = prefix + str(len(cursor))

cursor.updateRow(no_asset_id)

AndresCastillo
MVP Regular Contributor

This looks great, I will try.

Since its' a script, is there a way to execute it on new record creation?