10

I am needing to auto increment a field based on groups within a feature class. I have 8 plots within a given polygon and I need to assign them an ID from 1-8 for each set of plots within each polygon. The polygon would have its own unique ID number to be used to group the plots.

I assume it would be an alteration of this:

rec=0
def autoIncrement():
 global rec
 pStart = 1 
 pInterval = 1 
 if (rec == 0): 
  rec = pStart 
 else: 
  rec = rec + pInterval 
 return rec
  • 1
    You probably want to read up on the use of the modulus operator of Python (%) – Vince Jun 27 '16 at 23:47
11

Field calculator for Python

d={}
def GroupOrder(groupID):
  if groupID in d: d[groupID]+=1
  else: d[groupID]=1
  return d[groupID]

---------------------------

GroupOrder( !locality! )

Change !locality! to relevant field.

UPDATE: This variation of expression:

d={}
def GroupOrder(groupID):
  N=d.get(groupID,0);N+=1
  d[groupID]=N
  return N

Should work much faster on large datasets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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