-
Notifications
You must be signed in to change notification settings - Fork 1
/
PrintGeoDatabaseSchemas.py
289 lines (246 loc) · 10.9 KB
/
PrintGeoDatabaseSchemas.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import arcpy, os, sys
from arcpy import env
reload(sys)
sys.setdefaultencoding('utf-8')
workspace = env.workspace = r'Database Connections\GIS_MASTER.sde'
#Prints all Feature Datasets, their feature classes and fields inside them
textFile = open(r"C:\GEOschemas\featureClasses.txt","w")
def writeDatasets():
datasets = arcpy.ListDatasets()
datasets.sort()
for ds in datasets:
for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
#fc.sort()
path = os.path.join(workspace, ds, fc)
print "\n"
#print "Path: "+path
print path.split("GISOWN.", 1)[1]
fcName1 = path.split("GISOWN.", 1)[1]
fcName2 = fcName1.split("GISOWN.", 1)[1]
textFile.write(fcName2+"\n")
fields = arcpy.ListFields(path)
for field in fields:
print("{0}\t\t{1}\t\t{2}\t\t{3}\t{4}\t\t{5}\t{6}".format(field.name, field.type, field.isNullable, field.defaultValue, field.domain, field.precision, field.length))
textFile.write("{0}\t\t{1}\t\t{2}\t\t{3}\t{4}\t\t{5}\t{6}".format(field.name, field.type, field.isNullable, field.defaultValue, field.domain, field.precision, field.length))
textFile.write("\n")
textFile.write("\n")
textFile.write("\n\n")
def writeAllFC_notDsets(plp):
fcList = arcpy.ListFeatureClasses(feature_type = plp)
fcList.sort()
for fc in fcList:
db = r'Database Connections\GIS_MASTER.sde'
fullName = db+"\\"+fc
justTheName = fc
print "\n"
try:
print justTheName.split("GISOWN.", 1)[1]
except:
print justTheName
textFile.write("\n")
try:
textFile.write("\tFeature Class")
textFile.write("\n\t"+justTheName.split("GISOWN.", 1)[1]+"\n")
textFile.write("\n")
except:
textFile.write("\tFeature Class")
textFile.write("\n\t"+justTheName)
textFile.write("\n")
fields = arcpy.ListFields(justTheName)
textFile.write("{0}\t\t{1}\t\t{2}\t{3}\t{4}\t\t\t{5}\t{6}".format("Field name", "Data type", "Allow nulls", "Default Value", "Domain", "Precision", " Length \n"))
for field in fields:
print("{0}\t\t{1}\t\t{2}\t\t{3}\t\t{4}\t\t{5}\t{6}".format(field.name, field.type, field.isNullable, field.defaultValue, field.domain, field.precision, " "+str(field.length)))
textFile.write("{0}\t\t{1}\t\t{2}\t\t{3}\t\t{4}\t\t\t{5}\t{6}".format(field.name, field.type, field.isNullable, field.defaultValue, field.domain, field.precision, " "+str(field.length)))
textFile.write("\n")
textFile.write("\n\n")
#textFile.close()
print "done"
def writeAllTables():
tables = arcpy.ListTables()
tables.sort()
for table in tables:
print table
db = r'Database Connections\GIS_MASTER.sde'
fullName = db+"\\"+table
justTheName = table
print "\n"
try:
print justTheName.split("GISOWN.", 1)[1]
except:
print justTheName
textFile.write("\n")
try:
textFile.write("\tTable")
textFile.write("\n\t"+justTheName.split("GISOWN.", 1)[1]+"\n")
textFile.write("\n")
except:
textFile.write("\tTable")
textFile.write("\n\t"+justTheName+"\n")
textFile.write("\n")
fields = arcpy.ListFields(justTheName)
textFile.write("{0}\t\t{1}\t\t{2}\t{3}\t{4}\t\t\t{5}\t{6}".format("Field name", "Data type", "Allow nulls", "Default Value", "Domain", "Precision", " Length \n"))
for field in fields:
print("{0}\t\t{1}\t\t{2}\t\t{3}\t{4}\t\t{5}\t{6}".format(field.name, field.type, field.isNullable, field.defaultValue, field.domain, field.precision, " "+str(field.length)))
textFile.write("{0}\t\t{1}\t\t{2}\t\t{3}\t\t{4}\t\t\t{5}\t{6}".format(field.name, field.type, field.isNullable, field.defaultValue, field.domain, field.precision, " "+str(field.length)))
textFile.write("\n")
textFile.write("\n\n")
#textFile.close()
print "done"
#-Start Formatted Domains -----------------------------------------------------------
def writeDomainsFormatted():
textDomains = open(r"C:\GEOschemas\domains.txt","w")
domains = arcpy.da.ListDomains(r'Database Connections\GIS_MASTER.sde')
for domain in domains:
print domain.domainType + ' Domain'
print domain.name
textDomains.write(domain.domainType + ' Domain'+'\n')
textDomains.write(domain.name)
textDomains.write('\n')
print ' Description: ' + domain.description
textDomains.write(' Description: ' + domain.description + '\n')
print ' Field Type: ' + domain.type
textDomains.write(' Field Type: ' + domain.type + '\n')
print ' Split policy: ' + domain.splitPolicy
textDomains.write('Split policy: ' + domain.splitPolicy + '\n')
print 'Merge policy: ' + domain.mergePolicy
textDomains.write('Merge policy: ' + domain.mergePolicy + '\n')
textDomains.write('\t Code \t \t Description \n')
if domain.domainType == 'CodedValue':
coded_values = domain.codedValues
for val, desc in coded_values.iteritems():
print('\t {0} \t \t {1}'.format(val, desc))
textDomains.write('\t {0} \t \t {1}'.format(val, desc) + '\n')
elif domain.domainType == 'Range':
print('Min: {0}'.format(domain.range[0]))
textDomains.write('Min: {0}'.format(domain.range[0]) + '\n')
print('Max: {0}'.format(domain.range[1]))
textDomains.write('Max: {0}'.format(domain.range[1]) + '\n')
print '\n'
textDomains.write('\n \n')
textDomains.close()
#-Start Relatinship Classes -----------------------------------------------------------
def detectRelationship():
R_textFile = open(r"C:\GEOschemas\relationshipClasses.txt","w")
rc_list = [c.name for c in arcpy.Describe(workspace).children if c.datatype == "RelationshipClass"]
for rc in rc_list:
rc_path = workspace + "\\" + rc
des_rc = arcpy.Describe(rc_path)
typ = des_rc.isComposite
cardnty = des_rc.cardinality
origin = des_rc.originClassNames
destination = des_rc.destinationClassNames
notice = des_rc.notification
R_textFile.write("Retationship Class:")
print "\tRetationship Class:"
#R_textFile.write("Retationship Class:")
rcf = rc.split("GISOWN.", 1)[1]
#Relationship class name:
print rcf
R_textFile.write("\n")
R_textFile.write(rcf)
#Type:
if typ == True:
fpLbl = des_rc.forwardPathLabel
print "Type: Composite\t\t\tForward label: "+fpLbl
R_textFile.write("Type: Composite\t\t\tForward label: "+fpLbl)
else:
fpLbl = des_rc.forwardPathLabel
R_textFile.write("\n")
print "Type: Simple \t\t\tForward label: "+fpLbl
#R_textFile.write("\n")
R_textFile.write("Type: Simple\t\t\tForward label: "+fpLbl)
R_textFile.write("\n")
print "Cardinality: " + cardnty
R_textFile.write("Cardinality: " + cardnty)
print "Notification: " + notice + "\t\tBackward label:"+ des_rc.backwardPathLabel
R_textFile.write("\n")
R_textFile.write("Notification: " + notice + "\t\tBackward label: "+ des_rc.backwardPathLabel)
print "Origin feature class\t\tDestination Feature Class"
R_textFile.write("\n")
R_textFile.write("Origin feature class\t\tDestination Feature Class")
ocn = str(des_rc.originClassNames)
ocnf = ocn.split("GISOWN.", 1)[1]
ooo = ocnf.split("'",1)[0]
dcn = str(des_rc.destinationClassNames)
dcnf = dcn.split("GISOWN.", 1)[1]
ddd = dcnf.split("'",1)[0]
print "Name: "+ooo+"\t\tName: "+ddd
R_textFile.write("\n")
R_textFile.write("Name: "+ooo+"\t\tName: "+ddd)
origClassNm = des_rc.originClassNames
R_textFile.write("\n")
print "Primary key: "+des_rc.originClassKeys[0][0]+"\tForeign key: "+des_rc.originClassKeys[1][0]
R_textFile.write("Primary key: "+des_rc.originClassKeys[0][0]+"\tForeign key: "+des_rc.originClassKeys[1][0])
R_textFile.write("\n")
R_textFile.write("\n")
print("\n")
print("\n")
R_textFile.close()
textFile.write("\n")
textFile.write("----------------------------------------------------------------------------------------- \n")
textFile.write("All Tables: \n")
textFile.write("-----------------------------------------------------------------------------------------")
textFile.write("\n \n")
writeAllTables()
textFile.write("\n")
textFile.write("----------------------------------------------------------------------------------------- \n")
textFile.write("All Feature Classes that are contained in feature datasets: \n")
textFile.write("-----------------------------------------------------------------------------------------")
textFile.write("\n \n")
writeDatasets()
textFile.write("\n")
textFile.write("\n")
textFile.write("\n")
textFile.write("----------------------------------------------------------------------------------------- \n")
textFile.write("All POINT Feature Classes that are NOT contained in feature datasets): \n")
textFile.write("-----------------------------------------------------------------------------------------")
textFile.write("\n \n")
writeAllFC_notDsets('Point')
textFile.write("\n")
textFile.write("\n")
textFile.write("\n")
textFile.write("----------------------------------------------------------------------------------------- \n")
textFile.write("All LINE Feature Classes that are NOT contained in feature datasets): \n")
textFile.write("-----------------------------------------------------------------------------------------")
textFile.write("\n \n")
writeAllFC_notDsets('Line')
textFile.write("\n")
textFile.write("\n")
textFile.write("\n")
textFile.write("----------------------------------------------------------------------------------------- \n")
textFile.write("All POLYGON Feature Classes that are NOT contained in feature datasets): \n")
textFile.write("-----------------------------------------------------------------------------------------")
textFile.write("\n \n")
writeAllFC_notDsets('Polygon')
textFile.write("\n")
textFile.write("\n")
textFile.write("\n")
textFile.write("----------------------------------------------------------------------------------------- \n")
textFile.write("All ANNOTATION Feature Classes that are NOT contained in feature datasets): \n")
textFile.write("-----------------------------------------------------------------------------------------")
textFile.write("\n \n")
writeAllFC_notDsets('Annotation')
#textFile.write("\n")
#textFile.write("\n")
#textFile.write("\n")
#textFile.write("----------------------------------------------------------------------------------------- \n")
#textFile.write("All Feature Classes that are NOT contained in feature datasets): \n")
#textFile.write("-----------------------------------------------------------------------------------------")
#textFile.write("\n \n")
textFile.close()
#textDomains = open(r"C:\GEOschemas\domains.txt","w")
#textDomains.write("\n")
#textDomains.write("----------------------------------------------------------------------------------------- \n")
#textDomains.write("All Feature Domains: \n")
#textDomains.write("-----------------------------------------------------------------------------------------")
#textDomains.write("\n \n")
#textDomains.close()
writeDomainsFormatted()
#R_textFile = open(r"C:\GEOschemas\RelationshipClasses.txt","w")
#R_textFile.write("\n")
#R_textFile.write("----------------------------------------------------------------------------------------- \n")
#R_textFile.write("All Relationship Classes: \n")
#R_textFile.write("-----------------------------------------------------------------------------------------")
#R_textFile.write("\n \n")
#R_textFile.close()
detectRelationship()