摘要
使用 PauseDrawing 类暂停地图视图绘制,并在上下文管理器(with 语句)中运行工具。
说明
使用 PauseDrawing 类作为上下文管理器,暂停地图视图的绘制。 该类是一个上下文管理器,用于在对图层进行更新或运行地理处理工具时暂停地图视图的绘制。 地理处理工具的输出将被添加到内容窗格中,但在上下文管理器退出前不会绘制在地图视图中。 即使在 PauseDrawing 上下文管理器中调用 RefreshLayer 函数,地图视图也不会进行刷新,直到退出 with 块为止。
语法
PauseDrawing ()
代码示例
当使用更新游标应用更新时,可使用 PauseDrawing 类作为上下文管理器。
import arcpy
from random import randint
featureclass_1 = r"c:\data\myGDB.gdb\fc1"
featureclass_2 = r"c:\data\myGDB.gdb\fc2"
with arcpy.PauseDrawing():
with arcpy.da.UpdateCursor(featureclass_1, "class_field") as cursor:
for row in cursor:
classification = randint(1, 5)
row[0] = classification
cursor.updateRow(row)
with arcpy.da.UpdateCursor(featureclass_2, "class_field") as cursor:
for row in cursor:
classification = randint(1, 5)
row[0] = classification
cursor.updateRow(row)
arcpy.RefreshLayer((featureclass_1, featureclass_2))
# The map view will refresh now that the PauseDrawing context manager has closed.
当插入新要素并运行地理处理工具时,使用 PauseDrawing 类作为上下文管理器。
import arcpy
from random import randint
featureclass_1 = r"c:\data\myGDB.gdb\fc1"
featureclass_2 = r"c:\data\myGDB.gdb\fc2"
calculation_code_block = """
from random import randint
def f():
return randint(1, 5)
"""
with arcpy.PauseDrawing():
# Use cursors to copy geometries from one feature layer to another
with arcpy.da.InsertCursor(featureclass_1, ["SHAPE@"]) as icur:
with arcpy.da.SearchCursor(featureclass_2, "SHAPE@") as scur:
for row in scur:
icur.insertRow((row[0]))
# Use CalculateField to update the class field
arcpy.management.CalculateField(
in_table=featureclass_1,
field="class",
expression="f()",
expression_type="PYTHON3",
code_block=calculation_code_block
)
# The map view will refresh now that the PauseDrawing context manager has closed.