Change layer visibility range with arcpy in ArcPro

592
4
Jump to solution
12-11-2023 11:04 AM
cweirpatrickco
New Contributor II

I'm trying to change feature layer visibility ranges with Arcpy in ArcPro but am running into issues. For some reason all of the layers don't support scale ranges despite them being feature layers in the TOC. I can change the visibility ranges manually in Pro. The code below runs with no errors, but none of the visibility ranges change.

code.jpg

 

This is what is printed after running the code...

print_message.jpg

 

What am I doing wrong? How can I make this happen?

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

have you tried without the try except block?  I'm not sure of a layer property VISIBLESCALE so that may be forcing it into the except.  Your error message may be misleading by that logic.

Is this ArcMap or Pro? it might be min and maxThreshold properties now.

View solution in original post

4 Replies
DavidPike
MVP Frequent Contributor

have you tried without the try except block?  I'm not sure of a layer property VISIBLESCALE so that may be forcing it into the except.  Your error message may be misleading by that logic.

Is this ArcMap or Pro? it might be min and maxThreshold properties now.

cweirpatrickco
New Contributor II

Removing lyr.supports("VISIBLESCALE") and switching minScale and maxScale to minThreshold and maxThreshold was the fix! ESRI Documentation

Here's the working code:


working_code.jpg

 
Thanks @DavidPike!

Luke_Pinner
MVP Regular Contributor

Screenshots of code are pretty much useless unless the issue is really obvious and the code is short. Please post code as text in future.

How to insert code in your post 

cweirpatrickco
New Contributor II

Yeah, I was originally going to but it started giving me trouble about it being HTML so I resorted to screenshots.

Thanks for the link though.

Here's the working code snippet:

def set_visibility_range(lyr, min_threshold, max_threshold):
    if lyr.isFeatureLayer:
        lyr.visible = True
        lyr.minThreshold = min_threshold
        lyr.maxThreshold = max_threshold
    else:
        print(f"'{lyr.name}' does not support scale ranges")


for lyr in m.listLayers():
    if "Primary" in lyr.name:
        set_visibility_range(lyr, 300000, 0)
    elif "Secondary" in lyr.name:
        set_visibility_range(lyr, 2500, 0)
    elif "Pole" in lyr.name and "Anno" not in lyr.name:
        set_visibility_range(lyr, 5000, 0)
    elif "Anno" in lyr.name:
        set_visibility_range(lyr, 2500, 0)
    else:
        set_visibility_range(lyr, 0, 0)

aprx.save()

print("FC visibility ranges set...")
0 Kudos