geospatialesriarcpy

Using ArcPy to set visibility range not working in ArcPro 2.8.8


I need to automate the configuration of properties for a large number of Feature Layers that are based on queries of a base Filegeodatabase in ArcPro version 2.8.8. The first part of the process has gone smoothly where I add a series of feature layers using - arcpy.management.MakeFeatureLayer. The layers are named and limited based on a query which is working really well.

For each layer I need to set appropriate display scale so can do this manually in - Properties\General\In beyond (maximum scale) and Out beyond (minimum scale)

To set this programmatically it looks like <layer name>.minScale and <layer name>.maxScale should work with scale as integers.

The following code show how this is not working - note the test geodatabase is constructed in path is c:\tmp\

import arcpy
import os

# Define paths and names
workspace = r"C:\tmp\ExampleGeodatabase2.gdb"
feature_class_name = "ExampleFeatureClass"

# Create a new file geodatabase
if not arcpy.Exists(workspace):
    arcpy.CreateFileGDB_management(os.path.dirname(workspace), os.path.basename(workspace))

# Set the workspace environment
arcpy.env.workspace = workspace

# Define the path for the new feature class
feature_class_path = os.path.join(workspace, feature_class_name)

# Create a simple feature class with a point geometry
if not arcpy.Exists(feature_class_path):
    arcpy.CreateFeatureclass_management(workspace, feature_class_name, "POINT", spatial_reference=arcpy.SpatialReference(4326))

# Add a field to the feature class
arcpy.AddField_management(feature_class_path, "Name", "TEXT")

# Create a new ArcGIS Pro project and map
aprx = arcpy.mp.ArcGISProject("CURRENT")
map_obj = aprx.listMaps()[0]

# Add the feature class to the map
layer = map_obj.addDataFromPath(feature_class_path)

# Set the desired scale range
min_scale = 100000  # Maximum scale (1:100,000)
max_scale = 1000    # Minimum scale (1:1,000)

# Set the scale range for visibility if the layer is a FeatureLayer
if layer.isFeatureLayer:
    layer.minScale = min_scale
    layer.maxScale = max_scale
    print(f"Set scale range for '{layer.name}' to minScale: {min_scale}, maxScale: {max_scale}")
else:
    print(f"'{layer.name}' is not a FeatureLayer and cannot have scale ranges set.")

# Save changes to the project
aprx.save()

print("Feature class created, added to the map, and scale range set.")

If this was working, Properties/General should show the scale range set however I only see "None"

I've tried the range as decimals so 0.0001 and text in the form 1:100 and so far the visibility is not being set.

I've resorted to using GPT to write versions however so far it always completes with no errors however also does not set the scale interval.

Any help would be most appreciated as I have not been able to find anything via search and 4 hours of testing different approaches is leaving me with no result.

Thanks Bevan


Solution

  • Looking at the documentation, the layer class does not have minScale and maxScale properties, but minThreshold and maxThreshold, which do exactly what you need.

    You just have top replace your code

    if layer.isFeatureLayer:
        layer.minScale = min_scale
        layer.maxScale = max_scale
    

    with this:

    if layer.isFeatureLayer:
        layer.minThreshold = min_scale
        layer.maxThreshold = max_scale