- 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.
- 軟件測試工程師面試秘籍
- 零基礎(chǔ)學(xué)MQL:基于EA的自動化交易編程
- 微信小程序開發(fā)解析
- FLL+WRO樂高機(jī)器人競賽教程:機(jī)械、巡線與PID
- Hands-On Full Stack Development with Go
- HTML5與CSS3基礎(chǔ)教程(第8版)
- Access 2010數(shù)據(jù)庫應(yīng)用技術(shù)(第2版)
- C語言從入門到精通
- 從零開始:UI圖標(biāo)設(shè)計(jì)與制作(第3版)
- HTML+CSS+JavaScript網(wǎng)頁制作:從入門到精通(第4版)
- 石墨烯改性塑料
- Magento 2 Beginners Guide
- Data Manipulation with R(Second Edition)
- MATLAB 2020 GUI程序設(shè)計(jì)從入門到精通
- 小學(xué)生C++趣味編程從入門到精通