您的位置:首页 > 编程语言 > Python开发

ArcGIS Python arcpy 批量创建SDE空间索引

2015-02-11 10:12 344 查看
1、  SDE连接文件建立
使用Desktop系统工具箱建立SDE连接文件,如果catalog已经直连到SDE可以省略此步骤。
2、  Python脚本导入Toolbox设置
脚本中使用变量为SDE表空间,在导入脚本过程中只需要指定这一个变量即可
(1)      新建工具箱
(2)      添加脚本

(3)      变量设置
输入名称,在数据类型下拉列表中选择“工作空间”

(4)      脚本执行
在工具箱中双击建立的脚本,打开对话框,导航到SDE连接文件位置,按确定执行重建索引工作。
3、  脚本主要逻辑及脚本异常情况设置
脚本通过遍历SDE数据库下面的要素数据集以及要素类进行生成索引工作。生成索引过程中要确定要素不被其他程序占用,否则会出现独占锁情况出现,另外使用的建立空间索引为默认索引参数,如下所示0,0,0代表三级索引为默认索引arcpy.AddSpatialIndex_management(fc, 0, 0, 0)。
遍历完成后,获取该要素是否已经已有空间索引,已有索引先进行删除索引,然后进行重建,没有索引直接增加空间索引。

脚本如下:

# -*- coding: cp936 -*-
import os
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
import arcpy
# path = r"C:\Users\Administrator\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\连接到 localhost.sde"
path = arcpy.GetParameter(0)
arcpy.env.workspace = path
#遍历SDE下面要素类#
fcs = arcpy.ListFeatureClasses()
for fc in fcs:
try:
desc = arcpy.Describe(fc)
except:
arcpy.AddError('Failed:' + fc + " Check this featureclass")
#判断是否已经有空间索引
if (str(desc.hasSpatialIndex) == 'True'):
try:
arcpy.SetProgressorLabel("正在执行要素    " + fc + "...")
#如果有空间索引先删除原有索引,然后重新创建
arcpy.RemoveSpatialIndex_management(fc)
arcpy.AddMessage('Success:' + fc + ' Start Delete Spatial index')
arcpy.AddSpatialIndex_management(fc, 0, 0, 0)
arcpy.AddMessage('Success:' + fc + '  Create Spatial index Completed')
except:
arcpy.AddError('Failed:' + fc + " error")
else:
try:
arcpy.SetProgressorLabel("正在执行要素    " + fc + "...")
arcpy.AddSpatialIndex_management(fc, 0, 0, 0)
arcpy.AddMessage('Success:' + fc + " had no Spatial index,just now Created Spatial Index ")
except:
arcpy.AddError('Failed:' + fc + " error")

#遍历SDE下面的数据集
DSs = arcpy.ListDatasets()
print(DSs)
for ds in DSs:
#遍历数据集下面的要素类
arcpy.env.workspace = str(path) + "\\" + ds
print(arcpy.env.workspace)
ff = arcpy.ListFeatureClasses()
for fc in ff:
try:
desc = arcpy.Describe(fc)
except:
arcpy.AddError('Failed:' + fc + "Check this featureclass")
if (str(desc.hasSpatialIndex) == 'True'):
try:
arcpy.SetProgressorLabel("正在执行要素    " + fc + "...")
#如果有空间索引先删除原有索引,然后重新创建
arcpy.RemoveSpatialIndex_management(fc)
arcpy.AddMessage('Success:' + fc + ' Start Delete Spatial index')
arcpy.AddSpatialIndex_management(fc, 0, 0, 0)
arcpy.AddMessage('Success:' + fc + '  Create Spatial index Completed')
except:
arcpy.AddError('Failed:' + fc + " error")
else:
try:
arcpy.SetProgressorLabel("正在执行要素    " + fc + "...")
arcpy.AddSpatialIndex_management(fc, 0, 0, 0)
arcpy.AddMessage('Success:' + fc + " had no Spatial index,just now Created Spatial Index ")
except:
arcpy.AddError('Failed:' + fc + " error")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: