English

How To: Add legend range information to the theme table

Summary

You can use Avenue to add a theme's legend classification information to the attribute table. The script in this article creates a new field and populates each record with the legend class label.

Procedure

Load the following code into a new script window, compile, and run the script.

  1. Open a new script window.

    A. Activate the Project window.
    B. Click the Scripts icon.
    C. Click New.

  2. Paste the following code into the script window:

    Code:
    '-- Script: LEGEND2TABLE.AVE
    '-- This script populates a feature theme attribute
    '-- table with a new field containing the legend
    '-- classification label string for the range
    '-- that each record's value falls into.

    '-- To use, make your View the active window,
    '-- and make the desired theme active.

    '-- Setup initial objects
    theView = av.GetActiveDoc
    theTheme = theView.GetActiveThemes.Get(0)
    theFTab = theTheme.GetFTab

    '-- See if the theme's attribute table can be edited
    if (theFTab.CanEdit.Not)
    then
    MsgBox.Warning("The active theme's attribute table"+NL+
    "cannot be edited.","Stop")
    return nil
    else
    theFTab.SetEditable(true)
    end

    '-- Get the legend, its classification, and the legend field
    theLegend = theTheme.GetLegend
    theClassifications = theLegend.GetClassifications
    legendField = theFTab.FindField(theLegend.GetFieldNames.Get(0))

    '-- Find the width of the widest classification
    '-- label and use it to set the width of the new
    '-- range field to be added to the table
    widestString = 5
    listofClLabels = {}
    for each cl in theClassifications
    theLabel = cl.GetLabel
    if (theLabel.Count > widestString)
    then
    widestString = theLabel.Count
    end
    end
    newRgField = Field.Make
    ("RangeLabel",#FIELD_CHAR,widestString,0)
    theFTab.AddFields({newRgField})

    '-- For each record in the theme's attribute table,
    '-- get the value in the legend field
    for each rec in theFTab
    legendFieldValue = theFTab.ReturnValue(legendField,rec)

    '-- For each classification in the legend, determine
    '-- which range contains the legend value for that field
    for each cl in theClassifications
    if (cl.Contains(legendFieldValue))

    '-- Populate that cell with the range's label string
    then
    theFTab.SetValue(newRgField,rec,(cl.GetLabel))
    break
    end
    end
    end

    '-- Stop editing the table
    theFTab.SetEditable(false)
    MsgBox.Info("Done","")

    '-- End of Script: LEGEND2TABLE.AVE

  3. Attach the script to a button on the View GUI.

    A. Compile the script.
    B. Switch to the Project window.
    C. Select Customize from the Project menu.
    D. On the Customize dialog box, click the Type dropdown arrow and click View.
    E. Select Buttons under Category.
    F. Click the New button.
    G. Double-click the Click property in the Customize dialog box.
    H. Type the name of the script in the Script Manager and click Select.
    I. Close the Customize dialog box.

    For more information, see "Customize dialog box" in ArcView Help.

  4. Open the view.
  5. Make the theme active and click the new button.