HOW TO

Extract legend components and store them for later use [SCRIPT]

Last Published: April 25, 2020

Procedure

How can I extract legend classifications and symbols to use later in my programming?

Answer:

The following script demonstrates how to extract the legend components of a
theme and store them in a dictionary attached to the view. These classifications and corresponding symbols can be used to restore the symbols
for particular classifications with additional Avenue programming.

'--- This script will work with a View active and one Theme active.
'--- It does the following:
'--- 1. Check to see if you need to rebuild the object tag,
'--- 2. Query the client for yes/no
'--- 3. Set the object tag to nil
'--- 4. Check for missing object tag or for missing elements you may need
'--- to rebuild, rebuild if necessary
'--- 5. Make sure there is only one theme active
'--- 6. Make a dictionary and populate it with the classifications and the
symbols
'--- 7. Loop through the symbols and report back some info

'--- Clear the object tag
if (msgbox.MiniYesNo("Would you like to rebuild the object tag?",FALSE) =
TRUE)
then
av.getProject.FindDoc("View1").SetObjectTag(nil)
end

'--- Get the current view
theView = av.GetActiveDoc

'--- Get the active theme(s)
theThemeList = theView.GetActiveThemes
if (theThemeList.Count <> 1) then
msgbox.info("Please activate only one theme","To Many Themes")
exit
else
theTheme = theThemeList.Get(0)
end

'--- Get the stored dictionary
aDict = theView.GetObjectTag

'--- Make a dictionary to store bitmaps if needed
'--- Check for none or an empty one
if ((aDict = nil) or (aDict.Count = 0)) Then
av.showmsg("Building the dict...")
'see help for hashing size on command below (the 75)
aDict = Dictionary.Make(75)

'--- Get the legend
theLegend = theTheme.GetLegend

'--- Get the classification list
theclassificationslist = thelegend.getclassifications

'--- Get the symbols
theSymbollist = theLegend.GetSymbols

'--- Extract the number of symbols - hence the classifications
theNumberOfSymbolsInTheTheme = theSymbolList.Count

'--- Loop through the total classifications (symbols as well) and store
'--- the content in the dictionary.

'--- Populate the dict with classifications and symbols
for each i in (0..(theNumberOfSymbolsInTheTheme - 1)) 'adjust for start
of
0
'--- Get the classification
aClassification = theclassificationslist.Get(i)
'--- Get the symbol
aSymbol = theSymbollist.Get(i)
'--- Add them to the dictionary

'--- CLONE ALL ADDITIONS TO THE DICTIONARY
'--- Reclone them as you copy symbols back out for future use, without
proper
'--- cloning, only a pointer to the actual symbol is taken, copies of
the symbols
'--- are needed so they are not shared between classifications

aDict.Add(aClassification.Clone, aSymbol.Clone)
end

'--- Save the new dictionary
theView.SetObjectTag(aDict)

end '--- Make a dict and populate it if needed

'--- Retrieve info from the dictionary.

'--- Get a list of the keys
theDictKeyList = aDict.ReturnKeys

'--- Show the keys in a listbox
'msgbox.listasstring(theDictKeyList,"","")

'--- Process each record by looking up the key and getting the value

'--- Make a list to store the info in
aMasterList = List.Make

for each v in theDictKeyList 'keys are the classifications

'--- Get requests return the value attached to the key
theLabel = v.GetLabel
theRangeString = v.ReturnRangeString
theMinimum = v.returnMinimum
theMaximum = v.returnMaximum
'msgbox.report(theLabel +nl+ theMinimum.AsString +nl+ theMaximum.AsString
+nl+ theRangeString.AsString ,"")
theClassificationString = theLabel.asstring ++ theMinimum.AsString ++
theMaximum.AsString ++ theRangeString.AsString
aMasterList.Add(theClassificationString)

'--- Get the symbol info
theSymbolType = aDict.Get(v).getType
thecolorList = aDict.Get(v).GetColor.GetRGBList
theOLcolor = aDict.Get(v).getOLColor
theBGcolor = aDict.Get(v).getBGColor
'theSize = aDict.Get(v).GetSize
'msgbox.report(theSymbolType.AsString +nl+ thecolorList.Get(0).AsString ++
theColorList.Get(1).AsString
'++ theColorList.Get(2).AsString,"")
theSymbolString = theSymbolType.AsString ++ thecolorList.Get(0).AsString
++
theColorList.Get(1).AsString
++ theColorList.Get(2).AsString
aMasterList.Add(theSymbolString)
end

'--- Present the user with the info
msgbox.listAsString(aMasterList,"Classification first line followed by symbol
info next line","")

'--- End of Script

Notes:

Legend objects store 2 distinct lists, one that will store the classifications and another that stores the symbols. These lists are parallel in nature, that is element zero (0) in the lassifications list refers to element zero (0) in the symbol list. Classifications have 4 properties, a Label, a minimum, a maximum, and a range string. Symbols have a number of properties. See the online help for more information.

Article ID:000001463

Software:
  • Legacy Products

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic