forked from wilmerjimenez/SoilDataDevelopmentToolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemove_Indexes.py
129 lines (97 loc) · 4.29 KB
/
Remove_Indexes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
## ===================================================================================
class MyError(Exception):
pass
## ===================================================================================
def errorMsg():
try:
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
theMsg = tbinfo + " \n" + str(sys.exc_type)+ ": " + str(sys.exc_value) + " \n"
PrintMsg(theMsg, 2)
except:
PrintMsg("Unhandled error in errorMsg method", 2)
pass
## ===================================================================================
def PrintMsg(msg, severity=0):
# Adds tool message to the geoprocessor
#
#Split the message on \n first, so that if it's multiple lines, a GPMessage will be added for each line
try:
for string in msg.split('\n'):
#Add a geoprocessing message (in case this is run as a tool)
if severity == 0:
arcpy.AddMessage(string)
elif severity == 1:
arcpy.AddWarning(string)
elif severity == 2:
arcpy.AddMessage(" ")
arcpy.AddError(string)
except:
pass
## ===================================================================================
def Number_Format(num, places=0, bCommas=True):
try:
# Format a number according to locality and given places
#locale.setlocale(locale.LC_ALL, "")
if bCommas:
theNumber = locale.format("%.*f", (places, num), True)
else:
theNumber = locale.format("%.*f", (places, num), False)
return theNumber
except:
errorMsg()
return False
## ===================================================================================
# Main
try:
# Remove all attribute indexes and relationshipclasses from a geodatabase
# Might not work when a featuredataset is present
import arcpy, os, sys
db = arcpy.GetParameterAsText(0) # input geodatabase
from arcpy import env
env.workspace = db
PrintMsg(" \nRemoving indexes and relationshipclasses from " + os.path.basename(db), 0)
# Process standalone tables first
tblList = arcpy.ListTables()
PrintMsg(" \nProcessing " + str(len(tblList)) + " tables", 0)
tbl = ""
arcpy.SetProgressor("step", "Removing indexes for " + tbl, 0, len(tblList), 1)
for tbl in tblList:
arcpy.SetProgressorLabel("Removing indexes for " + tbl)
arcpy.SetProgressorPosition()
indxList = arcpy.ListIndexes(tbl)
for indx in indxList:
# assuming only one field per index
fldList = indx.fields
if fldList[0].name != "OBJECTID":
arcpy.RemoveIndex_management(tbl, indx.name)
#print tbl + ", " + indx.name + ", " + fldList[0].name
# remove relationshipclasses after indexes are gone
rcs = arcpy.Describe(tbl).relationshipClassNames
for rc in rcs:
arcpy.Delete_management(rc)
# Process featureclasses next
fcList = arcpy.ListFeatureClasses()
PrintMsg(" \nProcessing " + str(len(fcList)) + " featureclasses", 0)
tbl = ""
arcpy.SetProgressor("step", "Removing indexes for " + tbl, 0, len(fcList), 1)
for tbl in fcList:
arcpy.SetProgressorPosition()
indxList = arcpy.ListIndexes(tbl)
for indx in indxList:
# assuming only one field per index
fldList = indx.fields
if not fldList[0].name in ("Shape", "OBJECTID"):
#print tbl + ", " + indx.name + ", " + fldList[0].name
if arcpy.Exists(indx.name): # Having problems removing some indexes the first time through
arcpy.RemoveIndex_management(tbl, indx.name)
# remove relationshipclasses after indexes are gone
rcs = arcpy.Describe(tbl).relationshipClassNames
for rc in rcs:
arcpy.Delete_management(rc)
PrintMsg(" \n", 0)
except MyError, e:
# Example: raise MyError, "This is an error message"
PrintMsg(str(e) + " \n", 2)
except:
errorMsg()