-
Notifications
You must be signed in to change notification settings - Fork 2
/
gSSURGO_TabularReport.py
310 lines (234 loc) · 11.1 KB
/
gSSURGO_TabularReport.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# gSSURGO_TabularReport.py
#
# Converts the SDV_Data table into a report
#
# Noticed that the HydricRating map creates an SDV_Data table with null values
# for mapunit records where HydricRating = 'Yes', but CompPct_R is null. Not a
# big deal, but a little messy in the report.
#
## ===================================================================================
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 attFld 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.AddError(" \n" + 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 "???"
## ===================================================================================
def GetUser():
try:
# Get computer login and try to format
#
envUser = arcpy.GetSystemEnvironment("USERNAME")
if "." in envUser:
user = envUser.split(".")
userName = " ".join(user).title()
elif " " in envUser:
user = env.User.split(" ")
userName = " ".join(user).title()
else:
userName = envUser
return userName
except:
errorMsg()
return ""
## ===================================================================================
## MAIN
## ===================================================================================
# Import system module
import arcpy, sys, string, os, traceback, locale, time
# Create the environment
from arcpy import env
try:
layerName = arcpy.GetParameterAsText(0) # Input Soil Map layer in ArcMap TOC
env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
df = mxd.activeDataFrame
sdvLayer = arcpy.mapping.ListLayers(mxd, layerName, df)[0]
gdb = os.path.dirname(arcpy.Describe(sdvLayer).catalogPath)
env.workspace = gdb
sdvTableName = "SDV_Data"
inputTbl = os.path.join(gdb, sdvTableName) # temporary table containing pre-aggregated data in gSSURGO database
legendTbl = os.path.join(gdb, "legend") # table containing legend.areaname
if arcpy.Exists(inputTbl):
# Create relate between rating table and legend table to get areaname
#arcpy.CreateRelationshipClass_management(legendTbl, inputTbl, os.path.join(gdb, "xLegend_SDVData"), "SIMPLE", "SDV_Data", "Legend", None, "ONE_TO_ONE", None, "AREASYMBOL", "AREASYMBOL", "","")
# Get SDV map layer information
layerFields = arcpy.Describe(layerName).fields
layerField = layerFields[-1] # assume the last field is the rating
layerRatingField = layerField.baseName.upper().encode('ascii')
fieldType = layerField.type.lower()
# Get SDV Rating table information
fmFields = list()
ratingFields = arcpy.Describe(inputTbl).fields
ratingField = ratingFields[-1] # assume the last field is the rating
ratingType = ratingFields[-1].type.lower()
ratingFieldName = ratingField.name.upper().encode('ascii')
if ratingType != fieldType:
#PrintMsg(" \nSwitch in data type (" + fieldType + "-->" + ratingType + ")", 1)
fieldType = ratingType
for fld in ratingFields:
fmFields.append(fld.name)
# Need to also add a comparison of field alias names for the rating table and the selected layer. Try
# to make sure they are both based upon the same data (this is now being done in the validation code).
#
# Determine type of rating table and which report template will match attributes
if "HZDEPT_R" in fmFields:
# horizon level rating, use landscape format for all
#
mxdName = "SDV_MapDescription_Landscape.mxd"
fm = {"AREASYMBOL":"AREASYMBOL", "MUKEY":"MUKEY", "MUSYM":"MUSYM", "MUNAME":"MUNAME", "COMPNAME":"COMPNAME", "COMPPCT_R":"COMPPCT_R", "HZDEPT_R":"HZDEPT_R", "HZDEPB_R":"HZDEPB_R", "RATING":ratingFieldName}
if fieldType in ["string", "choice"]:
templateName = "SDV_Report_Hz_String.rlf"
elif fieldType in ["single", "double", "float"]:
templateName = "SDV_Report_Hz_Float.rlf"
elif fieldType == "smallinteger":
templateName = "SDV_Report_Hz_Integer.rlf"
else:
raise MyError, "Invalid data type: " + fieldType
elif "COMPNAME" in fmFields:
# component level rating
fm = {"AREASYMBOL":"AREASYMBOL", "MUKEY":"MUKEY", "MUSYM":"MUSYM", "MUNAME":"MUNAME", "COMPNAME":"COMPNAME", "COMPPCT_R":"COMPPCT_R", "RATING":ratingFieldName}
if fieldType in ["string", "choice"]:
mxdName = "SDV_MapDescription_Landscape.mxd"
templateName = "SDV_Report_Co_String.rlf"
elif fieldType in ["single", "double", "float"]:
mxdName = "SDV_MapDescription_Portrait.mxd"
templateName = "SDV_Report_Co_Float.rlf"
elif fieldType == "smallinteger":
mxdName = "SDV_MapDescription_Portrait.mxd"
templateName = "SDV_Report_Co_Integer.rlf"
else:
raise MyError, "Invalid data type: " + fieldType
else:
# Map unit level rating
mxdName = "SDV_MapDescription_Portrait.mxd"
fm = {"AREASYMBOL":"AREASYMBOL", "MUKEY":"MUKEY", "MUSYM":"MUSYM", "MUNAME":"MUNAME", "RATING":ratingFieldName}
if ratingFieldName == "MUNAME":
PrintMsg(" \nCreating report for map unit name", 1)
templateName = "SDV_Report_MuName.rlf"
fm = None
elif fieldType in ["string", "choice"]:
templateName = "SDV_Report_Mu_String.rlf"
elif fieldType in ["single", "double", "float", "smallinteger"]:
templateName = "SDV_Report_Mu_Float.rlf"
elif fieldType == "integer":
templateName = "SDV_Report_Mu_Integer.rlf"
else:
raise MyError, "Invalid data type: " + fieldType
# Get the path for the template MXD being used to create the cover page
mxdFile = os.path.join(os.path.dirname(sys.argv[0]), mxdName)
# Get SDV narrative and settings used from the soil map layer
layerDesc = sdvLayer.description
# Open mxd with text box and update with layer description
textMXD = arcpy.mapping.MapDocument(mxdFile)
textDF = textMXD.activeDataFrame
textBox = arcpy.mapping.ListLayoutElements(textMXD, "TEXT_ELEMENT", "Description Text Box*")[0]
textMXD.title = layerName
textBox.text = layerDesc
textPDF = os.path.join(env.scratchFolder, "description.pdf")
arcpy.mapping.ExportToPDF(textMXD, textPDF)
# Get report template file fullpath (.rlf) and import current SDV_Data table into it
template = os.path.join(os.path.dirname(sys.argv[0]), templateName)
reportPDF = os.path.join(os.path.dirname(gdb), layerName + ".pdf")
if arcpy.Exists(reportPDF):
arcpy.Delete_management(reportPDF, "FILE")
# Set some of the parameters for the ExportReport command
dataset = "USE_RLF"
dataset = "ALL"
title = layerName
start = None
range = None
extent = None
# Remove table view "SDV_Data". It might be an old one
tableViews = arcpy.mapping.ListTableViews(mxd, sdvTableName, df)
for tbl in tableViews:
if tbl.name == sdvTableName:
arcpy.mapping.RemoveTableView(df, tbl)
#PrintMsg(" \nCreating table view using " + inputTbl, 1)
sdvTbl = arcpy.mapping.TableView(inputTbl)
sdvTableName = sdvTbl.name #?????
PrintMsg(" \nInput rating table: " + inputTbl, 0)
PrintMsg(" \nUsing report template: " + template, 0)
#PrintMsg(" \nUsing field mapping: " + str(fm), 0)
#PrintMsg(" \nRating data type: " + fieldType, 0)
arcpy.mapping.AddTableView(df, sdvTbl)
arcpy.SetProgressorLabel("Running report for '" + title + "' ....")
PrintMsg(" \nImporting table into report template (" + template + ")...", 0)
# Create PDF for tabular report
arcpy.mapping.ExportReport(sdvTbl, template, reportPDF, dataset, report_title=title, field_map=fm)
# Open the report PDF for editing
pdfDoc = arcpy.mapping.PDFDocumentOpen(reportPDF)
# Delete the opening page where I have the title and a page break. It would be better if I
# simply remove the title object from each of the report template files.
#
#pdfDoc.deletePages(1)
# Insert the title page PDF with narrative that created using the MXD layout
pdfDoc.insertPages(textPDF, 1)
# Update some of the PDF settings and metadata properties
keyWords = 'gSSURGO;soil map'
userName = GetUser()
pdfDoc.updateDocProperties(pdf_title=title, pdf_author=userName, pdf_subject="Soil Map", pdf_keywords=keyWords, pdf_layout="SINGLE_PAGE", pdf_open_view="USE_NONE")
pdfDoc.saveAndClose()
# Remove the 'SDV_Data' table view
arcpy.mapping.RemoveTableView(df, sdvTbl)
if arcpy.Exists(reportPDF):
arcpy.SetProgressorLabel("Report complete")
PrintMsg(" \nReport complete (" + reportPDF + ")\n ", 0)
os.startfile(reportPDF)
# Is this an attempt to keep the MXD object from breaking??
if mxd.filePath != "":
mxd = arcpy.mapping.MapDocument(mxd.filePath)
else:
raise MyError, "Failed to identify target layer"
try:
mxd = arcpy.mapping.MapDocument(mxd.filePath)
except:
pass
del mxd
except MyError, e:
PrintMsg(str(e), 2)
try:
mxd = arcpy.mapping.MapDocument(mxd.filePath)
except:
pass
except:
errorMsg()
try:
mxd = arcpy.mapping.MapDocument(mxd.filePath)
except:
pass