http://www.gissky.net- GIS空间站

我要投稿 投稿指南 RSS订阅 网站资讯通告:
搜索: 您现在的位置: GIS空间站 >> 技术专栏 >> ArcGIS >> ArcSDE >> 正文

ArcGIS使用Python 脚本批量协调和提交版本

作者:ESRI    文章来源:本站原创    点击数:    更新时间:2014-2-7
摘要:ArcGIS 地理数据库管理员可使用 Python 脚本自动执行通常要使用多个地理处理工具执行的任务。本主题将介绍管理员可能需要执行的、使安排在每晚进行的版本协调得以运行的过程。 许多管理员都想要确保运行协调时没有其他用户连接到数据库。ArcPy 函数 ListUsers 和 DisconnectUser 可用来确保只有管理员连接到数据库。

ArcGIS 地理数据库管理员可使用 Python 脚本自动执行通常要使用多个地理处理工具执行的任务。本主题将介绍管理员可能需要执行的、使安排在每晚进行的版本协调得以运行的过程。

许多管理员都想要确保运行协调时没有其他用户连接到数据库。ArcPy 函数 ListUsersDisconnectUser 可用来确保只有管理员连接到数据库。

查找已连接用户

要断开用户连接,第一步是确定哪些用户连接到了数据库。可使用 ListUsers 函数,它会传入管理员连接。

# get a list of connected users.
userList = arcpy.ListUsers("Database Connections/admin.sde")

分析已连接用户列表

一旦创建了已连接用户列表,就可以使用它来通知需要断开连接的用户。可以通过获取用户列表及其相关电子邮件地址来完成通知。

为简单起见,本例假定每个连接到数据库的用户都有与其电子邮件地址名称相同的基表名。可简单地更改本例,以使用其他方法确定电子邮件地址。

# get a list of usernames from the list of named tuples returned from ListUsers
userNames = [u.Name for u in userList]

# take the userNames list and make email addresses by appending the appropriate suffix.
emailList = [name +  '@company.com' for name in userNames]

生成和发送电子邮件

一旦生成了电子邮件列表,Python 就会发送电子邮件。本例使用了 Python 的 smtplib 模块,但是还有其他一些可以通过非标准模块发送电子邮件的选项。

注注:

要了解关于 smtplib 模块的更多信息,请参阅

http://docs.python.org/ 中的 Python 文档。

import smtplib
SERVER = "mailserver.yourcompany.com"
FROM = "SDE Admin <python@yourcompany.com>"
TO = emailList
SUBJECT = "Maintenance is about to be performed"
MSG = "Auto generated Message.\n\rServer maintenance will be performed in 15 minutes. Please log off."

# Prepare actual message
MESSAGE = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, MSG)

# Send the mail
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, MESSAGE)
server.quit()

阻止连接到数据库

通过脚本阻止连接数据库的工作流是使用 ArcPy 函数 AcceptingConnections 实现的。仅可通过 Python 脚本获得此函数。

在已通知用户并且脚本已暂停 15 分钟之后,用户连接就会断开。

#block new connections to the database.
arcpy.AcceptConnections('Database Connections/admin.sde', False)
注注:

没有必要阻止连接到数据库或断开所有用户连接来执行此维护。如果您的组织可允许断开所有连接,那么压缩过程的效率可能会更高。

暂停脚本

为了让用户有时间在连接断开之前完成工作,脚本需要暂停 15 分钟。Python 中的时间模块能够在已连接用户在断开连接之前提供 15 分钟的宽限期。

import time
time.sleep(900)#time is specified in seconds

断开用户连接

通过脚本断开用户连接的工作流程是使用 ArcPy 函数 DisconnectUser。仅可通过 Python 脚本获得此函数。

在已通知用户并且脚本已暂停 15 分钟之后,用户连接就会断开。

#disconnect all users from the database.
arcpy.DisconnectUser('Database Connections/admin.sde', "ALL")
注注:

或者,如果只想要特定用户断开连接,可使用 ListUsers 函数或已连接用户对话框返回的 ID,作为字符串或 Python 字符串列表来断开适合条件的用户的连接。

批量协调

“协调版本”工具可用来协调和提交企业级地理数据库中的所有版本。此工具提供了可以将地理数据库中所有版本协调至目标版本,或仅协调至固定为默认的版本的选项。此工具是实现有效压缩的方法,因为它允许多个版本以合适顺序一次性协调和提交。在本例中,以地理数据库管理员身份运行此工具。以地理数据库管理员身份进行连接能够协调和发布地理数据库中的所有版本,甚至是由其他用户拥有的私有版本或受保护版本。

了解有关协调版本地理处理工具的详细信息

# Get a list of versions to pass into the ReconcileVersions tool.
versionList = arcpy.ListVersions('Database Connections/admin.sde')

# Execute the ReconcileVersions tool.
arcpy.ReconcileVersions_management('Database Connections/admin.sde', "ALL_VERSIONS", "sde.DEFAULT", versionList, "LOCK_ACQUIRED", "NO_ABORT", "BY_OBJECT", "FAVOR_TARGET_VERSION", "POST", "DELETE_VERSION", "c:/temp/reconcilelog.txt")

压缩地理数据库

在协调和提交之后,要压缩数据库以移除任何冗余信息并将编辑内容移动至业务表内,这一点很重要。

