UpdateLayer (arcpy.mapping)

摘要

通过从源图层提取信息,更新地图文档 (.mxd) 中所有图层属性或仅更新图层的符号系统。

讨论

arcpy.mapping API 仅可访问有限数量的、可直接修改的图层属性,但所有可在图层属性对话框中找到的属性都可使用 UpdateLayer 函数进行修改。从源图层中提取信息并将其应用于需要更新的地图文档中的图层。source_layer 可以是图层 (.lyr) 文件,也可以是地图文档中的图层。UpdateLayer 是一个多功能函数,可用多个不同的方法来使用此函数以产生不同结果。

一种选择是仅更新图层的符号系统。在此情况下,update_layersource_layer 必须具有相似的几何类型。根据渲染器的要求(例如,唯一值使用特定属性),属性定义也需相同。默认情况下仅更新符号系统。

另一个选择是更新所有图层属性,包括符号系统 (symbology_only=False)。例如,您可能要更新在许多地图文档中使用的图层的字段别名、选择符号系统、查询定义等等。进行此操作的简单方法是使用 ArcMap 来修改具有相应属性的图层,然后将图层输出保存到图层 (.lyr) 文件中。随后可将图层文件用作 source_layer 来更新给定图层的所有属性。更新所有属性时应谨慎,需要确保不对不希望被覆盖的属性进行更新。

UpdateLayer 也可用于交换完全不相关的图层。例如,您可用栅格图层或图层组来替换面图层。仅在 symbology_only 值设置为 False 时,才可使用此功能;否则可能与属性和/或几何类型发生冲突。当不限制符号系统时,UpdateLayer 实际上是调用 RemoveLayerAddLayer 函数。

如果要更新图层文件内的图层属性,则必须首先修改地图文档中的图层属性,然后将更改保存到图层文件中。请参阅图层对象的 savesaveACopy 方法。

如果只需要更新图层的时间属性,请参阅 UpdateLayerTime

语法

UpdateLayer (data_frame, update_layer, source_layer, {symbology_only})
参数说明数据类型
data_frame

A reference to a DataFrame object that contains the layer to be updated.

DataFrame
update_layer

A Layer object representing an existing layer that will be updated.

Layer
source_layer

A reference to a Layer object that contains the information to be applied to the update_layer.

Layer
symbology_only

A Boolean that determines whether or not to update only the layer's symbology, or all other properties as well. If set to True, only the layer's symbology will be updated.

(默认值为 True)

Boolean

代码实例

UpdateLayer 示例 1

以下脚本仅使用图层文件来对图层的符号系统进行更新。此图层名为 Rivers 并且在名为 County Maps 的数据框中。

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "County Maps")[0]
updateLayer = arcpy.mapping.ListLayers(mxd, "Rivers", df)[0]
sourceLayer = arcpy.mapping.Layer(r"C:\Project\Data\Rivers.lyr")
arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True)
mxd.saveACopy(r"C:\Project\Project2.mxd")
del mxd, sourceLayer
UpdateLayer 示例 2

以下脚本用来自另一个地图文档(以不同比例显示不同的河流要素类)的图层组完全替换名为 Rivers 的图层。

import arcpy

#Reference layer in secondary map document
mxd2 = arcpy.mapping.MapDocument(r"C:\Project\ProjectTemplate.mxd")
df2 = arcpy.mapping.ListDataFrames(mxd2, "Layers")[0]
sourceLayer = arcpy.mapping.ListLayers(mxd2, "Rivers Group Layer", df2)[0]

#Update layer in primary map document
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "County Maps")[0]
updateLayer = arcpy.mapping.ListLayers(mxd, "Rivers", df)[0]
arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, False)

#Save to a new map document and clear variable references
mxd.saveACopy(r"C:\Project\Project2.mxd")
del mxd, mxd2
9/15/2013