
Getting a list of layers in a map document
Frequently, one of the first steps in a geoprocessing script is to obtain a list of layers in the map document. Once obtained, your script may then cycle through each of the layers and perform some type of processing. The mapping module contains a ListLayers()
function, which provides the capability of obtaining this list of layers. In this recipe, you will learn how to get a list of layers contained within a map document.
Getting ready
The arcpy.mapping
module contains various list functions to return lists of layers, data frames, broken data sources, table views, and layout elements. These list functions normally function as the first step in a multistep process, in which the script needs to get one or more items from a list for further processing. Each of these list functions returns a Python list, which, as you know from earlier in the book, is a highly functional data structure for storing information.
Normally, the list functions are used as a part of a multistep process, in which creating a list is only the first step. Subsequent processing in the script will iterate over one or more of the items in this list. For example, you might obtain a list of layers in a map document and then iterate through each layer looking for a specific layer name, which will then be subjected to further geoprocessing.
In this recipe, you will learn how to obtain a list of layers from a map document file.
How to do it…
Follow these steps to learn how to get a list of layers from a map document:
- Open
c:\ArcpyBook\Ch2\Crime_Ch2.mxd
with ArcMap. - Click on the Python window button from the main ArcMap toolbar.
- Import the
arcpy.mapping
module:import arcpy.mapping as mapping
- Reference the currently active document (
Crime_Ch2.mxd
) and assign the reference to a variable:mxd = mapping.MapDocument("CURRENT")
- Call the
ListLayers()
function and pass a reference to the map document:layers = mapping.ListLayers(mxd)
- Start a
for
loop and print out the name of each layer in the map document:for lyr in layers: print(lyr.name)
- Run the script to see the following output (you can check your work by examining the
c:\ArcpyBook\code\Ch2\GetListLayers.py
solution file):Burglaries in 2009 Crime Density by School District Bexar County Boundary Test Performance by School District Bexar County Boundary Bexar County Boundary Texas Counties School_Districts Crime Surface Bexar County Boundary
How it works…
The ListLayers()
function retrieves a list of layers in a map document, specific data frame, or layer file. In this case, we passed a reference to the current map document to the ListLayers()
function, which should retrieve a list of all the layers in the map document. The results are stored in a variable called layers
, which is a Python list that can be iterated with a for
loop. This Python list contains one or more Layer
objects.
There's more…
The ListLayers()
function is only one of the many list functions provided by the arcpy.mapping
module. Each of these functions returns a Python list containing data of some type. Some of the other list functions include ListTableViews()
, which returns a list of Table
objects; ListDataFrames()
, which returns a list of DataFrame
objects; and ListBoomarks()
, which returns a list of bookmarks in a map document. There are additional list functions, many of which we'll cover later in this book.