ArcGIS for Desktop

  • 文档
  • 合约
  • 支持

  • My Profile
  • 帮助
  • Sign Out
ArcGIS for Desktop

ArcGIS Online

专为贵组织打造的制图平台

ArcGIS for Desktop

全面的专业性 GIS

ArcGIS for Server

面向企业的 GIS

ArcGIS for Developers

用于构建位置感知应用程序的工具

ArcGIS Solutions

适用于行业的免费模板地图和应用程序

ArcGIS Marketplace

获取适用于组织的应用程序和数据

  • 文档
  • 合约
  • 支持
Esri
  • 登录
user
  • 我的个人资料
  • 登出

帮助

  • 主页
  • 入门
  • 制图
  • 分析
  • 管理数据
  • 工具
  • 更多...

ListLayers

  • 摘要
  • 讨论
  • 语法
  • 代码实例

摘要

Returns a Python list of Layer objects that exist within a map document (.mxd), a data frame within a map document, or layers within a layer (.lyr) file.

讨论

ListLayers always returns a Python list object even if only one layer is returned. In order to return a Layer object, an index value must be used on the list (e.g., lyr = arcpy.mapping.ListLayers(mxd)[0]). For loops on a list provide an easy mechanism to iterate through each item in a list (e.g., for lyr in arcpy.mapping.ListLayers(mxd):).

When working with layer files, the data_frame parameter should not be used because layer files don't support data frames; if it is, it will be ignored. The Layer() function is for referencing layer (.lyr) files stored on disk.

Group layers are treated just like layers. The index values are simply generated from top to bottom as they appear in the table of contents or the way they would appear in a layer file. The same applies if a group layer is within another group layer. A map document with a single group layer with three layers within it will return a Python list of four layer objects, the group layer being the first. One way of determining if a layer is inside a group layer is to interrogate the longName property. A layer's longName will include the group layer name as part of the name.

Wildcards are used on the name property and are not case sensitive. A wildcard string of "so*" will return a layer with the name Soils. Wildcards can be skipped in the scripting syntax simply by passing an empty string (""), an asterisk (*), or entering wildcard=None, or nothing at all if it is the last optional parameter in the syntax.

It is possible that there might be layers in a map document or layer file that have the same name. If that is the case, then other properties may need to be used to isolate a specific layer. Properties such as a layer's description or a layer's dataSource could be used to do this. It is ideal that all layers in a map document or at least in a layer be uniquely named.

语法

ListLayers (map_document_or_layer, {wildcard}, {data_frame})
参数说明数据类型
map_document_or_layer

A variable that references a MapDocument or Layer object.

Object
wildcard

A combination of asterisks (*) and characters can be used to help limit the results.

(默认值为 None)

String
data_frame

A variable that references a DataFrame object.

(默认值为 None)

DataFrame

返回值

数据类型说明
Layer

A Python list of Layer objects.

代码实例

ListLayers example 1:

This script will print the name of the first layer in a data frame named Traffic Analysis. The wildcard parameter is skipped using a blank string.

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Traffic Analysis")[0]
print arcpy.mapping.ListLayers(mxd, "", df)[0].name
del mxd
ListLayers example 2:

The following script will find a layer called Lakes in a data frame named County Maps, turn the layer on (to be visible) and set the transparency to 50%. An issue is that there happens to be two layers with the same name in the same data frame so the description property is used to further isolate the layer of interest. Ideally, all layers would have a unique name but that isn't always the case so other properties have to be used to isolate the layer of interest. In this example, the description is used.

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "County Maps")[0]
for lyr in arcpy.mapping.ListLayers(mxd, "Lakes", df):
    if lyr.description == "USGS Lakes":
        lyr.visible = True
        lyr.transparency = 50
mxd.save()
del mxd
有关此主题的反馈?

ArcGIS for Desktop

  • 主页
  • 文档
  • 合约
  • 支持

ArcGIS 平台

  • ArcGIS Online
  • ArcGIS for Desktop
  • ArcGIS for Server
  • ArcGIS for Developers
  • ArcGIS Solutions
  • ArcGIS Marketplace

关于 Esri

  • 关于我们
  • 招贤纳士
  • 内部人员博客
  • 用户大会
  • 开发者峰会
Esri
© Copyright 2016 Environmental Systems Research Institute, Inc. | 隐私政策 | 法律声明