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

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.

主站蜘蛛池模板: 神农架林区| 家居| 肥西县| 布拖县| 建始县| 铁力市| 桃园县| 卢氏县| 福海县| 潍坊市| 阿坝县| 乐平市| 昌都县| 定远县| 元阳县| 万载县| 泾川县| 阜南县| 光山县| 宜良县| 东乡族自治县| 遂宁市| 泽库县| 故城县| 天镇县| 甘孜县| 正安县| 班戈县| 胶州市| 呼伦贝尔市| 东海县| 丰镇市| 玉田县| 昭苏县| 日土县| 万安县| 正镶白旗| 喀喇| 南雄市| 手游| 霍州市|