[GIS] Auto-incrementing field in feature class using ArcGIS Desktop

arcgis-10.2arcgis-desktopfield-calculatorpersonal-geodatabasepython-parser

So I'm relatively new to ArcGIS and ArcCatalog. I have created a field name of PROJECTID in a feature class in a Personal Geodatabase. The data type is TEXT. This will be a ten digit ID. The first five characters will be a acronym. The sixth character will be a digit that represents the type of feature. It will be a number. The last four characters will be numbers that should be incremented starting at 0001.

Example:
WATER-1-0001

Screen shot of the field:
enter image description here

So my question is how can I do this in ArcCatalog. I've seen some Python code that will autoincrement like the following:

rec=[OBJECTID]
def autoIncrement(a): 
 global rec 
 return "water" + str(a) + "-" +  format(rec, '04d')

Again I'm a newbie when it comes to ArcGIS .
Now I am getting an error NameError name 'OBJECTID' is not defined.

Best Answer

you need to use this code within ArcMap and the field calculator. Add your feature class in the table of content, right click on it to open the table, right click on the name of the field and launch the field calculator.

Then you check for codeblock and copy the code you mentioned.

enter image description here

now for your code snippets, here is what I would do

rec=0 
def autoIncrement(a): 
 global rec 
 pStart = 1  
 pInterval = 1 
 if (rec == 0):  
  rec = pStart  
 else:  
  rec += pInterval  
 return "water" + str(a) + "-" +  format(rec, '04d')

you call this code using

autoIncrement(!name_of_field!)

where name_of_field contains the type of feature

EDIT : If you want to use the OBJECTID field directly, then a simple concatenation is enough

"WATER-" + str(!typrfield!) + "-" +  format(!OBJECTID!, '04d')

if your number has to depend on the type, it then makes sense to use the Python code block

rec1=0 
rec2=0
def autoIncrement(a): 
 global rec1
 global rec2 
 pStart = 1  
 pInterval = 1 
 if (a == 1):
  if (rec1 == 0):  
   rec1 = pStart  
  else:  
   rec1 += pInterval
  out = "water-1-" +  format(rec1, '04d')  
 else:
  if (rec2 == 0):  
   rec2 = pStart  
  else:  
   rec1 += pInterval
  out = "water-2-" +  format(rec2, '04d')  
 return out
Related Question