Using UniqueValuesSymbology Class at 10.1

852
3
06-13-2012 08:32 AM
MikeMacRae
Occasional Contributor III
Hey,

I am playing around with the UniqueValuesSymbology class introduced at 10.1. It looks like the properties of the layer symbology is passed into python lists (i.e. classValues, classLabels)

I edited the script example from the help menu to update the classValues based on a list built from values in a field ('SMU') in a feature class. This works fine.

What I am struggling with is, after the class Values are updated, I want hard code the matching classLabels (or use a look up table) to update the label based on values in the classValue. So just an if statement to say something like.

  if lyr.symbology.classValues == "M1":
      lyr.symbology.classLabels = "Test"


I understand why this kind of is statement isn't working because the properties are python lists. So, I'm thinking I need to build 2 lists, one for the classValues and then one for the classLabels, but I'm just not sure how to go about it. Here's is my code:


import arcpy
mxd = arcpy.mapping.MapDocument(r"Z:\Test.mxd")
lyr = arcpy.mapping.ListLayers(mxd, "Soil Map Units_temp")[0]

smuList = []
rows = arcpy.da.SearchCursor(lyr, ["SMU"])
for row in rows:
  smuList.append(row[0])

if lyr.symbologyType == "UNIQUE_VALUES":
  lyr.symbology.classValues = smuList
  lyr.symbology.showOtherValues = False


# this is my pseudo code. In essence, this is what I want to do. Update the classLabel based on the values from classValues
  if lyr.symbology.classValues == "M1":
      lyr.symbology.classLabels = "Test"




Any suggestions are welcome.

Thanks,
Mike
Tags (2)
0 Kudos
3 Replies
JeffBarrette
Esri Regular Contributor
I just did something similar for a colleague.  Here is the complete code snippet.  Just like you suggest, I build two empty lists and append the unique values to them.  Note, I'm using the new da.SearchCursor at 10.1.

import arcpy

def int_if_you_can(x):
  return int(x) if x % 1.0 == 0 else x

#Reference layer and update to Unique Value renderer using layer file
mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr100 = arcpy.mapping.ListLayers(mxd, "interval_100")[0]
lyrFile = arcpy.mapping.Layer(r"C:\Active\ArcPY\Users\SteveLynch\interval_100.lyr")
arcpy.mapping.UpdateLayer(df,lyr100,lyrFile)

#Apply source data to layer
sym = lyr100.symbology
sym.addAllValues()

#Generate unique list of lables
classList = []
labelList = []

with arcpy.da.SearchCursor(lyr100, ("mean_cont", "low_cont", "high_cont"), sql_clause=(None,"ORDER BY mean_cont")) as rows:
  for row in rows:
    lowCont = str(int_if_you_can(row[1]))
    highCont = str(int_if_you_can(row[2]))
    if not lowCont + " - " + highCont in labelList:
      classValue = in_if_you_can(row[0])
      classList.append(classValue)
      label = lowCont + " - " + highCont
      labelList.append(label)

#Update layer with new label classes
sym.classValues = classList
sym.classLabels = labelList
arcpy.RefreshActiveView()


Jeff
0 Kudos
jiangjian
New Contributor
I follow your way to update mxd,but ''sym.classLabels = labelList''cannot affect after i check the labelList in ArcMap10.1,in fact,there was not any change in mxd,what's wrong?Any help will be appreciated !
I just did something similar for a colleague.  Here is the complete code snippet.  Just like you suggest, I build two empty lists and append the unique values to them.  Note, I'm using the new da.SearchCursor at 10.1.

import arcpy

def int_if_you_can(x):
  return int(x) if x % 1.0 == 0 else x

#Reference layer and update to Unique Value renderer using layer file
mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr100 = arcpy.mapping.ListLayers(mxd, "interval_100")[0]
lyrFile = arcpy.mapping.Layer(r"C:\Active\ArcPY\Users\SteveLynch\interval_100.lyr")
arcpy.mapping.UpdateLayer(df,lyr100,lyrFile)

#Apply source data to layer
sym = lyr100.symbology
sym.addAllValues()

#Generate unique list of lables
classList = []
labelList = []

with arcpy.da.SearchCursor(lyr100, ("mean_cont", "low_cont", "high_cont"), sql_clause=(None,"ORDER BY mean_cont")) as rows:
  for row in rows:
    lowCont = str(int_if_you_can(row[1]))
    highCont = str(int_if_you_can(row[2]))
    if not lowCont + " - " + highCont in labelList:
      classValue = in_if_you_can(row[0])
      classList.append(classValue)
      label = lowCont + " - " + highCont
      labelList.append(label)

#Update layer with new label classes
sym.classValues = classList
sym.classLabels = labelList
arcpy.RefreshActiveView()


Jeff
0 Kudos
JeffBarrette
Esri Regular Contributor
0 Kudos