0

I'm working in an ArcGIS Pro 2.8 Notebook. GOAL: How would I set the classificationField property to read a list of fields? I believe this is a python matter more so than arcpy. My whole script is meant to iterate 3 polygon layers in 8 maps and change the breakCount of the layers based off of each maps' corresponding classificationField. The polygons in each map are identical. I've been using this as a reference, which uses only one classificationField.

I've tried iterating the fields inside both the Maps' and Layers' for loops.

import arcpy
aprx = arcpy.mp.ArcGISProject('CURRENT')

fields =  ['VaccPercentageTotPop', 'VaccPercentage0to18', 'VaccPercentage11to14', 'VaccPercentage12to18', 
           'VaccPercentage14to16', 'VaccPercentage16to18', 'VaccPercentage19to64', 'VaccPercentage65Plus']

for map in aprx.listMaps():
    print(f'--- map: {map.name} ---')
    layers = map.listLayers()
    
    for layer in layers:
        print(f'layer: {layer.name}')
        sym = layer.symbology 

        if hasattr(sym, 'renderer') and layer.name.startswith('BaseLayer'):
            #I'm working with graduated colors
            if sym.renderer.type == 'GraduatedColorsRenderer':
                Renderer = sym.renderer
                
                #for every map, change the symbology based on its corresponding field
                for f in fields:
                    print(f'field: {f}') 
                    Renderer.classificationField = f

                    classbreakvalues = [40, 45, 56.8, 100]
                    classbreaklabels = ['0-40%', '41-45%', '46-56.8%', '56.9-100%']
                    Renderer.breakCount = len(classbreakvalues)

                    i = 0 
                    for brk in Renderer.classBreaks:
                        brk.upperBound = classbreakvalues[i]
                        brk.label = classbreaklabels[i]
                        i+=1

                    layer.symbology = sym
                    aprx.save()

Unfortunately, it's changing all the classificationFields at once. So, the symbology field ends up being set for VaccPercentage65Plus, for example, because its the last field to be run.

--- map: Tot Pop ---
layer: BaseLayer_Processed_Unified_09272021
field: VaccPercentageTotPop
field: VaccPercentage0to18
field: VaccPercentage11to14
field: VaccPercentage12to18
field: VaccPercentage14to16
field: VaccPercentage16to18
field: VaccPercentage19to64
field: VaccPercentage65Plus
layer: BaseLayer_Processed_Secondary_09272021
field: VaccPercentageTotPop
field: VaccPercentage0to18
field: VaccPercentage11to14
field: VaccPercentage12to18
field: VaccPercentage14to16
field: VaccPercentage16to18
field: VaccPercentage19to64
field: VaccPercentage65Plus
layer: BaseLayer_Processed_Elementary_09272021
field: VaccPercentageTotPop
field: VaccPercentage0to18
field: VaccPercentage11to14
field: VaccPercentage12to18
field: VaccPercentage14to16
field: VaccPercentage16to18
field: VaccPercentage19to64
field: VaccPercentage65Plus
--- map: 0-18 ---
layer: BaseLayer_Processed_Unified_09272021
field: VaccPercentageTotPop
field: VaccPercentage0to18
...

Ideally: find the map --> find the layer --> change the property symbology. And maybe print something like this:

--- map: Tot Pop ---
layer: BaseLayer_Processed_Elementary_09272021
field: VaccPercentageTotPop
layer: BaseLayer_Processed_Secondary_09272021
field: VaccPercentageTotPop
layer: BaseLayer_Processed_Unified_09272021
field: VaccPercentageTotPop
--- map: 0-18 ---
layer: BaseLayer_Processed_Elementary_09272021
field: VaccPercentage0to18
layer: BaseLayer_Processed_Secondary_09272021
field: VaccPercentage0to18
layer: BaseLayer_Processed_Unified_09272021
field: VaccPercentage0to18
...
1

If I understood correctly, your script doing what it supposed to do. Graduated Colours can be assigned to one specific field in a layer but you are cycling all fields and your symbology stuck at VaccPercentage65Plus provided it is the last element in your fields list. If you comment out for f in fields:, I think you will have the expected result. You do not need to loop the fields because the symbology field has been defined already.

0

Thanks for your answer. There were quite a few things I had to do to get this to work the way I wanted. (1) Added a counter for each field in the list. (2) Added these lines: sym.updateRenderer('SimpleRenderer') & sym.updateRenderer('GraduatedColorsRenderer') to get the symbols to "reset" because they would not update as they were. (3) Removed this line: if sym.renderer.type == 'GraduatedColorsRenderer': because it was unnecessary. (4) Removed for f in fields because it was unnecessary. (5) Though not critical I removed aprx.save() because it slowed the script way down. Correction notes are also added to the updated script.

import arcpy
aprx = arcpy.mp.ArcGISProject('CURRENT')

#make sure fields in list are in same order as maps in project
fields =  ['VaccPercentageTotPop', 'VaccPercentage11to14', 'VaccPercentage0to18', 'VaccPercentage12to18',  
           'VaccPercentage14to16', 'VaccPercentage16to18', 'VaccPercentage19to64', 'VaccPercentage65Plus']

c = 0 #### added counter
for map in aprx.listMaps():
    print(f'--- map: {map.name} ---')
    layers = map.listLayers()
    for layer in layers:
        print(f'layer: {layer.name}')
        sym = layer.symbology    
        sym.updateRenderer('SimpleRenderer') #### added 
        sym.updateRenderer('GraduatedColorsRenderer') #### added

        if hasattr(sym, 'renderer') and layer.name.startswith('BaseLayer'):
            #if sym.renderer.type == 'GraduatedColorsRenderer':  Removed! 
            Renderer = sym.renderer
            #for f in fields: Removed! 
            Renderer.classificationField = fields[c] ### added counter

            classbreakvalues = [40, 45, 60, 100]
            classbreaklabels = ['0-40%', '41-45%', '46-60%', '60.4%-100%']
            Renderer.breakCount = len(classbreakvalues)

            i = 0 
            for brk in Renderer.classBreaks:
                brk.upperBound = classbreakvalues[i]
                brk.label = classbreaklabels[i]
                i+=1

                layer.symbology = sym
                #aprx.save() Removed! 
    c+=1 #### added counter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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