10.1: problem with lyr.symbology.addAllValues()

488
3
04-29-2012 12:58 AM
LeonidEliseev
New Contributor
I have template lyrs("UNIQUE_VALUES") for my projects and need to limit values.

mxd = arcpy.mapping.MapDocument("CURRENT")
lyr = arcpy.mapping.ListLayers(mxd, "lyr")
lyr.symbology.addAllValues()


After that arcpy shuffle my values. How to limit legend correctly?
Tags (2)
0 Kudos
3 Replies
JeffBarrette
Esri Regular Contributor
Is there a problem with .addAllValues() or are you looking for a way to add only some values.  Because .addAllValues adds ALL values.

You may want to try working with layer files that contain the unique set of values you are interested in.   You could add the layer and it would be possible to change the data source on the layer.

Jeff
0 Kudos
LeonidEliseev
New Contributor
Yes,  i need to  adds ALL values and use .addAllValues for it.
But my template layer file contain more values than may contain current feature class connected to this layer file.
And when i use .addAllValues to limit my layer file to current feature class they broken link between value in layer and value in feature class.

Example:
lyr.lyr have design to [ID]:1,2,3,4,5
shp.shp in current project have features with [ID]:1,3,5

after .addAllValues thay retain design, but it shuffle:
shp.shp lyr.lyr
1         1
3         2
5         3

why reason they doing this? and how to solve this problem?
0 Kudos
JeffBarrette
Esri Regular Contributor
AddAllValues is reapplying the symbology back to the existing values in a random fashion the same way that the user interface works.  For example, add a feature class to the user interface and open the layer properties dialog.  Via the symbology tab set the symbology to Unique Value and click the "Add All Values" button mutliple times.  Each time you click, the symbols for the existing features change.

I think what you want to do is use arcpy.mapping.Updatelayer.  This will update your shapefile using the symbology from the layer file and it will preserve the symbology for the matching symbols.  One problem is that UpdateLayer will bring across all unique values into the TOC and if you insert a legend, lables will appear.  At 10.1, the way to get around this is to check off "Only show classes that are in the visible extent".

mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(mxd, "layers")[0]
myShape = arcpy.mapping.ListLayers(mxd, "MyShape")[0]
lyrFile = arcpy.mapping.Layer(r"C:\Path\To\layerfile.lyr")
arcpy.UpdateLayer(df, myShape, lyrFile, True)


I hope this helps,
Jeff
0 Kudos