Editor

摘要

通过 Editor 对象可以使用编辑会话和编辑操作来管理数据库事务。

编辑内容将被暂存,直到触发时才提交到数据。 如果在一个编辑操作中或在多个编辑操作之间发生了错误,这样可以更好地进行控制。

说明

编辑会话和编辑操作具有以下优点:

  • 将编辑内容细分为原子事务。 如果在完成所有编辑之前发生了错误,编辑内容可以舍弃并且事务可以回滚。
  • 由地理数据库维护可选编辑操作的撤消和重做堆栈。 停止编辑操作后,系统会将其置于撤消堆栈上。 可以通过调用 undoOperationredoOperation 方法来遍历撤消和重做堆栈。
  • 编辑会话和编辑操作都可实现分批更新,因此在编辑企业级地理数据库时可发挥巨大的性能优势。
  • 在允许多用户同时编辑的地理数据库中,编辑会话中的应用程序在会话完成前将无法识别其他应用程序所做的更改。

Editor 类可用于启动和停止地理数据库和 shapefile 不同的编辑会话和编辑操作。

startEditing 方法用于启动编辑会话,startOperation 方法用于启动编辑操作。 要提交编辑操作,请调用 stopOperation。 要取消编辑操作,请调用 abortOperation。 要完成编辑会话,请调用 stopEditing,该方法接受一个布尔型参数,用于指示提交还是放弃会话内所做的更改。

注:

偏离此模式可能会导致意外结果。 例如,当编辑操作正在进行时,不要调用 stopEditing。 而应该取消编辑操作,并停止编辑会话。

编辑操作必须控制在编辑会话的上下文中,而且编辑操作不能嵌套到其他编辑操作中。

编辑会话和 with 语句

编辑会话和编辑操作还可以与 with 语句结合使用。 with 语句充当上下文管理器并负责处理相应的启动、停止和中止调用。 以下示例突出了 Editor 类与 with 语句结合使用的基本结构。

import arcpy

# Open an edit session and start an edit operation
with arcpy.da.Editor(workspace):
    print('<your edits go here>')

    # If an exception is raised, the operation is cancelled, and the edit
    # session is closed without saving.

    # If no exceptions are raised, the operation stops, is saved, and the edit
    # session closes.

使用撤消和重做堆栈

撤消和重做堆栈在编辑会话中是否启用,具体取决于 startEditing 方法的布尔型参数。 如果编辑会话将包含多个可能依相应的条件回滚(和重做)的操作,请启用撤消和重做堆栈。 否则可以通过将参数设置为 False 来禁用撤消和重做堆栈,以提升系统性能(例如,编辑会话仅包含一个操作)。

注:

在企业级地理数据库中启动版本化的编辑会话时,将启用撤消和重做堆栈。 未版本化的编辑会话不支持撤消和重做操作。

用于控制撤消和重做堆栈的两种方法是 undoOperationredoOperationundoOperation 将编辑会话的状态回滚到上一编辑操作,并将编辑操作移至重做堆栈。 redoOperation 方法将编辑操作从最近的重做堆栈移回撤消堆栈,并将编辑会话状态向前滚动至编辑操作后的状态。 提交编辑操作时,将清除重做堆栈,之后将无法重做任何曾位于重做堆栈中的操作。

需要编辑会话的情况

以下列出了只能在编辑会话中编辑的一些数据集类型:

  • 参与拓扑的要素类
  • 参与几何网络的要素类
  • 参与网络数据集的要素类
  • 企业级地理数据库中的版本化数据集
  • 一些带有类扩展的对象和要素类

编辑会话和游标

游标应限制在单个编辑操作中。 这意味着,对于每个操作都应实例化并释放一个新的游标。 在编辑由某一游标返回的行时这一点很重要,因为行与地理数据库的特定状态紧密相关。 避免在多个编辑操作中使用游标,因为这样做可能会导致意外行为和数据丢失。

编辑会话和版本化数据

可以对企业级地理数据库表和要素类进行版本化。 isVersioned 属性可用于确定表或要素类是否已版本化。

编辑企业级地理数据库表和要素类时,应根据正在编辑的数据类型设置编辑会话模式,具体如下:

  • 编辑非版本化表或要素类时,将 multiuser_mode 参数设置为 False
  • 编辑版本化表或要素类时,将 multiuser_mode 参数设置为 True

使用 Editor 类的 multiuser_mode 参数或 EditorstartEditing 方法的 multiuser_mode 参数来设置多用户模式。

如果未正确设置多用户模式,可能会收到无法在编辑会话之外更新此类中对象的错误。

语法

 Editor (workspace, {multiuser_mode}, {version})
参数说明数据类型
workspace

The path to the workspace to edit. The editor can edit only one workspace at a time.

The following are valid inputs:

  • The path to a workspace, for example: r"c:\myProj\f.gdb" or r"c:\myProj\db.sde".
  • An arcpy.Describe object. This can be the result of describing a workspace, or the workspace property returned by describing a layer, table, or feature class, for example: arcpy.Describe(fc).workspace.
  • An arcpy.mp.Layer object.

注:

If the value provided has an associated version, the edit session will use that version. This can be overridden with the version argument.

Object
multiuser_mode

Specifies how edits will be applied to the data source when editing an enterprise geodatabase. For any other data source, including a feature service with version management enabled, this parameter will be ignored.

To allow editing on versioned data, set this parameter to True. Nonversioned data can be edited with the parameter set to either True or False.

When this parameter is set to True, the following will occur:

  • For versioned data, the undoOperation and redoOperation methods are supported, as well as saving or discarding the edit session.
  • For nonversioned data (for example, nonversioned archived or unversioned) the edits created in the edit session can be saved or discarded. Neither the undoOperation or redoOperation methods are supported with nonversioned data.

