[GIS] Changing the attribute behind a legend without changing the symbology

arcgis-10.0arcgis-desktoparcobjectslegendsymbology

I have a geology layer with a very large legend that was painstakingly created based on a geological code. I now have a new attribute which is a friendly description for each code and I would like the legend to use (or simply display) the description instead of the geological code. How can I do this without recreating the legend by hand?

Best Answer

The following C# snippet did the trick (in the OnClick() method of a Button AddIn):

const string CodeField = "SMUcode";
const string DescriptionField = "SMUdetails";

IGeoFeatureLayer layer = ArcMap.Document.SelectedLayer as IGeoFeatureLayer;
IUniqueValueRenderer renderer = layer.Renderer as IUniqueValueRenderer;
IDisplayTable table = layer as IDisplayTable;
IFeatureCursor cursor = table.SearchDisplayTable(null, false) as IFeatureCursor;
int indexCodeField = cursor.Fields.FindField(CodeField);
int indexDescriptionField = cursor.Fields.FindField(DescriptionField);
//loop through each feature creating a dictionary mapping codes to descriptions
//code and descriptions should match 1 for 1, but if not, the last description
//for a code value will be used.
var descriptions = new Dictionary<string, string>();
IFeature feature = cursor.NextFeature();
while (feature != null)
{
    string code = feature.Value[indexCodeField] as string;
    string description = feature.Value[indexDescriptionField] as string;
    descriptions[code] = description;
    feature = cursor.NextFeature();
}

//loop through the renderer changing each value
//Assumes descriptions are unique
renderer.Field[0] = DescriptionField;
for (int i = 0; i < renderer.ValueCount; i++)
{
    string code = renderer.Value[i];
    // change the label
    renderer.Label[code] = descriptions[code];
    //Change the value of the renderer item.  
    renderer.Value[i] = descriptions[code];
}
Related Question