-
Notifications
You must be signed in to change notification settings - Fork 2
/
GetDominantComponent.py
134 lines (107 loc) · 4.88 KB
/
GetDominantComponent.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
# GetDominantComponent.py
#
# Steve Peaslee, USDA-NRCS
# 2015-10-07
#
# Uses simplest method of determining dominant componet for each map unit. Takes
# the first component with the highest representative component percent.
#
# One weakness in this method is that it does not handle 'ties' where two components
# have the same comppct_r. This simplistic method does not take into account
# what kind of data is behind either (nulls or higher-lower values).
#
## ===================================================================================
class MyError(Exception):
pass
## ===================================================================================
def PrintMsg(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:
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)
#arcpy.AddMessage(" ")
except:
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)
PrintMsg(theMsg, 2)
except:
PrintMsg("Unhandled error in errorMsg method", 2)
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:
PrintMsg("Unhandled exception in Number_Format function (" + str(num) + ")", 2)
return False
## ===================================================================================
## ====================================== Main Body ==================================
# Import modules
import sys, string, os, locale, arcpy, traceback
from arcpy import env
inputDB = arcpy.GetParameterAsText(0) # Input database. Assuming SSURGO or gSSURGO soils database
outputTbl = arcpy.GetParameterAsText(1) # Output table containing dominant component for each map unit
try:
PrintMsg(" \nGetting dominant component for each map unit in " + os.path.basename(inputDB), 0)
coTbl = os.path.join(inputDB, "component")
if not arcpy.Exists(coTbl):
raise MyError, "COMPONENT table not found for " + inputDB
# Open component table sorted by cokey and comppct_r
iCnt = int(arcpy.GetCount_management(coTbl).getOutput(0))
dComp = dict()
sqlClause = (None, "ORDER BY MUKEY DESC, COMPPCT_R DESC, COKEY DESC")
arcpy.SetProgressor("step", "Reading component table...", 0, iCnt, 1)
with arcpy.da.SearchCursor(coTbl, ["mukey", "cokey", "comppct_r"], sql_clause=sqlClause) as incur:
for inrec in incur:
if not inrec[0] in dComp:
# this is the dominant component
dComp[inrec[0]] = inrec[1], inrec[2]
arcpy.SetProgressorPosition()
if len(dComp) > 0:
arcpy.ResetProgressor()
# write values to new table
if arcpy.Exists(outputTbl):
arcpy.Delete_management(outputTbl)
arcpy.CreateTable_management(os.path.dirname(outputTbl), os.path.basename(outputTbl))
arcpy.AddField_management(outputTbl, "mukey", "Text", "", "", 30)
arcpy.AddField_management(outputTbl, "cokey", "Text", "", "", 30)
arcpy.AddField_management(outputTbl, "comppct_r", "Short")
arcpy.SetProgressor("step", "Saving dominant component information...", 0, len(dComp), 1)
with arcpy.da.InsertCursor(outputTbl, ["mukey", "cokey", "comppct_r"]) as outcur:
for mukey, val in dComp.items():
cokey, comppct_r = val
outrec = [mukey, cokey, comppct_r]
outcur.insertRow(outrec)
arcpy.SetProgressorPosition()
arcpy.AddIndex_management(outputTbl, "mukey", "Indx_dcMukey", "UNIQUE")
mxd = arcpy.mapping.MapDocument("CURRENT")
df = mxd.activeDataFrame
tblView = arcpy.mapping.TableView(outputTbl)
arcpy.mapping.AddTableView(df, tblView)
PrintMsg(" \nFinished writing dominant component information to " + os.path.basename(outputTbl) + " \n ", 0)
except MyError, e:
# Example: raise MyError("this is an error message")
PrintMsg(str(e) + " \n", 2)
except:
errorMsg()