Select to view content in your preferred language

Apply minScale/maxScale of group layer to all nested layers

658
4
05-20-2022 07:01 AM
AlanDodson
New Contributor III

Can someone help me to solve this arcpy problem? I'm using ArcGIS Pro 2.5 if that helps.

An example of my issue:

I have 20 group layers. Inside each group layer are 20 feature layers (all vector). The grouping only goes one level deep (Group layer Roads --> Feature layer Highways, for example). Values for minScale and maxScale have been applied to the top-level group layers (which control visibility for everything in the group), but not to the feature layers within the group. Using arcpy I'd like to get the minScale and maxScale values from the top-level group and set those values to all feature layers within that group. A bonus would be if there was some operation that could comprehend what higher group layer is controlling the minScale / maxScale of a feature layer and apply that, but for now the rigid structure outlined above with just two levels will do.

Can someone help me organize the logic and code around this problem?

0 Kudos
4 Replies
RhettZufelt
MVP Notable Contributor

You can get and set these in the lyr properties of the mapping module.

You could get the current min/maxThreshold settings from the group, then apply to the layers within it.

Not sure why you would, as it will have no effect at all as setting the visibility range on the FC's in a group to the same visibility range as the group is redundant.

R_

0 Kudos
AlanDodson
New Contributor III

Yes, fully aware of how to get and set the scale thresholds through the mapping module for individual layers. What I can't figure out is how to apply the thresholds from the group layer to every feature layer that falls within that group layer. I hope that clarifies. Any advice?

As for why I'm attempting this - One reason (aside from being told to do so by a boss 😉 ) is that I have a requirement to make these projects more bulletproof so that if a layer gets moved out of place it's still being displayed correctly. I also have to output a config file that shows the properties of every layer and this is how I envision making the scales of grouped layers more easy to understand for some users.

0 Kudos
RhettZufelt
MVP Notable Contributor

Something like this should get you started:

import arcpy
aprx = arcpy.mp.ArcGISProject("Current")
m = aprx.listMaps("Map")[0]
minscale = 0
maxscale = 0
for lyr in m.listLayers():

    if lyr.isGroupLayer:
        minscale = lyr.minThreshold
        maxscale = lyr.maxThreshold
    else:
        lyr.minThreshold = minscale
        lyr.maxThreshold = maxscale

Not sure your setup, would recommend testing thoroughly on a copy as I have not tested this for anything outside of  group layers.

However, I have several groups of layers,  The listLayers() gets all of the layers in the TOC (in order), even the "group" layer, and lists the group layer first.

If the  layer "is Group Layer", then set the min and max scale from the current settings for the group.

If not "is Group Layer" (which is false for layers "inside" the group.  (only reports True for the Group name itself), set the min/max threshold values extracted from the group.

When it finds the next group layer, resets the min/max and applies them to the rest of the layers (at least, until a group layer is found again).

The documentation says that m.listLayers() will create a list of all the layers in a group that might give you more control.  However, I can't figure out how to get it to limit to layers inside a group.  Always give me everything in the TOC.

can also use lyr.longName as a test for grouped layers as if lyr.name == lyr.longName, it is not grouped if you need more control.  The first section of the docs cover this pretty well.

R_

 

 

AlanDodson
New Contributor III

Ah, that logic makes total sense when I see it like this.  I haven't had the opportunity to test this out yet but will do so ASAP. Will do some testing with longName as you suggested, my previous tries included this but didn't use the same logic as you did so might be an excellent building block. Thank you!  

0 Kudos