1

I'm working on an arcpy script that will automatically set up excluded values, class breaks values, and class break labels in a series of raster layers. A simplified version of part of my code is visible below:

mxd = arcpy.mapping.MapDocument(r'C:\Users\djh\Desktop\GIS Temp\test.mxd')
df = arcpy.mapping.ListDataFrames(mxd)[0]

symbology_lyr = (r'C:\Users\djh\Desktop\GIS Temp\test.lyr')
raster_name = (r'C:\Users\djh\Desktop\GIS Temp\test.gdb\test')

levels = [31.0, 32.0, 33.0, 34.0, 35.0]
class_break_labels = ['31.0 - 32.0', '32.0 - 33.0', '33.0 - 34.0', '> 34.0']

layers = arcpy.mapping.ListLayers(mxd, data_frame=df)

for lyr in layers:
    if lyr.isGroupLayer:
        for sublyr in lyr:
            if sublyr.name == raster_name:
                arcpy.ApplySymbologyFromLayer_management(sublyr, symbology_layer)
                if sublyr.symbologyType == "RASTER_CLASSIFIED":
                    sublyr.symbology.excludedValues = '0 - ' + str(levels[0])
                    sublyr.symbology.classBreakValues = levels[1:]
                    sublyr.symbology.classBreakLabels = class_break_labels

When I run up to the Apply Symbology From Layer line, it works fine and updates the symbology accordingly, but when I include the last 'if' loop, I get the following error on the last line of the above snippet:

ValueError: Number of label classes don't match number of class breaks

I included a print statement to get the difference between the lengths of levels[1:] and class_break_labels (both lists), and for every iteration it was 0, so I don't see how the numbers could possibly not match.

I appropriated the last 'if' loop from this Esri help page.

Has anyone encountered this before?

  • What is symbology_layer set to? Which line number is your error message coming from? At the moment you have not presented a code snippet that will work up to where you are stuck. We need to see the whole error thrown when you run the exact code snippet presented. – PolyGeo Aug 2 '16 at 22:42
  • @PolyGeo I've update the question to hopefully clarify. Thanks. – David Aug 2 '16 at 22:52
  • If you add import arcpy to the beginning of your code snippet, and then run it from an IDE like IDLE, what is the whole error message that you receive? – PolyGeo Aug 2 '16 at 22:55
1

levels = [31.0, 32.0, 33.0, 34.0, 35.0] has 5 values in it, and will go with the 4 in class_break_labels = ['31.0 - 32.0', '32.0 - 33.0', '33.0 - 34.0', '> 34.0'], as per the code sample on the Esri help page.

However, in your code snippet levels[1:] returns only 4 values, and so is not enough to match with the labels.

Try it without the [1:] on levels:

if sublyr.symbologyType == "RASTER_CLASSIFIED":
    sublyr.symbology.excludedValues = '0 - ' + str(levels[0])
    sublyr.symbology.classBreakValues = levels
    sublyr.symbology.classBreakLabels = class_break_labels

If you are wanting to use the first value as exclusion only, and then class breaks from there, you will need to remove your first label also.

The reason you have one more value in your class breaks over your labels is due to the fact that your labels are ranges between each break level.

Breaks    Labels
31.0
          31.0 - 32.0
32.0
          32.0 - 33.0
33.0
          33.0 - 34.0
34.0
          > 34.0
35.0
  • Thank you for this, worked perfectly. Could you please explain why the additional break value is required? When I set these up manually in ArcMap I have excluded values 0 - 31.0, and then the only need 4 break values for 4 break labels. – David Aug 3 '16 at 16:20
  • 1
    @David I have edited my answer to include my understanding of why there is one more break value than label value – Midavalo Aug 3 '16 at 19:20

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.