Skip to main content

addLayer

汇总

提供访问图层文件( .lyr.lyrx )中图层的访问权限和其他基本图层管理方法。

讨论

LayerFile 类提供的方法和属性提供对图层文件中包含的单个图层和表的访问权限。 图层文件可以包含一个或多个图层、表和组图层。

ArcGIS Pro 可读取 .lyr.lyrx 图层文件类型。 更改只能保存到 .lyrx 图层文件类型。 如果要更改 .lyr 文件,则必须调用 saveACopy 方法。

语法

addLayer()

属性

名称 说明 数据类型

filePath

(只读)

返回报告完全限定的图层文件路径和文件名的字符串值。

String

metadata

(读取和写入)

获取或设置图层文件的 元数据 类信息。请注意:设置元数据取决于 isReadOnly 属性值。

Metadata

version

(只读)

根据上次保存项目的时间返回版本字符串。它使用语义版本化格式:Major.Minor.Patch。

String

方法

addLayer(add_layer_or_layerfile, {add_position})

用于使用基本放置选项向图层文件 (.lyrx) 添加 LayerLayerFile

名称 说明 数据类型

add_layer_or_layerfile

LayerLayerFile 对象(表示要添加的单个或多个图层)的引用。

Layer

add_position

(可选)

此常数用于确定添加的单个或多个图层在图层文件中的放置位置。

  • AUTO_ARRANGE—基于图层权重规则和几何来自动放置单个或多个图层。

  • BOTTOM—在 TOC 图层堆叠底部放置单个或多个图层。

  • TOP—在 TOC 图层堆叠顶部放置单个或多个图层。

(默认值为 AUTO_ARRANGE)

String

返回值

数据类型 说明

List

Layer 对象的 Python 列表。

addLayerToGroup(target_group_layer, add_layer_or_layerfile, {add_position})

用于使用基本放置选项向图层文件 (.lyrx) 内的现有图层组添加 LayerLayerFile 的内容。

名称 说明 数据类型

target_group_layer

对现有 Layer 对象组的引用。

Layer

add_layer_or_layerfile

LayerLayerFile 对象(表示要添加的单个或多个图层)的引用。

Layer

add_position

(可选)

此常数用于确定在 target_group_layer 中添加的单个或多个图层的放置位置。

  • AUTO_ARRANGE—基于图层权重规则和几何来自动放置图层。

  • BOTTOM—在 TOC 图层堆叠底部放置图层。

  • TOP—在 TOC 图层堆叠顶部放置图层。

(默认值为 AUTO_ARRANGE)

String

insertLayer(reference_layer, insert_layer_or_layerfile, {insert_position})

用于通过指定位置向图层文件 (.lyrx) 添加 LayerLayerFile

名称 说明 数据类型

reference_layer

表示可确定新图层插入位置的现有图层的 Layer 对象。

Layer

insert_layer_or_layerfile

LayerLayerFile 对象(表示要添加的单个或多个图层)的引用。

Layer

insert_position

(可选)

此常数用于确定相对于 reference_layer 的单个或多个图层的放置位置。

  • AFTER—在参考图层之后或下方插入新图层。

  • BEFORE—在参考图层之前或上方插入新图层。

(默认值为 BEFORE)

String

listBrokenDataSources()

返回图层文件中已与其原始数据源断开连接的 Layer 对象的 Python 列表。

返回值

数据类型 说明

List

Layer 对象的 Python 列表。

listLayers({wildcard})

返回图层文件中 图层 的 Python 列表。

名称 说明 数据类型

wildcard

(可选)

A wildcard is based on the layer name and is not case sensitive. A combination of asterisks (*) and characters can be used to help limit the resulting list.

(默认值为 None)

String

返回值

数据类型 说明

List

图层文件中 图层 对象的 Python 列表。

listTables({wildcard})

返回存在于 LayerFile 中的 Table 对象的 Python 列表。

名称 说明 数据类型

wildcard

(可选)

A wildcard is based on the table name and is not case sensitive. A combination of asterisks (*) and characters can be used to limit the resulting list.

(默认值为 None)

String

返回值

数据类型 说明

List

LayerFile 中的 Table 对象的 Python 列表。

moveLayer(reference_layer, move_layer, {insert_position})

用于在图层堆叠中将图层文件中的图层或图层组移动到特定位置。

名称 说明 数据类型

reference_layer

表示可确定新图层移动位置的现有图层的 Layer 对象。

Layer

move_layer

Layer 对象(表示要移动的图层)的引用。

Layer

insert_position

(可选)

此常数用于确定移动的图层相对于参考图层的放置位置。

  • AFTER—将图层移动到参考图层的后面或下方。

  • BEFORE—将图层移动到参考图层的前面或上方。

(默认值为 BEFORE)

String

removeLayer(remove_layer)

用于从图层文件中移除图层。

名称 说明 数据类型

remove_layer

引用表示要移除的图层的 Layer 对象。

Layer

save()

保存对 .lyrx 文件的现有图层文件引用,即使引用的是 .lyr 文件也同样如此。

saveACopy(file_name)

LayerFile 保存到新路径或另存为新文件名。

名称 说明 数据类型

file_name

用于将 (LayerFile) 保存到新路径或另存为新文件名的字符串。只能保存到 .lyrx 文件。

String

updateConnectionProperties(current_connection_info, new_connection_info, {auto_update_joins_and_relates}, {validate}, {ignore_case})

updateConnectionProperties 方法使用工作空间字典或路径替换连接属性。

名称 说明 数据类型

current_connection_info

A string that represents the workspace path or a Python dictionary that contains connection properties to the source you want to update. If an empty string or None is used in current_connection_info, all connection properties will be replaced with the new_workspace_info, depending on the value of the validate parameter.

String

new_connection_info

A string that represents the workspace path or a Python dictionary that contains connection properties with the new source information.

String

auto_update_joins_and_relates

(可选)

If set to True, the updateConnectionProperties method will also update the connections for associated joins or relates.

(默认值为 True)

Boolean

validate

(可选)

If set to True, the connection properties will only be updated if the new_connection_info value is a valid connection. If it is not valid, the connection will not be replaced. If set to False, the method will set all connections to match the new_connection_info value, regardless of a valid match. In this case, if a match does not exist, the data sources would be broken.

(默认值为 True)

Boolean

ignore_case

(可选)

Determines whether searches will be case sensitive. By default, queries are case sensitive. To perform queries that are not case sensitive, set ignore_case to True.

(默认值为 False)

Boolean

代码示例

LayerFile 示例 1

以下脚本打印图层文件中数据源损坏的单个图层的名称。

import arcpy
lyrFile = arcpy.mp.LayerFile(r"C:\Projects\YosemiteNP\Yosemite.lyrx")
for lyr in lyrFile.listLayers():
    if lyr.supports("datasource"):
        if lyr.isBroken:
            print(lyr.name)
LayerFile 示例 2

以下脚本将从名为 Yosemite National Park 的地图中删除所有图层,并在图层文件中添加名为 Yosemite.lyrx 的图层。

import arcpy
aprx = arcpy.mp.ArcGISProject(r"C:\Projects\YosemiteNP\Yosemite.aprx")
m = aprx.listMaps("Yosemite National Park")[0]
for lyr in m.listLayers():
    m.removeLayer(lyr)
lf = arcpy.mp.LayerFile(r"C:\Projects\YosemiteNP\Yosemite.lyrx")
m.addLayer(lf)
aprx.save()
del aprx