When this parameter is set to False, the following will occur:

  • For versioned data, an error will occur when attempting to edit.
  • For nonversioned data, edits will be committed when stopEditing(True) or rolled back when stopEditing(False).

(默认值为 True)

Boolean
version

The workspace's version that will be associated with the edit session.

  • This parameter overrides the version associated with the value set using the workspace parameter.
  • When the workspace does not have a version that matches the name provided, a RuntimeError exception will be raised.

String

属性

属性说明数据类型
isEditing
(只读)

指定 Editor 对象是否在编辑会话中。

Boolean
version
(只读)

对于支持版本化的工作空间,这是与对象关联的工作空间版本的名称。

当不适用时,属性值将为 None

String
workspacePath
(只读)

与对象关联的工作空间的路径。

String

方法概述

方法说明
__enter__ ()

启动编辑会话。

__exit__ ()

如果成功,Editor 对象将停止编辑并保存编辑会话。 如果发生异常,则编辑停止但不保存。

startEditing ({with_undo}, {multiuser_mode})

启动编辑会话。

stopEditing ({save_changes})

停止编辑会话。

startOperation ()

开始编辑操作。

stopOperation ()

停止编辑操作。

abortOperation ()

中止编辑操作。

undoOperation ()

撤消编辑操作(回滚修改)。

redoOperation ()

重做编辑操作。

方法

__enter__ ()
__exit__ ()
startEditing ({with_undo}, {multiuser_mode})
参数说明数据类型
with_undo

Specifies whether the undo and redo stacks will be enabled or disabled for an edit session.

If an edit session will contain multiple operations that might be conditionally rolled back (and redone), enable the undo and redo stacks by setting this argument to True. If not—for example, if the edit session will only contain a single operation—the undo and redo stacks can be disabled for performance benefits by setting this argument to False.

When starting a versioned edit session in an enterprise geodatabase, the undo and redo stacks will always be enabled. Nonversioned edit sessions do not support undo and redo operations.

(默认值为 True)

Boolean
multiuser_mode

Specifies whether multiuser mode is enabled. When set to False, you have full control of editing a nonversioned or versioned dataset. If the dataset is nonversioned and you use stopEditing(False), the edits will not be committed (if set to True, the edits will be committed).

(默认值为 True)

Boolean
stopEditing ({save_changes})
参数说明数据类型
save_changes

Specifies whether the edits will be saved or discarded.

(默认值为 True)

Boolean
startOperation ()
stopOperation ()
abortOperation ()
undoOperation ()
redoOperation ()

代码示例

Editor 示例 1

以下代码使用 with 语句,此语句用于开启编辑操作并在表的一组所选行中使用 CalculateField 函数。 并将处理和打印出现的任何工具错误。

由于在 with 语句中执行 CalculateField 函数,如果出现任何异常,将不会保存更改。 如果成功完成 CalculateField,将保存更新。


import arcpy

fc = 'C:/Portland/Portland.gdb/Land/Parks'
workspace = 'C:/Portland/Portland.gdb'
layer_name = 'Parks'

arcpy.management.MakeFeatureLayer(fc, layer_name)
arcpy.management.SelectLayerByAttribute(
    layer_name, 'NEW_SELECTION',
    """CUSTODIAN = 'City of Portland'""")

with arcpy.da.Editor(workspace):
    arcpy.management.CalculateField(layer_name, 'Usage', '"PUBLIC"', 'PYTHON')
Editor 示例 2

以下为开始编辑会话和编辑操作、在表中创建行、停止编辑操作以及提交编辑会话的示例。

import arcpy
import os

fc = 'C:/projects/Portland/Portland.sde/portland.jgp.schools'
workspace = os.path.dirname(fc)

# Start an edit session. Must provide the workspace.
edit = arcpy.da.Editor(workspace)

# Edit session is started without an undo/redo stack for versioned data

# Set multiuser_mode to False if working with unversioned enterprise gdb data
edit.startEditing(with_undo=False, multiuser_mode=True)

# Start an edit operation
edit.startOperation()

# Insert a row into the table.
with arcpy.da.InsertCursor(fc, ('SHAPE@', 'Name')) as icur:
    icur.insertRow([(7642471.100, 686465.725), 'New School'])

# Stop the edit operation.
edit.stopOperation()

# Stop the edit session and save the changes
edit.stopEditing(save_changes=True)
Editor 示例 3

以下是将 Editor 类作为上下文管理器用于版本化数据的示例。


import arcpy
import os

new_students = 'C:/projects/Portland/Portland.sde/ptld.main.students_new2022'
students = 'C:/projects/Portland/Portland.sde/ptld.main.students'

workspace = arcpy.Describe(students).workspace

# Start an edit session. Provide the workspace being acted on
# and with sde specify if data is edited in multiuser_mode.
with arcpy.da.Editor(workspace, multiuser_mode=True):

    # If all 3 edit operations run successfully without error
    # upon exiting this code block the edits will be applied to the data

    arcpy.management.CalculateField(new_students , 'arrived', '2022')
    arcpy.management.Append(new_students, students)
    arcpy.management.CalculateField(students, "active", True)
Editor 示例 4

下面显示了在脚本工具中使用 Editor 类的推荐模式。


import arcpy

# arcpy.GetParameter(0) could be a feature class or layer in a map.
# If the dataset's workspace is versioned, the editor will use the same version
in_features = arcpy.GetParameter(0)
workspace = arcpy.Describe(in_features).workspace

with arcpy.da.Editor(workspace):
    # Perform edit operations here.
    print('Perform edit operations here')

相关主题