Select to view content in your preferred language

Save feature class as lyrx file - ArcPy (ArcGIS Pro)

1184
3
09-13-2018 08:13 AM
Business_IntelligenceSoftware
Regular Contributor

I have a python script that finds the feature class with the highest count for a field called 'SUM_USER_VisitCount' and then creates the symbology I specify. What I then want to do is save this feature class as a lyrx file, but the code I have for this seems to get skipped over. Can someone help me figure out what I'm doing wrong?

Here is a section of my code (let me know if you need to see the whole code):

 ### get key for dictionary item with the highest value
        highest_count_layer = max(results_dict, key=results_dict.get)
        print(highest_count_layer)
        print(results_dict[highest_count_layer])
        #print(results_dict)
                

        for row in searchCursor:
            Symbology_Layer = highest_count_layer
            sym = Symbology_Layer.symbology
            
            if hasattr(sym, 'renderer'):
                if sym.renderer.type == 'SimpleRenderer':
                    sym.updateRenderer('GraduatedColorsRenderer')
                    sym.renderer.classificationField = 'SUM_USER_VisitCount'
                    sym.renderer.breakCount = 5
                    sym.renderer.colorRamp = p5.listColorRamps('Orange-Red (Continuous)')[0]
                    ### Remove decimal places from labels
                    if sym.renderer == "GraduatedColorsRenderer":
                        breaks = sym.renderer.classBreaks
                        for b in breaks:
                            b_int = b.label[0:-7]
                            b.label = b_int
                            ### Thousands Separators in labels
                            def FindLabel (b):
                                return "{:,}".format(float(b))
                    
                    
                    
                    Symbology_Layer.symbology = sym
                    
                    p5.save()
                    
                    ### Where lyrx will be saved
                    arcpy.env.workspace = r"E:\arcGIS_Shared\Python\Symbology Layers"
                    
                    ### Set local variables
                    in_layer = Symbology_Layer
                    out_layer_file = r"E:\arcGIS_Shared\Python\Symbology Layers\SGF_CumulativeSymbology.lyrx"
                    
                    arcpy.management.SaveToLayerFile(in_layer, out_layer_file, "ABSOLUTE")
                    
                    print('Lyrx file saved')
0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus

places where a 'skip' can occur are lines 12, 13 and 19

why don't you throw a print statement before those lines and get the condition before it enters the 'if'.

otherwise add an 'else' so that it does something (even a print) to report what it got when the condition failed

Business_IntelligenceSoftware
Regular Contributor

Hey Dan,

Thanks for responding. I added some print statement like you suggested, and it looks like the script runs fine until line 41. I also got the following error:

I think the reason why my code was being "skipped" earlier was because it is meant to be run an a FC with single symbol symbology, and I had forgotten to reset it to that when I was testing. 

0 Kudos
DanPatterson_Retired
MVP Emeritus

That's good you found it... print is your friend and skip try, except blocks, a good old error message is just as informative.