- Programming ArcGIS 10.1 with Python Cookbook
- Eric Pimpler
- 551字
- 2021-07-30 17:29:57
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:
- Open
c:\ArcpyBook\Ch3\Crime_Ch3.mxd
with ArcMap. - Click on the Python window button from the main ArcMap toolbar.
- Import the
arcpy.mapping
module:import arcpy.mapping as mapping
- Reference the currently active document (
Crime_Ch3.mxd
) and assign the reference to a variable:mxd = mapping.MapDocument("CURRENT")
- Call the ListLayers() function and pass a reference to the map document:
layers = mapping.ListLayers(mxd)
- Start a
for
loop and print out the name of each layer in the map document:for lyr in layers: print lyr.name
- 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.
- C# 2012程序設計實踐教程 (清華電腦學堂)
- 編程卓越之道(卷3):軟件工程化
- Mastering Selenium WebDriver
- Mastering Kali Linux for Web Penetration Testing
- Kali Linux Wireless Penetration Testing Beginner's Guide(Third Edition)
- D3.js 4.x Data Visualization(Third Edition)
- Java Fundamentals
- Django 3.0入門與實踐
- Cocos2d-x Game Development Blueprints
- ASP.NET Web API Security Essentials
- Java高手是怎樣煉成的:原理、方法與實踐
- Java多線程并發體系實戰(微課視頻版)
- Spring Boot 3:入門與應用實戰
- Web前端開發技術實踐指導教程
- Vue.js項目開發實戰