addLayer
汇总
提供访问图层文件( .lyr 或 .lyrx )中图层的访问权限和其他基本图层管理方法。
讨论
LayerFile 类提供的方法和属性提供对图层文件中包含的单个图层和表的访问权限。 图层文件可以包含一个或多个图层、表和组图层。
ArcGIS Pro 可读取 .lyr 和 .lyrx 图层文件类型。 更改只能保存到 .lyrx 图层文件类型。 如果要更改 .lyr 文件,则必须调用 saveACopy 方法。
语法
addLayer()
属性
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
filePath (只读) |
返回报告完全限定的图层文件路径和文件名的字符串值。 |
String |
|
metadata (读取和写入) |
获取或设置图层文件的 元数据 类信息。请注意:设置元数据取决于 |
Metadata |
|
version (只读) |
根据上次保存项目的时间返回版本字符串。它使用语义版本化格式:Major.Minor.Patch。 |
String |
方法
addLayer(add_layer_or_layerfile, {add_position})
用于使用基本放置选项向图层文件 (.lyrx) 添加 Layer 或 LayerFile 。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
add_layer_or_layerfile |
Layer |
|
|
add_position (可选) |
此常数用于确定添加的单个或多个图层在图层文件中的放置位置。
(默认值为 AUTO_ARRANGE) |
String |
返回值
| 数据类型 | 说明 |
|---|---|
|
List |
Layer 对象的 Python 列表。 |
addLayerToGroup(target_group_layer, add_layer_or_layerfile, {add_position})
用于使用基本放置选项向图层文件 (.lyrx) 内的现有图层组添加 Layer 或 LayerFile 的内容。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
target_group_layer |
对现有 Layer 对象组的引用。 |
Layer |
|
add_layer_or_layerfile |
Layer |
|
|
add_position (可选) |
此常数用于确定在
(默认值为 AUTO_ARRANGE) |
String |
insertLayer(reference_layer, insert_layer_or_layerfile, {insert_position})
用于通过指定位置向图层文件 (.lyrx) 添加 Layer 或 LayerFile 。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
reference_layer |
表示可确定新图层插入位置的现有图层的 Layer 对象。 |
Layer |
|
insert_layer_or_layerfile |
Layer |
|
|
insert_position (可选) |
此常数用于确定相对于
(默认值为 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 (可选) |
此常数用于确定移动的图层相对于参考图层的放置位置。
(默认值为 BEFORE) |
String |
removeLayer(remove_layer)
用于从图层文件中移除图层。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
remove_layer |
引用表示要移除的图层的 Layer 对象。 |
Layer |
save()
保存对 .lyrx 文件的现有图层文件引用,即使引用的是 .lyr 文件也同样如此。
saveACopy(file_name)
将 LayerFile 保存到新路径或另存为新文件名。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
file_name |
用于将 ( |
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 |
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) |
Boolean |
|
validate (可选) |
If set to (默认值为 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 (默认值为 False) |
Boolean |
代码示例
以下脚本打印图层文件中数据源损坏的单个图层的名称。
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)
以下脚本将从名为 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