# Run the compress tool. 
arcpy.Compress_management('Database Connections/admin.sde')

重新构建索引和更新统计数据

在执行压缩操作之后,推荐重新构建索引并更新统计数据。可使用“重新构建索引”和“分析数据集”工具来执行这些步骤。这些工具允许进行输入一列输入数据集,并可一次性在所有数据集上执行函数。这些工具还可用来更新统计数据并为合适的系统表重新构建索引。此过程的第一部分是获取用户所拥有的数据的列表。仅可由数据所有者或者已经拥有了一定管理权限的用户对索引和统计数据进行更新。

# set the workspace 
arcpy.env.workspace = 'Database Connections/admin.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.
dataList = 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 + '.*'):
    dataList += arcpy.ListFeatureClasses(feature_dataset=dataset)

一旦用户所拥有的数据列表确定,就会被传递到“重新构建索引”和“分析数据集”工具。

注注:

为简单起见,此脚本假设管理员用户 (admin) 也是数据所有者。如果您有多个数据所有者,则需要为每个数据所有者生成一个数据列表并传递到 RebuildIndexes 和 AnalyzeDatasets 工具。

注注:

用于限制用户拥有的数据集的通配符令牌将与操作系统相关。以上示例 ('*.'+ userName + '.*') 将适用于 SQL Server、PostgreSQL 或 DB2。对于 Oracle,可以使用以下通配符:(userName + '.*')。对于 Informix,可以使用以下通配符:('*:' + userName + '.*')

# Execute rebuild indexes and analyze datasets
# Note: to use the "SYSTEM" option the user must be an administrator.

workspace = "Database Connections/admin.sde"

arcpy.RebuildIndexes_management(workspace, "SYSTEM", dataList, "ALL")

arcpy.AnalyzeDatasets_management(workspace, "SYSTEM", dataList, "ANALYZE_BASE", "ANALYZE_DELTA", "ANALYZE_ARCHIVE")

完整代码示例

以下代码示例将上述所有部分组合在一起,以执行以下操作:

  • 识别连接的用户
  • 发送电子邮件通知
  • 防止数据库接受新连接
  • 断开用户连接
  • 协调版本
  • 压缩数据库
  • 重建索引并收集统计数据
  • 允许数据库开始接受新连接
import arcpy, time, smtplib

# set the workspace 
arcpy.env.workspace = 'Database Connections/admin.sde'

# set a variable for the workspace
workspace = arcpy.env.workspace

# get a list of connected users.
userList = arcpy.ListUsers("Database Connections/admin.sde")

# get a list of usernames of users currently connected and make email addresses
emailList = [u.Name + "@yourcompany.com" for user in arcpy.ListUsers("Database Connections/admin.sde")]

# take the email list and use it to send an email to connected users.
SERVER = "mailserver.yourcompany.com"
FROM = "SDE Admin <python@yourcompany.com>"
TO = emailList
SUBJECT = "Maintenance is about to be performed"
MSG = "Auto generated Message.\n\rServer maintenance will be performed in 15 minutes. Please log off."

# Prepare actual message
MESSAGE = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, MSG)

# Send the mail
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, MESSAGE)
server.quit()

#block new connections to the database.
arcpy.AcceptConnections('Database Connections/admin.sde', False)

# wait 15 minutes
time.sleep(900)

#disconnect all users from the database.
arcpy.DisconnectUser('Database Connections/admin.sde', "ALL")

# Get a list of versions to pass into the ReconcileVersions tool.
versionList = arcpy.ListVersions('Database Connections/admin.sde')

# Execute the ReconcileVersions tool.
arcpy.ReconcileVersions_management('Database Connections/admin.sde', "ALL_VERSIONS", "sde.DEFAULT", versionList, "LOCK_ACQUIRED", "NO_ABORT", "BY_OBJECT", "FAVOR_TARGET_VERSION", "POST", "DELETE_VERSION", "c:/temp/reconcilelog.txt")

# Run the compress tool. 
arcpy.Compress_management('Database Connections/admin.sde')

#Allow the database to begin accepting connections again
arcpy.AcceptConnections('Database Connections/admin.sde', True)

#Get a list of datasets owned by the admin user

# 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.
dataList = arcpy.ListTables('*.' + userName + '.*') + arcpy.ListFeatureClasses('*.' + userName + '.*') + arcpy.ListRasters('*.' + userName + '.*')

# Next, for feature datasets get all of the featureclasses
# from the list and add them to the master list.
for dataset in arcpy.ListDatasets('*.' + userName + '.*'):
    dataList += arcpy.ListFeatureClasses(feature_dataset=dataset)

# pass in the list of datasets owned by the admin to the rebuild indexes and analyze datasets tools
# Note: to use the "SYSTEM" option the user must be an administrator.
arcpy.RebuildIndexes_management(workspace, "SYSTEM", dataList, "ALL")

arcpy.AnalyzeDatasets_management(workspace, "SYSTEM", dataList, "ANALYZE_BASE", "ANALYZE_DELTA", "ANALYZE_ARCHIVE")

自动为脚本进行计划

脚本完成后,就会使用操作系统的计划任务为其进行计划,以使其在某个特定时间以设定间隔运行。

Tags:ArcGIS,版本,ArcSDE  
责任编辑:gissky
  • 上一篇文章:
  • 下一篇文章:
  • 关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 中国地图