- Programming ArcGIS 10.1 with Python Cookbook
- Eric Pimpler
- 595字
- 2021-07-30 17:29:57
Restricting the list of layers
In the previous recipe, you learned how to get a list of layers using the ListLayers()
function. There will be times when you will not want a list of all the layers in a map document, but rather only a subset of the layers. The ListLayers()
function allows you to restrict the list of layers that is generated. In this recipe, you will learn how to restrict the layers returned using a wildcard and a specific data frame from the ArcMap table of contents.
Getting ready
By default, if you only pass a reference to the map document or layer file, the ListLayers()
function will return a list of all the layers in these files. However, you can restrict the list of layers returned by this function through the use of an optional wildcard parameter or by passing in a reference to a specific data frame.
In this recipe, you will learn how to restrict the list of layers returned by ListLayers()
through the use of a wildcard and data frame.
How to do it…
Follow these steps to learn how to restrict 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")
- Get a list of data frames in the map document and search for a specific data frame name of
Crime
. Please note that text strings can be surrounded by either single or double quotes:for df in mapping.ListDataFrames(mxd): if (df.name == 'Crime'):
- Call the
ListLayers()
function and pass a reference to the map document, a wildcard to restrict the search, and the data frame found in the last step to further restrict the search. TheListLayers()
function should be indented inside theif
statement you just created:layers = mapping.ListLayers(mxd,'Burg',df)
- Start a
for
loop and print out the name of each layer in the map document.for layer in layers: print layer.name
- The complete script should appear as follows:
import arcpy.mapping as mapping mxd = mapping.MapDocument("CURRENT") for df in mapping.ListDataFrames(mxd): if (df.name == 'Crime'): layers = mapping.ListLayers(mxd,"'Burg*',df) for layer in layers: print layer.name
- Run the script to see the following output:
Burglaries in 2009
How it works…
As you learned in a previous recipe, the ListDataFrames()
function is another list function provided by arcpy mapping
. This function returns a list of all the data frames in a map document. We then loop through each of the data frames returned by this function, looking for a data frame that has the name Crime
. If we do find a data frame that has this name, we call the ListLayers()
function, passing in the optional wildcard value of Burg*
as the second parameter, and a reference to the Crime
data frame. The wildcard value passed in as the second parameter accepts any number of characters and an optional wildcard character (*
).
In this particular recipe, we're searching for all the layers that begin with the characters Burg
and that have a data frame name of Crime
. Any layers found matching these restrictions are then printed. Keep in mind that all we're doing in this case is printing the layer names, but in most cases, you would be performing additional geoprocessing through the use of tools or other functions.
- Power Up Your PowToon Studio Project
- Mastering QGIS
- Java程序設計與計算思維
- Python Tools for Visual Studio
- 數據結構(Python語言描述)(第2版)
- Functional Kotlin
- TypeScript項目開發實戰
- 小學生C++創意編程(視頻教學版)
- C語言程序設計上機指導與習題解答(第2版)
- Swift 4 Protocol-Oriented Programming(Third Edition)
- Android嵌入式系統程序開發:基于Cortex-A8(第2版)
- 鴻蒙OS應用編程實戰
- Android Game Programming by Example
- 征服C指針(第2版)
- 企業級Java現代化:寫給開發者的云原生簡明指南