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")