Restricting the list of layers

In the previous recipe, you learned how to get a list of layers by using the ListLayers() function. There will be times when you will not want a list of all the layers in a map document, but rather only a subset of the layers. The ListLayers() function allows you to restrict the list of layers that is generated. In this recipe, you will learn how to restrict the layers returned using a wildcard and a specific data frame from the ArcMap table of contents.

Getting ready

By default, if you only pass a reference to the map document or layer file, the ListLayers() function will return a list of all the layers in these files. However, you can restrict the list of layers returned by this function by using an optional wildcard parameter or by passing in a reference to a specific data frame. A wildcard is a character that will match any character or sequence of characters in a search. This will be demonstrated in this recipe.

Note

If you're working with a layer file (.lyr), you can't restrict layers with a data frame. Layer files don't support data frames.

In this recipe, you will learn how to restrict the list of layers returned by ListLayers() through the use of a wildcard and data frame.

How to do it…

Follow these steps to learn how to restrict a list of layers from a map document:

  1. Open c:ArcpyBookCh2Crime_Ch2.mxd with ArcMap.
  2. Click on the Python window button from the main ArcMap toolbar.
  3. Import the arcpy.mapping module:
    import arcpy.mapping as mapping
  4. Reference the currently active document (Crime_Ch2.mxd) and assign the reference to a variable:
    mxd = mapping.MapDocument("CURRENT")
  5. Get a list of data frames in the map document and search for a specific data frame named Crime (please note that text strings can be surrounded by either single or double quotation marks):
    for df in mapping.ListDataFrames(mxd):
      if df.name == 'Crime':
  6. Call the ListLayers() function and pass a reference to the map document, a wildcard to restrict the search, and the data frame found in the last step to further restrict the search. The ListLayers() function should be indented inside the if statement you just created:
    layers = mapping.ListLayers(mxd,'Burg*',df)
  7. Start a for loop and print out the name of each layer in the map document:
    for layer in layers:
        print(layer.name)
  8. The complete script should appear as follows or you can consult the solution file at c:ArcpyBookcodeCh2RestrictLayers.py:
    How to do it…
  9. Run the script to see the following output:
    Burglaries in 2009
    

How it works…

The ListDataFrames() function is another list function that is provided by arcpy.mapping. This function returns a list of all the data frames in a map document. We then loop through each of the data frames returned by this function, looking for a data frame that has the name Crime. If we do find a data frame that has this name, we call the ListLayers() function, passing in the optional wildcard value of Burg* as the second parameter, and a reference to the Crime data frame. The wildcard value passed in as the second parameter accepts any number of characters and an optional wildcard character (*).

In this particular recipe, we searched for all the layers that begin with the characters Burg and have a data frame named Crime. Any layers found matching these restrictions were then printed. Keep in mind that all we did in this case was print the layer names, but in most cases, you would be performing additional geoprocessing with the use of tools or other functions, and having a shorter list will speed up your script and will keep things neat and tidy.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset