''' #According mxd's LayerName to modify symbology.classLabels ''' def EditLyrSymInfo(mxd,lyrName): lyr = arcpy.mapping.ListLayers(mxd, lyrName)[0] if lyr.symbologyType == "UNIQUE_VALUES": Labels=lyr.symbology.classLabels Values=lyr.symbology.classValues #use this upt_Labels List to update Layer's symbology.classLabels upt_Labels=[] for value in Values: #value_label dictionary's value in Figure 2 upt_label=label_value[value] upt_Labels.append(upt_label) print u'Labels:{0}, Values:{1};'.format(upt_label,value) lyr.symbology.classLabels=upt_Labels arcpy.RefreshActiveView() arcpy.RefreshTOC()Solved! Go to Solution.
# -*- coding: utf-8 -*- import arcpy, os import arcpy.mapping #Pre-defined ???code:name??? key-value pair code_name={'A':'Apples','B':'Bananas','C':'Cherries'} #Get mxd mxd=arcpy.mapping.MapDocument(r"E:\Test_Symbology\Test_Symbology.mxd") #According mxd&layername ???code:name??? key-value pair to modify lyr.symbology.classLabels def EditLyrSymInfo(mxd,lyrName): lyr = arcpy.mapping.ListLayers(mxd, lyrName)[0] if lyr.symbologyType == "UNIQUE_VALUES": Labels=lyr.symbology.classLabels Values=lyr.symbology.classValues upt_Labels=[] print "Layer Symbology Info to be Edit:",lyrName for value in Values: upt_label=code_name[value] upt_Labels.append(upt_label) print u'Labels:{0}, Values:{1};'.format(upt_label,value) lyr.symbology.classLabels=upt_Labels lyr.symbology.showOtherValues = False arcpy.RefreshActiveView() arcpy.RefreshTOC() mxd.save() def main(): #Print Pre-defined code:name key-value pair print '##Print Pre-defined code:name key-value pair##' for key,value in code_name.iteritems(): print key,value lyrName="PlanPolygonSelectR" #After modify lyr.symbology.classLabels(Labels:Values)key-value pair: print '##After modify lyr.symbology.classLabels(Labels:Values)key-value pair:##' EditLyrSymInfo(mxd,lyrName) if __name__=="__main__": main()Then assign the value list �??lyr.symbology.classValues�?? to customized �??Values�??
cList = ["f", "g", "h", "i", "j"] ## these values don't exist in my data. sym.classValues = aList
cList = ["a", "b", "c"] lList = ["apples", "bananas", "cherries"] sym.classValues = aList sym.classLables = lList
# -*- coding: utf-8 -*-
import arcpy, os
import arcpy.mapping
#Pre-defined �??code�?name�?? key-value pair
code_name={'A':'Apples','B':'Bananas','C':'Cherries'}
#Get mxd
mxd=arcpy.mapping.MapDocument(r"E:\Test_Symbology\Test_Symbology.mxd")
#According mxd&layername �??code�?name�?? key-value pair to modify lyr.symbology.classLabels
def EditLyrSymInfo(mxd,lyrName):
lyr = arcpy.mapping.ListLayers(mxd, lyrName)[0]
if lyr.symbologyType == "UNIQUE_VALUES":
Labels=lyr.symbology.classLabels
Values=lyr.symbology.classValues
upt_Labels=[]
print "Layer Symbology Info to be Edit:"+lyrName
for value in Values:
upt_label=code_name[value]
upt_Labels.append(upt_label)
print u'Labels:{0}, Values:{1};'.format(upt_label,value)
lyr.symbology.classLabels=upt_Labels
lyr.symbology.showOtherValues = False
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
def main():
#Print Pre-defined �??code�?name�?? key-value pair
print u'##Print Pre-defined �??code�?name�?? key-value pair##'
for key,value in code_name.iteritems():
print key,value
lyrName="PlanPolygonSelectR"
#After modify lyr.symbology.classLabels(Labels:Values)key-value pair:
print u'##After modify lyr.symbology.classLabels(Labels:Values)key-value pair:##'
EditLyrSymInfo(mxd,lyrName)
if __name__=="__main__":
main()
Thank you Jeff! I use sample data you provide to update mxd again,but ''lyr.symbology.classLabels=upt_Labels''cannot affect at all,there was not any change in mxd,All my code as follows:# -*- coding: utf-8 -*- import arcpy, os import arcpy.mapping #Pre-defined �??code�?name�?? key-value pair code_name={'A':'Apples','B':'Bananas','C':'Cherries'} #Get mxd mxd=arcpy.mapping.MapDocument(r"E:\Test_Symbology\Test_Symbology.mxd") #According mxd&layername �??code�?name�?? key-value pair to modify lyr.symbology.classLabels def EditLyrSymInfo(mxd,lyrName): lyr = arcpy.mapping.ListLayers(mxd, lyrName)[0] if lyr.symbologyType == "UNIQUE_VALUES": Labels=lyr.symbology.classLabels Values=lyr.symbology.classValues upt_Labels=[] print "Layer Symbology Info to be Edit:"+lyrName for value in Values: upt_label=code_name[value] upt_Labels.append(upt_label) print u'Labels:{0}, Values:{1};'.format(upt_label,value) lyr.symbology.classLabels=upt_Labels lyr.symbology.showOtherValues = False arcpy.RefreshActiveView() arcpy.RefreshTOC() def main(): #Print Pre-defined �??code�?name�?? key-value pair print u'##Print Pre-defined �??code�?name�?? key-value pair##' for key,value in code_name.iteritems(): print key,value lyrName="PlanPolygonSelectR" #After modify lyr.symbology.classLabels(Labels:Values)key-value pair: print u'##After modify lyr.symbology.classLabels(Labels:Values)key-value pair:##' EditLyrSymInfo(mxd,lyrName) if __name__=="__main__": main()
The sample data and mxd are in attatchment,Any help will be appreciated !
# -*- coding: utf-8 -*-
import arcpy, os
import arcpy.mapping
#Pre-defined �??code:name�?? key-value pair
code_name={'A':'Apples','B':'Bananas','C':'Cherries'}
#Get mxd
mxd=arcpy.mapping.MapDocument(r"E:\Test_Symbology\Test_Symbology.mxd")
#According mxd&layername �??code:name�?? key-value pair to modify lyr.symbology.classLabels
def EditLyrSymInfo(mxd,lyrName):
lyr = arcpy.mapping.ListLayers(mxd, lyrName)[0]
if lyr.symbologyType == "UNIQUE_VALUES":
Labels=lyr.symbology.classLabels
Values=lyr.symbology.classValues
upt_Labels=[]
print "Layer Symbology Info to be Edit:",lyrName
for value in Values:
upt_label=code_name[value]
upt_Labels.append(upt_label)
print u'Labels:{0}, Values:{1};'.format(upt_label,value)
lyr.symbology.classLabels=upt_Labels
lyr.symbology.showOtherValues = False
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
mxd.save()
def main():
#Print Pre-defined code:name key-value pair
print '##Print Pre-defined code:name key-value pair##'
for key,value in code_name.iteritems():
print key,value
lyrName="PlanPolygonSelectR"
#After modify lyr.symbology.classLabels(Labels:Values)key-value pair:
print '##After modify lyr.symbology.classLabels(Labels:Values)key-value pair:##'
EditLyrSymInfo(mxd,lyrName)
if __name__=="__main__":
main()
# -*- coding: utf-8 -*- import arcpy, os import arcpy.mapping #Pre-defined ???code:name??? key-value pair code_name={'A':'Apples','B':'Bananas','C':'Cherries'} #Get mxd mxd=arcpy.mapping.MapDocument(r"E:\Test_Symbology\Test_Symbology.mxd") #According mxd&layername ???code:name??? key-value pair to modify lyr.symbology.classLabels def EditLyrSymInfo(mxd,lyrName): lyr = arcpy.mapping.ListLayers(mxd, lyrName)[0] if lyr.symbologyType == "UNIQUE_VALUES": Labels=lyr.symbology.classLabels Values=lyr.symbology.classValues upt_Labels=[] print "Layer Symbology Info to be Edit:",lyrName for value in Values: upt_label=code_name[value] upt_Labels.append(upt_label) print u'Labels:{0}, Values:{1};'.format(upt_label,value) lyr.symbology.classLabels=upt_Labels lyr.symbology.showOtherValues = False arcpy.RefreshActiveView() arcpy.RefreshTOC() mxd.save() def main(): #Print Pre-defined code:name key-value pair print '##Print Pre-defined code:name key-value pair##' for key,value in code_name.iteritems(): print key,value lyrName="PlanPolygonSelectR" #After modify lyr.symbology.classLabels(Labels:Values)key-value pair: print '##After modify lyr.symbology.classLabels(Labels:Values)key-value pair:##' EditLyrSymInfo(mxd,lyrName) if __name__=="__main__": main()For one, your forward/backward sloping single quotes are invalid to the interpreter. It either errors out or just hangs there unitil I kill it. Once I removed them, the script would run.
Then, after I put a mxd.save() in there, the class labels are updated in the mxd.
Here is the code that works just fine for me, using your example data:# -*- coding: utf-8 -*- import arcpy, os import arcpy.mapping #Pre-defined �??code:name�?? key-value pair code_name={'A':'Apples','B':'Bananas','C':'Cherries'} #Get mxd mxd=arcpy.mapping.MapDocument(r"E:\Test_Symbology\Test_Symbology.mxd") #According mxd&layername �??code:name�?? key-value pair to modify lyr.symbology.classLabels def EditLyrSymInfo(mxd,lyrName): lyr = arcpy.mapping.ListLayers(mxd, lyrName)[0] if lyr.symbologyType == "UNIQUE_VALUES": Labels=lyr.symbology.classLabels Values=lyr.symbology.classValues upt_Labels=[] print "Layer Symbology Info to be Edit:",lyrName for value in Values: upt_label=code_name[value] upt_Labels.append(upt_label) print u'Labels:{0}, Values:{1};'.format(upt_label,value) lyr.symbology.classLabels=upt_Labels lyr.symbology.showOtherValues = False arcpy.RefreshActiveView() arcpy.RefreshTOC() mxd.save() def main(): #Print Pre-defined code:name key-value pair print '##Print Pre-defined code:name key-value pair##' for key,value in code_name.iteritems(): print key,value lyrName="PlanPolygonSelectR" #After modify lyr.symbology.classLabels(Labels:Values)key-value pair: print '##After modify lyr.symbology.classLabels(Labels:Values)key-value pair:##' EditLyrSymInfo(mxd,lyrName) if __name__=="__main__": main()
R_
Keep in mind, I tested this "outside" of ArcMap with a closed mxd. When I re-open the mxd, the changes are shown as expected.
Before Script:
[ATTACH=CONFIG]26801[/ATTACH]
After Script:
[ATTACH=CONFIG]26802[/ATTACH]
Just tested inside ArcMap with the python window and mxd = CURRENT. works fine.
In example 2, the mxd was set to "current" ( mxd = arcpy.mapping.MapDocument("current") ) which mean to apply the code to the currently open mxd. This would be run from the python window within ArcMap, so, you would already have the mxd open and would see the changes take place. you would then be warned if you tried to close without saving and could save if needed.
Not all operations require a mxd.save(). If you were exporting an output image in each iteration or something, the changes should be represented in the output. However, without a mxd.save(), the changes made by the script would not show in the mxd the next time you open it.
In example 2, the mxd was set to "current" ( mxd = arcpy.mapping.MapDocument("current") ) which mean to apply the code to the currently open mxd. This would be run from the python window within ArcMap, so, you would already have the mxd open and would see the changes take place. you would then be warned if you tried to close without saving and could save if needed.
Need to be carefull performing such an operation as too many mxd.save(s) within an iteration will crash python (in my case, error = 'PageLayoutObject: Error in executing ExportToJPEG' ). Often will make somewhere between 47 and 112 iterations, then crash for no reason. I had this happen in multiple scripts, once I removed the mxd.save(s), it will finish all 1800+ outputs.
R_