ArcMap Field Calculator: Create a Unique ID

One of the common functions I have to do is assign each record in a feature class with a unique identifier–normally just a sequential number from 1 to N.  In ArcView 3.x, the formula was simply “rec + 1” if I wanted to start with the number 1.

In ArcGIS, the process got a little more complex–you had to write a little VBA in Field Calculator as described by ESRI.

While this option still exists in ArcGIS 10, I believe it will disappear when 10.1 comes out and VBA support is completely eliminated.  But it is doable using Python which will continue to be supported.

Googling around, I did not find an exact answer but Dave Verbyla, Professor of GIS/Remote Sensing at the University of Alaska has a posted some samples that served as a good starting point.

In the Pre-Logic Script Code box, I declare a variable (counter) and a function. Then in the formula, I call the function.

counter = 0
def uniqueID():
  global counter
  counter += 1
  return counter

While composing this post, I actually wanted a concatenated value; “OC” plus an 8 character numeric sequential number starting at OC00000001 so the actual code is shown below:

Advertisements

8 thoughts on “ArcMap Field Calculator: Create a Unique ID

  1. great post. exactly what i needed. here is a code snippet for use in a python script (just output from tool results but might help somewhere)

    arcpy.CalculateField_management(zonePoly, “ZONE_ID”,”uniqueID()”,”PYTHON_9.3″,”counter = 0/ndef uniqueID():/n global counter/n counter +=1/n return counter”)

  2. A small detail I uncovered while trying this – you can’t name your field – ex. UniqueID, the same thing as the python function, ex. UniqueID(). Throw a random character in front of the function name – ex. zUniqueID(). Hope that helps people who tried the same thing I did at first and couldn’t figure out why it wouldn’t run.

    Field would be:
    UniqueID

    Codeblock would be:

    counter = 0
    def zUniqueID():
    global counter
    counter += 1
    return counter

    Formula would be
    UniqueID =
    zUniqueID()

  3. This is not completely random, it is ordered in the same sequence as the FID. Is there anyway to create a unique ID while still preserving the selected order of records?

    • Nice idea, I haven’t had a need to do so yet so I’m not sure if there is directly. If you’re sorting by a field that has a unique value, I can see a work-around: summarize by that field, creating a new table. Add the new field to this new table, use the method described and join the new table to the existing one based on the unique field & copy the values over. Make sense?

  4. Thank you so much! I’ve been searching the web for a practical solution and the only threads were from before version 10, and for some reason all of those scripts were returning errors whenever I ran them. Something so simple like this should definitely be a built-in function or a geoprocessing tool.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s