Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Is there a convenient (or even effective) method for recoding field values using the field calculator or standalone python script? 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. I would like to avoid using cursors, as I have been told that cursors are inefficient for large data processing (in this case, the table is a few million records). However, if necessary, it will be better than the alternative of doing it by hand.

Perhaps there is a way to create a dictionary at runtime, establishing a field value and its numerical code by sequential steps when processed? For example, creating a key-value pair for a value the first time it is encountered in the dataset being processed?

I apologize if this is a duplicate question, but I was unable to find anything even remotely similar via search or the ESRI forums.

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.

Thank you in advance.


share|improve this question
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 Feb 21 '11 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 Feb 21 '11 at 21:50
This question sounds like the solution at gis.stackexchange.com/q/6312/664 will work. –  whuber Feb 21 '11 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 Feb 21 '11 at 22:47
@Nathanus After all data are entered, simple (and quick) summaries will generate any key-value pairs you may need. –  whuber Feb 21 '11 at 23:30
show 6 more comments

2 Answers

up vote 2 down vote accepted

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].

share|improve this answer
So simple, and yet so sneaky. Perfect. Thank you! –  Nathanus Feb 22 '11 at 18:01
@Nathanus I'm glad we finally understand each other! –  whuber Feb 22 '11 at 18:03
add comment

This is not python but maybe it will help. Is this what you are looking for? 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.

share|improve this answer
Unfortunately, I don't know my bum from a hole in the ground when it comes to 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 Feb 22 '11 at 15:08
are we on 9.3.x or 10? –  Brad Nesom Feb 22 '11 at 15:48
see edit above. –  Brad Nesom Feb 22 '11 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 Feb 22 '11 at 16:48
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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