PCLivingood

Programmatically Set Colors for Unique Values Symbology?

Discussion created by PCLivingood on Aug 19, 2013
Latest reply on Aug 26, 2013 by PCLivingood
I have Python code that iterates through about 100 value fields and makes a single PDF composed of maps from each display.  I am using Unique Values Symbology, and I would like to figure out a way to programmatically control the color that is displayed with each value.  That is, value A should be red on all the maps, Value B blue, etc.  Is there a way to do this with arcpy?  Or, if not, what is the quickest way to accomplish this?

The existing code I cobbled together using examples on this forum, and so I thought I would pay forward by including the existing code for

#This code reads in a list of Species Names from a Table
#then interates through those and for each one sets the
#symbology on the map to a corresponding field and exports
#each page as a part of a single PDF


import arcpy,os

def MakeMap(SpeciesName):
    #Takes a Species Name and exports a PDF Map
    #The field name is Based on Species Name

    print SpeciesName
    FieldName="SpeciesData$." + SpeciesName + "__Source"
    FieldName=FieldName.replace (" ","_")

    #Set title on Map
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "Title"):
        elm.text = SpeciesName

    #Set Unique Values Field to FieldName
    if lyr.symbologyType == "UNIQUE_VALUES":
      lyr.symbology.valueField = FieldName
      lyr.symbology.addAllValues()

      #Remove Null Value fron display
      sourceList=lyr.symbology.classValues
      sourceList.remove("<Null>")
      lyr.symbology.classValues=sourceList
      lyr.symbology.showotherValues = False

    arcpy.RefreshActiveView()
    arcpy.RefreshTOC()

    #Position Legend
    legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT", "Legend")[0]
    legend.elementPositionY=.15

    #Export to PDF
    arcpy.mapping.ExportToPDF(mxd, r"C:\temp.pdf")
    pdfDoc.appendPages(r"C:\temp.pdf")
    return


sourceList = []
speciesList = []
mxd = arcpy.mapping.MapDocument("current")
lyr = arcpy.mapping.ListLayers(mxd, "DistrictMap")[0]

#Fill Species List from Table
fc = "C:\Species List.xlsx\Species_List$"
rows = arcpy.SearchCursor(fc)
for row in rows:
    speciesList.append(row.Species)

#Set PDF file name and remove if it already exists
pdfPath = r"C:\FullMapsPDF.pdf"
if os.path.exists(pdfPath):
    os.remove(pdfPath)
#Create the PDF file
pdfDoc = arcpy.mapping.PDFDocumentCreate(pdfPath)

#Iterate through Species
for MySpecies in speciesList:
    MakeMap(MySpecies)

#Commit changes and delete variable reference
pdfDoc.saveAndClose()
del pdfDoc
del mxd

Outcomes