2

How can I change the symbology of a layer with single symbol to unique values with Python. I don't know how to update it.

I used this code:

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
lyr = arcpy.mapping.ListLayers(mxd,"region")[0]
if lyr.symbologyType == "UNIQUE_SYMBOLE":
  lyr.symbology.valueField = "REGION"
  lyr.symbology.addAllValues()
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
del mxd

The code runs but doesn't change anything (no error message) the problem is that I want to change the symbology of the layer displaid with single symbol to category "uniques values"?

3
  • Can you edit your Question to include your ArcGIS version and what happens (error message etc) when you run your code, please?
    – PolyGeo
    Dec 10, 2013 at 22:17
  • 1
    lyr.symbologyType will never equal "UNIQUE_SYMBOLE". The list of possible values is: GRADUATED_COLORS, GRADUATED_SYMBOLS, OTHER, UNIQUE_VALUES and RASTER_CLASSIFIED as per the website on the layer class.
    – Fezter
    Dec 10, 2013 at 22:56
  • @Fezter I think you should turn your comment into an answer
    – PolyGeo
    Aug 6, 2016 at 4:31

2 Answers 2

2

As commented by @Fezter:

lyr.symbologyType will never equal "UNIQUE_SYMBOLE". The list of possible values is: GRADUATED_COLORS, GRADUATED_SYMBOLS, OTHER, UNIQUE_VALUES and RASTER_CLASSIFIED as per the website on the layer class.

2
  1. First manually create a layer file with a unique value symbology based on your single symbol layer (not shown below).
  2. Then use arcpy.mapping.UpdateLayer to update your single symbol layer

    mxd = arcpy.mapping.MapDocument("Current")
    df = arcpy.mapping.ListDataFrames(mxd)[0]
    
    updateLayer = arcpy.mapping.ListLayers(mxd, "single_symbol_layer", df)[0]
    sourceLayer = arcpy.mapping.Layer("unique_value_symbol_layer")
    arcpy.mapping.UpdateLayer(df,updateLayer,sourceLayer, symbology_only = True)
    
    if updateLayer.symbologyType == "UNIQUE_VALUES":
        updateLayer.symbology.valueField = "UniqueValueField"
        updateLayer.symbology.addAllValues()
    
    arcpy.RefreshActiveView()
    arcpy.RefreshTOC()
    

To ensure the values are ordered replace:

updateLayer.symbology.valueField = "UniqueValueField"
updateLayer.symbology.addAllValues()

with

updateLayer.symbology.classValues = listofUniqueValues
updateLayer.symbology.showOtherValues = False

You can create the listofUniqueValues by:

listofUniqueValues = sorted({row[0] for row in arcpy.da.SearchCursor("single_symbol_layer", ["UniqueValueField"]) if row[0]})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.