2

My ArcGIS Pro custom script tool is based off of a script that changes break values from a user's input. I believe I've done all I can troubleshooting the existing script. I'm suspicious of the tool parameters now.

GOAL: How do you change the Upper break value to a decimal? The user of this tool needs to be able to enter decimals. Here's the relevant part of the script that will set the break values:

#class break values
classbreakvalues = arcpy.GetParameterAsText(0)
breakvalues_list = classbreakvalues.split(";")
new_breakvalues_list = []
for brkvalues in breakvalues_list:
    newbreak = Decimal(brkvalues)
    arcpy.AddMessage(f'break value: {newbreak}')

Tool parameters:

enter image description here

Result:

enter image description here

What I've tried:

#Results in neither decimal in layer symbology nor print statement
for brkvalues in breakvalues_list:
    newbreak = int(float(brkvalues))
    arcpy.AddMessage(f'newbreak: {newbreak}')

#prints these values
#break value: 40
#break value: 45
#break value: 60
#break value: 100

#No decimal in layer symbology, but decimal in print statement
for brkvalues in breakvalues_list:
    newbreak = float(brkvalues)
    arcpy.AddMessage(f'break value: {newbreak}')

#prints these values
#break value: 40
#break value: 45
#break value: 60.9
#break value: 100

#No decimal in layer symbology, but decimal in print statement
from decimal import Decimal
for brkvalues in breakvalues_list:
    newbreak = Decimal(brkvalues)
    arcpy.AddMessage(f'break value: {newbreak}')

#prints these values
#break value: 40
#break value: 45
#break value: 60.9
#break value: 100

I've used various Data Types in the tool's Properties-- String, Double, Long. Not one allowed a decimal in the symbology.

enter image description here

2
  • What if you use str(brkvalues)?
    – Binx
    Commented Oct 20, 2021 at 22:27
  • If I set it to newbreak = str(brkvalues), I get TypeError: must be real number, not str.
    – Pfalbaum
    Commented Oct 21, 2021 at 14:35

1 Answer 1

1

The problem was further down in the script. I was too concentrated on one section. I needed to set the upperBound to a float. This is the part in the script where it takes the user input and sets the upper value in the symbology. Now I'm seeing decimals.

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 #use counter to give a number to each map in for loop
for map in aprx.listMaps():
    arcpy.AddMessage(f'--- map: {map.name} ---')
    layers = map.listLayers()
    for layer in layers:
        arcpy.AddMessage(f'layer: {layer.name}')
        sym = layer.symbology   
        #Reset renderer 
        sym.updateRenderer('SimpleRenderer')
        sym.updateRenderer('GraduatedColorsRenderer')

        if hasattr(sym, 'renderer') and layer.name.startswith('BaseLayer'):
            Renderer = sym.renderer
            Renderer.classificationField = fields[c]
            #class break values
            classbreakvalues = arcpy.GetParameterAsText(0)
            breakvalues_list = classbreakvalues.split(";")
            new_breakvalues_list = []
            for brkvalues in breakvalues_list:
                newbreak = brkvalues
                new_breakvalues_list.append(newbreak)
            classBreakValues = new_breakvalues_list
            #class break labels
            classbreaklabels = arcpy.GetParameterAsText(1)
            breaklabels_list = classbreaklabels.split(";")
            classBreakLabels = breaklabels_list
            arcpy.AddMessage(f'class break label: {classBreakLabels}')

            # Run the Renderer.breakCount function to use the classBreakValues 
            # array parameters as the values, and create a counter.
            Renderer.breakCount = len(classBreakValues)

            i = 0 
            for brk in Renderer.classBreaks:
                brk.upperBound = float(classBreakValues[i]) #<--force to float
                brk.label = classBreakLabels[i]
                arcpy.AddMessage(f'brk label: {brk.label}')
                i+=1

                layer.symbology = sym
    c+=1

EDIT: I just learned I could have made life easier on myself had I used arcpy.GetParameter() instead of arcpy.GetParameterAsText.

arcpy.GetParameterAsText Gets the specified parameter as a text string by its index position from the list of parameters.

arcpy.GetParameter() The parameter is returned as an object.

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.