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 we 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 关键字删除版本。在本例中,以地理数据库管理员身份运行此工具。以地理数据库管理员身份进行连接能够协调和发布地理数据库中的所有版本,甚至是由其他用户拥有的私有版本或受保护版本。
# 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)
一旦用户所拥有的数据列表得到确定,就会将其传递到重新构建索引和分析数据集工具。
如果您有多个数据所有者,则需要为每个数据所有者生成一个数据列表,并将在以每位用户的身份进行连接时运行重新构建索引和分析数据集工具。
# 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")
删除副本不再引用的版本
# 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 we reconciled earlier. # We 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 we can assume it's safe to delete them. if len(versionsList) > 0:
for version in verReconcileList:
arcpy.DeleteVersion_management(workspace, version)
完整代码示例
以下代码示例将上述所有部分组合在一起,以执行以下操作:
- 确定地理数据库版本。
- 协调版本和提交更改内容。
- 压缩地理数据库。
- 在系统表中重建索引并更新统计数据。
- 在用户表中重建索引并更新统计数据。
import arcpy, time, smtplib
# Set the workspace
arcpy.env.workspace = '//connectionLocation/admin.sde'
# Set a variable for the workspace
workspace = arcpy.env.workspace
# 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 we 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)
# 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')
# 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")
# Get a list of datasets owned by the data owner user (requires second connection file)
# Set the workspace
arcpy.env.workspace = '//connectionLocation/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 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)
# 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")
# set the workspace back to the admin 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 we reconciled earlier.
# We 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 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:
arcpy.DeleteVersion_management(workspace, version)