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

Accessing a data frame

The ArcMap table of contents is composed of one or more data frames. Each data frame can contain layers and tables. Data frames can be used to filter lists that are returned from the various list functions such as ListLayers(). For example, a DataFrame object can be used as an input parameter to restrict the layers returned by the ListLayers() function to only those layers within a particular data frame. You can also use a DataFrame object to get or set the current map extent, which can be useful when creating map books. In this recipe you will learn how to access data frames from your Python scripts.

Getting ready

The ListDataFrames() function returns a list of DataFrame objects. Each data frame can hold layers and tables and can be used to restrict the lists returned by the ListLayers() and ListTablesViews() functions.

How to do it...

Follow the steps below 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 ListDataFrames() function and pass a reference to the map document along with a wildcard used to find only the data frames that begin with the letter C:
    frames = mapping.ListDataFrames(mxd,"C*")
  6. Start a for loop and print out the name of each layer in the map document:
    for df in frames:
        print df.name
  7. Run the script to see the following output:
    Crime
    Crime_Inset
    

How it works...

The ListDataFrames() function returns a list of all the data frames in the ArcMap table of contents. Like any other Python list, you can iterate through the contents of the list using a for loop. Inside the for loop, each data frame is dynamically assigned to the df variable and the name of the data frame is printed to the screen.

主站蜘蛛池模板: 孟州市| 荣成市| 平江县| 阳曲县| 定远县| 南平市| 建始县| 开平市| 临武县| 云梦县| 济源市| 南城县| 中牟县| 烟台市| 张家口市| 朔州市| 甘孜| 大竹县| 兴山县| 沁源县| 忻城县| 嘉义县| 奇台县| 蒙阴县| 湟中县| 奉新县| 龙里县| 广灵县| 汤阴县| 榕江县| 九龙坡区| 新闻| 上饶县| 冕宁县| 霍城县| 凤城市| 阜宁县| 巨野县| 庄浪县| 红河县| 鹤庆县|