Graduated Colors Renderer On Existing Layer Not Successful

349
4
09-02-2021 09:57 AM
JaredPilbeam2
MVP Regular Contributor

Using the below script, I'm not able to change a layers symbology if it is already set. For example, I have three layers with 4 break values and labels each. The intention of the script is to change a break value and label. If I run the script on the layers as they exist currently, the script runs successfully, but the changes aren't made and I get an error of sorts in the Symbology pane.

Three graduated colors layers before running script.

JaredPilbeam2_1-1630601120191.png

I change a break value from 55.3 to 56.3, and it's corresponding label. Then I run the script.

 

import arcpy, os, sys
# Reference a project, map, and layer using arcpy.mp
p = arcpy.mp.ArcGISProject('current')
m = p.listMaps()[0]
lyr = m.listLayers()

for l in lyr[1:4]:
    sym = l.symbology
    if hasattr(sym, 'renderer'):
        if sym.renderer.type == 'GraduatedColorsRenderer':
            sym.renderer.classificationField = 'VaccPercentageTotPop'
            print(f'changing layer symbology for {l}')
            
            #enter class break values
            classBreakValues = [40, 45, 56.3, 100]
            classBreakLabels = ["0% - 40%", "40.01% - 45%", "45.01% - 56.3%", "55.31% - 100%"]
            
            # Run the renderer.breakCount function to use the classBreakValues array parameters as the values, and create a counter.
            sym.renderer.breakCount = len(classBreakValues)
            count = 0
            
            #Create a loop to set the renderer
        for brk in sym.renderer.classBreaks:
            brk.upperBound = classBreakValues[count]
            brk.label = classBreakLabels[count]
            count +=1
        
        l.symbology = sym
p.save()

###prints:
### changing layer symbology for Elementary Districts
### changing layer symbology for Secondary Districts
### changing layer symbology for Unified Districts

 

 

The following is what happens. I get a warning in the Symbology pane (which doesn't tell me much).

JaredPilbeam2_2-1630601749387.png

And my layers no longer have values.

JaredPilbeam2_3-1630601772187.png

0 Kudos
4 Replies
DanPatterson
MVP Esteemed Contributor

I don't suppose "CURRENT" would be the cause given that this is python and assuming esri would be following pythonic adherence to case sensitivity etc ....


... sort of retired...
0 Kudos
JaredPilbeam2
MVP Regular Contributor

"CURRENT" and "current" seem to be interchangeable I've found.

However, if I reset the layers' symbology, and then run the script it works fine. But, I won't be resetting the symbology every time just so I can run the script, of course. By "reset" I mean physically changing the symbology: Graduated Colors --> Unique Values --> back to Graduated Colors.

For some reason,  it doesn't like that the symbology is already set as Graduated Colors.

0 Kudos
JaredPilbeam2
MVP Regular Contributor

I've also tried the script as a standalone. No cigar.

0 Kudos
DanPatterson
MVP Esteemed Contributor

but sadly you didn't get an error message for little "current".

Now that you mention it, I have found your "Graduated Colors --> Unique Values --> back to Graduated Colors" my experience when working manually with classifications... It is almost instinctive now that you mention it.


... sort of retired...
0 Kudos