Select to view content in your preferred language

Determining Selected Layers in the Table of Contents with ArcPy

3996
5
Jump to solution
10-07-2013 11:16 AM
TomFlahive
Occasional Contributor
Is there a way to determine which layers are selected in the Table of Contents using ArcPy?  Lets say you drag in 10 shapefiles into ArcMap, and then you select 4 of the 10 layers using the mouse cursor (by holding down the Ctrl key to select multiple layers).  Is there a way with ArcPy to determine if a layer is selected or not?  What I would like to do is to write a script that loops through all the layers in the Table of Contents, and if a layer is selected, the script will add a particular field to that layer.  So I need a way of determining whether a layer is selected or not.  Thanks.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor
Hi Tom,

When accessing a layer you will not have information if the layer is selected or not. A map document does not need to be open to be accessed by Python code. That's why there is no "selected" property on layers. Instead you may want to use the "visible" property of the Layer (arcpy.mapping) object. This enable you to detect whether the layer is switched on or off.

Layer.visible:
Controls the display of a layer. This has the same effect as checking the check box next to the layer in the table of contents in ArcMap. If set to True, the layer will draw; if set to False, the layer will not be drawn. Not all layers support the visible property (for example, restricted web service layers), so it is good practice to test for this ahead of time using the supports method.

Kind regards,

Xander Bakker

View solution in original post

0 Kudos
5 Replies
JohnDye
Deactivated User
There's no simple function to simply return a list of layers with a selection on them, but you could probably manage this by looping through the layers in the dataframe, getting the layers extent, then getting the layer's selected extent and comparing the two, and if the extents differ, then there is a selection applied to the layer and you can then append it to a list.

I haven't tested this and I'm just writing this off the top of my head, but it should give you an idea of where to go with this:
mxd = arcpy.mapping.MapDocument("Current")
df = arcpy.mapping.ListDataFrames(mxd)[0]
TOCLayerList = arcpy.mapping.ListLayers(mxd, "*", df)

SelectedLayerList = []

for lyr in TOCLayerList:
    lyrExtent = lyr.getExtent()
    lyrSelectedExtent = lyr.getSelectedExtent()
    if not lyrExtent == lyrSelectedExtent:
        SelectedLayerList.append(lyr)
print SelectedLayerList


Again, I haven't tested this or played with it at all, but conceptually this is the best path I can think of to return only those layers with a selection applied. I'm not sure what the Layer method 'getSelectedExtent()' would return if there was no selection applied to a given layer, so you should toy with that as well and figure out what the return is and make a consideration for that in the logic so that you don't get any false positives.

You should also note that if you plan to do this on an MXD with a large amount of layers in it, it could take a fair amount of time to complete.

Hope this helps.
0 Kudos
XanderBakker
Esri Esteemed Contributor
Hi Tom,

When accessing a layer you will not have information if the layer is selected or not. A map document does not need to be open to be accessed by Python code. That's why there is no "selected" property on layers. Instead you may want to use the "visible" property of the Layer (arcpy.mapping) object. This enable you to detect whether the layer is switched on or off.

Layer.visible:
Controls the display of a layer. This has the same effect as checking the check box next to the layer in the table of contents in ArcMap. If set to True, the layer will draw; if set to False, the layer will not be drawn. Not all layers support the visible property (for example, restricted web service layers), so it is good practice to test for this ahead of time using the supports method.

Kind regards,

Xander Bakker
0 Kudos
JeffBarrette
Esri Regular Contributor
This function can be found in the pythonaddins module.  Addins include many additional features for building event driven applications, and as previously mentioned, the ArcMap application would need to be open.

import pythonaddins
mxd = arcpy.mapping.MapDocument("current")
lyr = pythonaddins.GetSelectedTOCLayerOrDataFrame()
print lyr.name


Jeff
0 Kudos
XanderBakker
Esri Esteemed Contributor
Hi Tom,

Jeffrey points out correctly the additional functionality offered by the pythonaddins module. However it will return the selected dataframe or layer. In case more than 1 layer is selected the result is None. This wouldn't work for your purpose.

Kind regards,

Xander Bakker
0 Kudos
TomFlahive
Occasional Contributor
Thanks Xander for the insights.  Looks like I won't be able to do exactly what I was hoping to do, but perhaps the Layer.visible check will allow me to create a close work-around.  Thanks Jeffrey as well.  I had looked into the GetSelectedTOCLayerOrDataFrame() method, but as Xander says, it doesn't work when multiple layers are selected.

Tom
0 Kudos