ArcGIS 地理数据库管理员可使用 Python 脚本自动执行通常要使用多个地理处理工具执行的多个任务。本主题介绍在现场工作人员对参与传统版本化的数据所做的编辑内容进行同步后,管理员每晚运行版本协调需要执行的过程。
根据服务名称确定地理数据库版本
首先,创建需要协调的地理数据库版本列表。
# Create a list of user names that will be used to find versions.
userList = ['???', '###']
# Get a list of versions for the service named '???' and '###' to pass into the ReconcileVersions tool.
versions = arcpy.da.ListVersions('//connectionLocation/admin.sde')
# Create an empty list that will be used to hold version names that you want to reconcile.
verReconcileList = []
# Loop through the versions list to look for versions with appropriate names.
# if these names are found append them to the verReconcileList.
for user in userList:
for version in versions:
if user.lower() in version.name.lower():
verReconcileList.append(version.name)
批量协调版本和提交更改内容
可以使用协调版本工具来协调和提交企业级地理数据库中的所有传统版本。此工具提供了可以将地理数据库中所有版本协调至目标版本 (ALL_VERSIONS),或仅协调阻止目标版本压缩的版本 (BLOCKING_VERSIONS)。此工具是实现有效压缩的方法,因为它允许多个版本以合适顺序一次性协调和提交。完成协调或提交操作后,可通过提供 DELETE_VERSION 关键字删除版本。
在此示例中,不会删除版本(提供 KEEP_VERSION 关键字),并且以地理数据库管理员的身份运行该工具。以地理数据库管理员身份进行连接能够协调和提交地理数据库中的所有版本,甚至是由其他用户拥有的私有版本或受保护版本。
# Get a list of versions to pass into the ReconcileVersions tool. versionList = arcpy.ListVersions('//connectionLocation/admin.sde')
# Execute the ReconcileVersions tool.
arcpy.ReconcileVersions_management('//connectionLocation/admin.sde', "ALL_VERSIONS", "sde.DEFAULT", verReconcileList, "LOCK_ACQUIRED", "NO_ABORT", "BY_OBJECT", "FAVOR_TARGET_VERSION", "POST", "KEEP_VERSION", "c:/temp/reconcilelog.txt")
压缩地理数据库
在协调和提交更改内容之后,必须压缩地理数据库才能移除任何冗余信息并将编辑内容移入基表。
# Run the compress tool.
arcpy.Compress_management('//connectionLocation/admin.sde')
重新构建索引并更新统计数据
执行压缩操作后,建议重新构建索引并更新统计数据。可使用重建索引和分析数据集工具来执行这些步骤。可使用这些工具输入数据集列表,以同时在所有数据集上执行其功能。以地理数据库管理员身份运行这些工具时还可以更新统计数据并为合适的系统表重新构建索引。此过程的第一部分是获取数据以及拥有这些数据的用户的列表。仅数据所有者可更新索引和统计数据。
# set the workspace arcpy.env.workspace = '//connectionLocation/dataOwner.sde'
# Get the user name for the workspace
# this assumes you are using database authentication. # OS authentication connection files do not have a 'user' property. userName = arcpy.Describe(arcpy.env.workspace).connectionProperties.user
# Get a list of all the datasets the user has access to. # First, get all the stand-alone tables, feature classes and rasters owned by the current user. oDataList = arcpy.ListTables('*.' + userName + '.*') + arcpy.ListFeatureClasses('*.' + userName + '.*') + arcpy.ListRasters('*.' + userName + '.*')
# Next, for feature datasets owned by the current user
# get all of the featureclasses and add them to the master list. for dataset in arcpy.ListDatasets('*.' + userName + '.*'):
oDataList += arcpy.ListFeatureClasses(feature_dataset=dataset)
识别每个用户所拥有的数据列表后,请在重新构建索引和分析数据集工具中使用该信息。
如果有多个数据所有者,请为每个数据所有者生成一个数据列表,然后以每个用户的身份使用连接到地理数据库的数据库连接文件 (.sde) 运行重新构建索引和分析数据集工具。
# Execute rebuild indexes and analyze datasets
# Note: to use the "SYSTEM" option, the user must be an administrator.
workspace = "//connectionLocation/user1.sde"
arcpy.RebuildIndexes_management(workspace, "NO_SYSTEM", oDataList, "ALL")
arcpy.AnalyzeDatasets_management(workspace, "NO_SYSTEM", oDataList, "ANALYZE_BASE", "ANALYZE_DELTA", "ANALYZE_ARCHIVE")
删除已协调版本
该过程的最后一步是删除已协调并提交的版本。之前当您运行 versionList 声明时,将获取已协调版本列表。在下面的代码中,您将循环遍历此列表以删除可安全删除的所有版本。
在下面的示例中,您将通过遍历所有复本获取复本版本列表。接下来,从已协调版本列表中移除所有这些复本版本。最后,循环遍历已协调版本列表的其余部分,然后删除这些版本。
# set the workspace workspace = '//connectionLocation/admin.sde'
# Get a list of all the replica versions in the geodatabase replicaVersions = [replica.version for replica in arcpy.da.ListReplicas(workspace)]
# Loop through this replica version list and remove these version names from the list of versions you reconciled earlier. # You want to remove versions from this list that are still being referenced by a replica for replicaVer in replicaVersions:
if replicaVer in verReconcileList:
verReconcileList.remove(replicaVer)
# Loop through the verReconcileList and delete versions.
# These versions are no longer being referenced by a replica so you can assume it's safe to delete them. if len(versionsList) > 0:
for version in verReconcileList:
arcpy.DeleteVersion_management(workspace, version)
完整代码示例
以下代码示例将上述大多数片段组合在一起,以执行以下操作:
- 确定地理数据库版本。
- 协调版本和提交更改内容。
- 压缩地理数据库。
- 在系统表中重建索引并更新统计数据。
- 在用户表中重建索引并更新统计数据。
import arcpy
try:
# Set the workspace
arcpy.env.workspace = 'Database Connections/admin.sde'
# Set variables
workspace = arcpy.env.workspace
arcpy.env.overwriteOutput = True
targetVersion = 'sde.DEFAULT'
# Create a list of user names that will be used to find versions.
userList = ['???', '###']
# Get a list of versions for the service named '???' and '###' to pass into the ReconcileVersions tool.
versions = arcpy.da.ListVersions(workspace)
# Create an empty list to hold version names to reconcile.
verReconcileList = []
# Loop through the list to look for versions with our user names in their name where the parent version is the target version.
# if these names are found, append them to the verReconcileList.
for user in userList:
for version in versions:
if user.lower() in version.name.lower():
if version.parentVersionName.lower() == targetVersion.lower():
verReconcileList.append(version.name)
# Perform maintenance if versions are found; otherwise, there is no maintenance to perform.
if len(verReconcileList)>0:
# Execute the ReconcileVersions tool.
arcpy.ReconcileVersions_management(workspace, "ALL_VERSIONS", targetVersion, verReconcileList, "LOCK_ACQUIRED", "NO_ABORT", "BY_OBJECT", "FAVOR_TARGET_VERSION", "POST", "KEEP_VERSION", "c:/temp/reconcilelog.txt")
# Run the compress tool.
arcpy.Compress_management(workspace)
# Rebuild indexes and analyze the states and states_lineages system tables
arcpy.RebuildIndexes_management(workspace, "SYSTEM", "", "ALL")
arcpy.AnalyzeDatasets_management(workspace, "SYSTEM", "", "ANALYZE_BASE", "ANALYZE_DELTA", "ANALYZE_ARCHIVE")
'''
*********************
Data Owner(s) Section
*********************
'''
# Get a list of datasets owned by the data owner user (requires second connection file)
# Set the workspace
arcpy.env.workspace = 'Database Connections/dataOwner.sde'
# Set a variable for the workspace
workspace = arcpy.env.workspace
# Get the user name for the workspace
# this assumes you are using database authentication.
# OS authentication connection files do not have a 'user' property.
userName = arcpy.Describe(arcpy.env.workspace).connectionProperties.user
# Get a list of all the datasets the user has access to.
# First, get all the stand alone tables, feature classes and raster datasets owned by the current user.
oDataList = arcpy.ListTables('*.' + userName + '.*') + arcpy.ListFeatureClasses('*.' + userName + '.*') + arcpy.ListRasters('*.' + userName + '.*')
# Next, for feature datasets owned by the current user
# get all of the feature classes and add them to the master list.
for dataset in arcpy.ListDatasets('*.' + userName + '.*'):
oDataList += arcpy.ListFeatureClasses(feature_dataset=dataset)
# Rebuild indexes and analyze the data owner tables
arcpy.RebuildIndexes_management(workspace, "NO_SYSTEM", oDataList, "ALL")
arcpy.AnalyzeDatasets_management(workspace, "NO_SYSTEM", oDataList, "ANALYZE_BASE", "ANALYZE_DELTA", "ANALYZE_ARCHIVE")
'''
*************************
End Data Owner(s) Section
*************************
'''
# set the workspace back to the geodatabase administrator workspace
workspace = 'Database Connections/admin.sde'
# Get a list of all the replica versions in the geodatabase
replicaVersions = [replica.version for replica in arcpy.da.ListReplicas(workspace)]
'''
- We now have a list of versions that were created by taking a map offline (verReconcileList)
- We also have a list of replica versions (replicaVersions)
- The versions that we were reconciling are ready to be deleted if they are not currently pointing to a version
- We are going to loop through the reconcile versions list and remove any versions that are still pointing to a replica
- The versions remaining in the reconcile list are ready to be cleaned (deleted) up because there are no maps/replicas pointing to them.
'''
# Use the list of versions associated with users/maps that we reconciled earlier. Remove any versions from the list that are still being used by a replica.
for replicaVer in replicaVersions:
if replicaVer in verReconcileList:
verReconcileList.remove(replicaVer)
# Loop through the versionsList and delete versions that are no longer being referenced by a replica.
# Since these versions are no longer being referenced by a replica, we can assume it's safe to delete them.
if len(verReconcileList) > 0: #check to see that the list is not empty
for version in verReconcileList:
try:
arcpy.DeleteVersion_management(workspace, version)
except:
print("Failed to delete version.")
print(arcpy.GetMessages(2))
else:
print("No versions to delete.")
else:
print("No versions to reconcile, aborting version maintenance routine.")
except:
print(arcpy.GetMessages(2))
print("Done.")