Is there a convenient (or even effective) method for recoding field values using the field calculator?

In this case, I am given several unique strings to identify a type of road feature. For the purpose of creating a unique ID-based join field, I want to give each road type a number to act as its equivalent when matching and classifying.

For example:

Say we have a shapefile of road line links called "Roads" with attributes "Type" and "TypeCode". If there are 3 types "Foo", "Bar", and "Other", I would like the calculation to assign the number 1 to whichever type it encounters first, and fill "TypeCode" with 1 for each successive row that contains the same type. It would do the same for the other two types, assigning 2 and 3 based on order encountered and fill the "TypeCode" for the appropriate rows.

In an attempt to make things more clear, another way of explaining what I'm looking for:

With a table of polyline road links, we have two fields, STYLE and STYLE_CODE:

Feature 1 -- [4WD]----------------[]
Feature 2 -- [DIVIDED]------------[]
Feature 3 -- [HIGH CLEAR]---------[]
Feature 4 -- [4WD]----------------[]

The point is to have the hash table created after being given a set of data. In this case, the script would see Feature 1's Style as the first ocurrence of the Style and assign it an arbitrary unique value (Let's say the value is 1). Feature 2's Style would also be the first occurence, so the script assigns it the value 2. Feature 3's Style gets the value 3. When Feature 4 is iterated, the script already has a value set for the key 4WD, so it assigns a 1 to the STYLE_CODE field again. This continues until the end of the table.


Feature 1 -- [4WD]----------------[1]
Feature 2 -- [DIVIDED]------------[2]
Feature 3 -- [HIGH CLEAR]---------[3]
Feature 4 -- [4WD]----------------[1]

I'd like to create a ubiquitous script that processes a table in this fashion. Sure, I only have to make the hash table once per product if I do it by hand, but making it for every product will eat up too much time.

  • Could you add an example of what you are describing? Do you want both a unique ID for each element in the million records, and a way to relate subsets of the data to another table? Is it a once off operation, or a frequent one? If once off, even if cursors are slower (I don't know if that is true), it might not matter.
    – djq
    Commented Feb 21, 2011 at 20:14
  • should you also normalize data first? Or perhaps it has been? It would be inefficient to classify and find that two classes are only one or two mis-spelled letters off. Is this the same question/ process? gis.stackexchange.com/questions/6312/…
    – Brad Nesom
    Commented Feb 21, 2011 at 21:50
  • 1
    This question sounds like the solution at gis.stackexchange.com/q/6312/664 will work.
    – whuber
    Commented Feb 21, 2011 at 22:47
  • @Brad The example given is doing a similar process (although I've never heard the term stratigrafic in my life), but I would like this to be completed dynamically based on the data given (as opposed to creating key-value pairs ahead of time). Assume data is normalized to avoid unintended variation in strings.
    – Nathanus
    Commented Feb 21, 2011 at 22:47
  • @Nathanus After all data are entered, simple (and quick) summaries will generate any key-value pairs you may need.
    – whuber
    Commented Feb 21, 2011 at 23:30

2 Answers 2


The table processing you describe is accomplished as a straightforward summary.

In the example, use [Style] as the key in the summary. Let's flesh out the example a little. Suppose the table contains

Feature 1 [4WD]----------------[]
Feature 2 [DIVIDED]------------[]
Feature 3 [HIGH CLEAR]---------[]
Feature 4 [4WD]----------------[]
Feature 5 [4WD]----------------[]
Feature 6 [HIGH CLEAR]---------[]
Feature 7 [4WD]----------------[]

where "[]" denotes anything. Then the summary would be

    3--[HIGH CLEAR]-----2

There it is: your lookup table. Just copy the unique identifier [OID] into a new [Style_Code] field. You can join this table to any other table that contains a match to [Style], thereby accessing the corresponding style code in the foreign key [Style_Code].

  • @Nathanus I'm glad we finally understand each other!
    – whuber
    Commented Feb 22, 2011 at 18:03

This is part of an example from a field calculator script file. When you run something like this on your destination field you calculate a value.

 Static d As Object
 Static i As Long
 Dim lSerial As Long
 Dim sField

 sField = [Type]

If (i = 0) Then
  Set d = CreateObject("Scripting.Dictionary")
End If
If (d.Exists(CStr(sField))) Then
  lSerial = d.Item(CStr(sField)) + 1
   d.Item(CStr(sField)) = lSerial
  lSerial = 1
  d.Add CStr(sField), lSerial
End If
i = i + 1


OK let's go here If you are on 10 go here

Sorry I am not able to help more. Here is where I found that code. Look at the calculate 5.0 mark duplicates#.cal
He does have a new version for 10 but states there are not as many routines. I have just downloaded it.

Last but not least. I found this... on ESRI forum. Maybe it is the one to help.

  • Unfortunately, I don't know VB... I think I may have a slight handle on what is going on here based on my knowledge of Python, but could you perhaps add a bit of commenting and/or directions to where I might find this script's documentation?
    – Nathanus
    Commented Feb 22, 2011 at 15:08
  • are we on 9.3.x or 10?
    – Brad Nesom
    Commented Feb 22, 2011 at 15:48
  • see edit above.
    – Brad Nesom
    Commented Feb 22, 2011 at 16:05
  • Actually, frequency analysis would have use, since it would create a table of the unique values for me, and then calculating a code would be a simple matter from there. Not an ideal solution, but one that works. At least, if I had an ArcInfo license. I'd plus your answer if I could, but for now I'll just accept it as a viable workaround given proper licensing. Although I wonder if there is a similar function that returns an object I can .getOutput from.
    – Nathanus
    Commented Feb 22, 2011 at 16:48

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.