8

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.

3 Answers 3

14

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
6
  • Oh Ok. I could probably use the OBJECTID that is already on the table. Thank you, I'll try this and post any questions I've got. I really appreciate it.
    – yams
    Commented May 7, 2014 at 20:24
  • Instead of using pStart for rec how can I assign one of the fields from the Attribute table for rec?
    – yams
    Commented May 7, 2014 at 21:28
  • I'm getting a OBJECTID is not defined error. My python code is shown above.
    – yams
    Commented May 7, 2014 at 21:37
  • you cannot initialize rec based on "objectid" as you mention in your post, because "objectid" is a field and not a single value. What you could do is a simple conncatenation (which doesn't need a code block)
    – radouxju
    Commented May 8, 2014 at 5:29
  • Hey quick question, how would I check for Null Entries in the FacilityID field?
    – yams
    Commented May 20, 2014 at 20:44
3

I think this is achieved easier with updateCursor:

import arcpy

# Create update cursor for feature class 
fc = r'D:\temp\tempFGDB.gdb\point'
fields = "Munic; Point; UniqueID"
sort_fields="Munic A; Point A"

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

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

First, I would create an additional field for the sequential numbers(PROJECT_ID). Download this toolbox http://www.arcgis.com/home/item.html?id=242e0e5c8273404085b174f66db97ecf

On your project id field right click > Field Calculator and enter :

"WATER" + [PROJECT_ID]

This will concatenate any text you enter with the corresponding project id number. If you already have separate fields for the acronym and project type you could do something similar:

[ACRONYM] + [TYPE] + [PROJECT_ID]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.