'ArcMap Field Calculator Program to create Unique ID's

I'm using the Field Calculator in ArcMap and I need to create a unique ID for every storm drain in my county.
An ID Should look something like this: 16-I-003

The first number is the municipal number which is in the column/field titled "Munic"

The letter is using the letter in the column/field titled "Point"

The last number is simply just 1 to however many drains there are in a municipality.

So far I have:

rec=0
def autoIncrement()
pStart=1 
pInterval=1
 if(rec==0):
  rec=pStart
else:
 rec=rec+pInterval
return "16-I-" '{0:03}'.format(rec)

So you can see that I have manually been typing in the municipal number, the letter, and the hyphens. But I would like to use the fields: Munic and Point so I don't have to manually type them in each time it changes.

I'm a beginner when it comes to python and ArcMap, so please dumb things down a little.



Solution 1:[1]

I'm not familiar with the ArcMap, so can't directly help you, but you might just change your function to a generator as such:

def StormDrainIDGenerator(): rec = 0 while (rec < 99): rec += 1 yield "16-I-" '{0:03}'.format(rec)

If you are ok with that, then parameterize the generator to accept the Munic and Point values and use them in your formatting string. You probably should also parameterize the ending value as well.

Use of a generator will allow you to drop it into any later expression that accepts an iterable, so you could create a list of such simply by saying list(StormDrainIDGenerator()).

Is your question on how to get Munic and Point values into the string ID? using .format()?

Solution 2:[2]

I think you can use following code to do that.

def autoIncrement(a,b):
  global rec
  pStart=1 
  pInterval=1
  if(rec==0):
    rec=pStart
  else:
    rec=rec+pInterval
  r = "{1}-{2}-{0:03}".format(a,b,rec)
  return r

and call

autoIncrement( !Munic! , !Point! )

The r = "{1}-{2}-{0:03}".format(a,b,rec) just replaces the {}s with values of variables a,b which are actually the values of Munic and Point passed to the function.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Pratik