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

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 might then cycle through each of the layers and perform some type of processing. The mapping module contains a ListLayers() function that 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 are normally the first step in a multi-step process, where 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 part of a multi-step process, where 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 subject 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:

  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. Call the ListLayers() function and pass a reference to the map document:
    layers = mapping.ListLayers(mxd)
  6. Start a for loop and print out the name of each layer in the map document:
    for lyr in layers:
        print lyr.name
  7. Run the script to see the following output:
    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, a specific data frame, or a layer file. In this case, we passed a reference to the current map document to the ListLayers() function, which will retrieve a list of all layers in the map document. The results will be 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 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 ListBookmarks() which returns a list of bookmarks in a map document. There are additional list functions, many of which we'll cover later in the book.

主站蜘蛛池模板: 台东市| 凌云县| 玉门市| 常州市| 渑池县| 永济市| 沙雅县| 奉新县| 南雄市| 子洲县| 醴陵市| 泾川县| 娄烦县| 宜阳县| 石嘴山市| 蒙阴县| 康定县| 尼木县| 雷山县| 湾仔区| 五台县| 余江县| 都昌县| 吉隆县| 阿坝| 崇文区| 麦盖提县| 禄劝| 康马县| 方城县| 拜泉县| 唐山市| 丰原市| 沙洋县| 南丰县| 玛纳斯县| 富平县| 安阳县| 天津市| 五原县| 桃园县|