官术网_书友最值得收藏!

Restricting the list of layers

In the previous recipe, you learned how to get a list of layers 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 through the use of an optional wildcard parameter or by passing in a reference to a specific data frame.

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:\ArcpyBook\Ch3\Crime_Ch3.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_Ch3.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 name of Crime. Please note that text strings can be surrounded by either single or double quotes:
    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:
    import arcpy.mapping as mapping
    mxd = mapping.MapDocument("CURRENT")
    for df in mapping.ListDataFrames(mxd):
      if (df.name == 'Crime'):
            layers = mapping.ListLayers(mxd,"'Burg*',df)
            for layer in layers:
          print layer.name
  9. Run the script to see the following output:
    Burglaries in 2009
    

How it works…

As you learned in a previous recipe, the ListDataFrames() function is another list function 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're searching for all the layers that begin with the characters Burg and that have a data frame name of Crime. Any layers found matching these restrictions are then printed. Keep in mind that all we're doing in this case is printing the layer names, but in most cases, you would be performing additional geoprocessing through the use of tools or other functions.

主站蜘蛛池模板: 昌宁县| 宁河县| 聂荣县| 江川县| 斗六市| 文登市| 天柱县| 乌拉特后旗| 永吉县| 合江县| 白水县| 襄樊市| 吐鲁番市| 天台县| 马龙县| 宜章县| 西盟| 玉树县| 冀州市| 鄂州市| 屯昌县| 社旗县| 东明县| 蕲春县| 石台县| 柳林县| 大兴区| 手游| 道孚县| 新密市| 准格尔旗| 同德县| 德化县| 河北区| 鹿泉市| 宜宾县| 宝山区| 太康县| 武威市| 贵溪市| 吴旗县|