-
Notifications
You must be signed in to change notification settings - Fork 3
/
indexTest.py
136 lines (105 loc) · 5.38 KB
/
indexTest.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
130
131
132
133
134
135
136
#-------------------------------------------------------------------------------
# Name: module1
# Purpose:
#
# Author: Adolfo.Diaz
#
# Created: 23/10/2017
# Copyright: (c) Adolfo.Diaz 2017
# Licence: <your licence>
#-------------------------------------------------------------------------------
## ===================================================================================
def print_exception():
tb = sys.exc_info()[2]
l = traceback.format_tb(tb)
l.reverse()
tbinfo = "".join(l)
AddMsgAndPrint("\n\n----------ERROR Start-------------------",2)
AddMsgAndPrint("Traceback Info: \n" + tbinfo + "Error Info: \n " + str(sys.exc_type)+ ": " + str(sys.exc_value) + "",2)
AddMsgAndPrint("----------ERROR End-------------------- \n",2)
## ================================================================================================================
def AddMsgAndPrint(msg, severity=0):
# prints message to screen if run as a python script
# 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:
print msg
#for string in msg.split('\n'):
# Add a geoprocessing message (in case this is run as a tool)
if severity == 0:
arcpy.AddMessage(msg)
elif severity == 1:
arcpy.AddWarning(msg)
elif severity == 2:
arcpy.AddMessage(" ")
arcpy.AddError(msg)
except:
pass
## ===============================================================================================================
def addAttributeIndex(table,fieldList,verbose=True):
# Attribute indexes can speed up attribute queries on feature classes and tables.
# This function adds an attribute index(es) for the fields passed to the table that
# is passed in. This function takes in 2 parameters:
# 1) Table - full path to an existing table or feature class
# 2) List of fields that exist in table
# This function will make sure an existing index is not associated with that field.
# Does not return anything.
try:
# Make sure table exists. - Just in case
if not arcpy.Exists(table):
AddMsgAndPrint("\tAttribute index cannot be created for: " + os.path.basename(table) + " TABLE DOES NOT EXIST",2)
return False
else:
AddMsgAndPrint("\tAdding Indexes to Table: " + os.path.basename(table))
# iterate through every field
for fieldToIndex in fieldList:
# Make sure field exists in table - Just in case
if not len(arcpy.ListFields(table,"*" + fieldToIndex))>0:
AddMsgAndPrint("\tAttribute index cannot be created for: " + fieldToIndex + ". FIELD DOES NOT EXIST",2)
continue
# list of indexes (attribute and spatial) within the table that are
# associated with the field or a field that has the field name in it.
# Important to inspect all associated fields b/c they could be using
# a differently named index
existingIndexes = arcpy.ListIndexes(table,"*" + fieldToIndex)
bFieldIndexExists = False
# check existing indexes to see if fieldToIndex is already associated
# with an index
if existingIndexes > 0:
# iterate through the existing indexes looking for a field match
for index in existingIndexes:
associatedFlds = index.fields
# iterate through the fields associated with existing index.
# Should only be 1 field since multiple fields are not allowed
# in a single FGDB.
for fld in associatedFlds:
# Field is already part of an existing index - Notify User
if fld.name == fieldToIndex:
AddMsgAndPrint("\tAttribute Index for " + fieldToIndex + " field already exists",1)
bFieldIndexExists = True
# Field is already part of an existing index - Proceed to next field
if bFieldIndexExists:
break
# Attribute field index does not exist. Add one.
if not bFieldIndexExists:
newIndex = "IDX_" + fieldToIndex
# UNIQUE setting is not used in FGDBs - comment out
arcpy.AddIndex_management(table,fieldToIndex,newIndex,"#","ASCENDING")
if verbose:
AddMsgAndPrint("\tSuccessfully added attribute index for " + fieldToIndex)
except:
print_exception()
return False
import arcpy, sys, string, os, time, datetime, re, csv, traceback, shutil
from arcpy import env
if __name__ == '__main__':
FGDBpath = r'O:\scratch\test_20171023.gdb'
env.workspace = FGDBpath
fgdbTables = arcpy.ListTables('*')
fgdbFCs = [fgdbTables.append(fc) for fc in arcpy.ListFeatureClasses('*')]
AddMsgAndPrint("\nAdding Attribute Indexes to tables")
for table in fgdbTables:
tablePath = os.path.join(FGDBpath,table)
fieldNames = [f.name for f in arcpy.ListFields(tablePath)]
if not addAttributeIndex(tablePath,fieldNames): continue