How to determine True visibility of Layer in a map?

1089
3
Jump to solution
09-19-2012 01:33 PM
RyanKelley
New Contributor II
Before I start trying to write a bunch of code... has anyone figured out how to really determine whether a layer is visible or not in a map? For example, visible (checked) layers within a group layer that is not visible (unchecked) are not truly visible in an MXD. 
for lyr in lyrList:      return lyr.visible 


Is there any way to determine a relationship between a layer and it's parent group it resides in? 

The point of this is so we can give a report to the cartography dept and the report will tell them which layers were used, that were truly visible, not just "checked."  They make their maps from a list of data sources and layer files, not necessarily the MXD. 

Thanks,
Ryan
0 Kudos
1 Solution

Accepted Solutions
JeffBarrette
Esri Regular Contributor
Hello Ryan,

What a great question and something I never considered until just now.  I think the only way to test this is to use the Layer.longName property.  I hacked together some code below.

I have a function called "IsLayerReallyVisible".  I split the longName into a list (of each layer name in the group layer path) and then test each individual layer name.  If I find a False, I immediately return that value and exit the function.

import arcpy  def IsLayerReallyVisible(mxd, layer):   lyrNameList = layer.longName.split("\\")   for lyrName in lyrNameList:     lyr = arcpy.mapping.ListLayers(mxd, lyrName)[0]     if not lyr.visible:       return False   return True   mxd = arcpy.mapping.MapDocument("current") lyr = arcpy.mapping.ListLayers(mxd, "US_States")[0] print IsLayerReallyVisible(mxd, lyr)


I'd like to learn more about your workflows and if you think it would be necessary to add an enhancement to the API to make this a little more straight forward - or is the code above good enough.

Jeff

View solution in original post

0 Kudos
3 Replies
JeffBarrette
Esri Regular Contributor
Hello Ryan,

What a great question and something I never considered until just now.  I think the only way to test this is to use the Layer.longName property.  I hacked together some code below.

I have a function called "IsLayerReallyVisible".  I split the longName into a list (of each layer name in the group layer path) and then test each individual layer name.  If I find a False, I immediately return that value and exit the function.

import arcpy  def IsLayerReallyVisible(mxd, layer):   lyrNameList = layer.longName.split("\\")   for lyrName in lyrNameList:     lyr = arcpy.mapping.ListLayers(mxd, lyrName)[0]     if not lyr.visible:       return False   return True   mxd = arcpy.mapping.MapDocument("current") lyr = arcpy.mapping.ListLayers(mxd, "US_States")[0] print IsLayerReallyVisible(mxd, lyr)


I'd like to learn more about your workflows and if you think it would be necessary to add an enhancement to the API to make this a little more straight forward - or is the code above good enough.

Jeff
0 Kudos
RyanKelley
New Contributor II
This works just fine IF layers are uniquely named.  It would make sense to me to incorporate this all into the API so we don't all have to write out error trapping for dup names.

thank you as always,
ryan
0 Kudos
GraemeBrowning
Occasional Contributor III
Many thanks for this code which I used today.

In the short term, I think the Layer (arcpy.mapping) documentation should be enhanced to mention that the visible property result may be misleading when dealing with Layer Groups, and to have this code mentioned as a workaround.

In the medium term, perhaps there can be a new checkedOn property added to return True or False irrespective of the Layer Group setting, and the visible property behaviour modified to reflect whether the layer really is visible.
0 Kudos