ArcGIS – Recoding Values Based on Contents Using ArcGIS Field Calculator

arcgis-desktopfield-calculatorvbscript

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:

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

Result:

------------[STYLE]----------[STYLE_CODE]
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.

Best Answer

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

-----------[STYLE]----------[STYLE_CODE]
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

[OID]--[STYLE]----[COUNT]
    1--[4WD]------------4
    2--[DIVIDED]--------1
    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].

Related Question