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:

9 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?

    1. 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.

  5. You are a GOD! I had Googled my ass trying to find this simple function until I bumped here. Other sites with a “solution” say “ArcGIS makes an OBJECTID field. Its values are unique values for you”. Well, they’re bloody not. I mean they are unique, but they don’t take into account the features you deleted during or after digitizing. So, I might have 5 features with 1, 10, 15, 30, 40 for their OBJECTID…
    Is there a way to add leading zeroes? I might want to list my unique values in a program which alphabetizes correctly only with leading zeroes. For example I wouldn’t want the list to go “1, 10, 2” and lead zeroes should fix that (i.e. “01, 02, 03 … 10”).

    Thanks again!

Comments are closed.