forked from dtarb/TauDEM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeukerDouglasStreamDef.py
194 lines (161 loc) · 6.63 KB
/
PeukerDouglasStreamDef.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
# Script Name: PeukarDouglasStreamDef
#
# Created By: David Tarboton
# Date: 9/29/11
# Import ArcPy site-package and os modules
import arcpy
import os
import subprocess
# Inputs
inlyr = arcpy.GetParameterAsText(0)
desc = arcpy.Describe(inlyr)
fel = str(desc.catalogPath)
arcpy.AddMessage("\nInput Pit Filled Elevation Grid: " + fel)
inlyr1 = arcpy.GetParameterAsText(1)
desc = arcpy.Describe(inlyr1)
p = str(desc.catalogPath)
arcpy.AddMessage("Input D8 Flow Direction Grid: " + p)
weightcenter = arcpy.GetParameterAsText(2)
arcpy.AddMessage("Weight Center: " + weightcenter)
weightside = arcpy.GetParameterAsText(3)
arcpy.AddMessage("Weight Side: " + weightside)
weightdiag = arcpy.GetParameterAsText(4)
arcpy.AddMessage("Weight Diagonal: " + weightdiag)
accthresh = arcpy.GetParameterAsText(5)
arcpy.AddMessage("Accumulation Threshold: " + accthresh)
contcheck = arcpy.GetParameterAsText(6)
arcpy.AddMessage("Edge contamination checking: " + contcheck)
ogrlyr = arcpy.GetParameterAsText(7)
if arcpy.Exists(ogrlyr):
desc = arcpy.Describe(ogrlyr)
shfl1 = str(desc.catalogPath)
extn = os.path.splitext(shfl1)[1] # get extension of a file
# if extention is shapfile do not convert into gjson other wise convert
if extn == ".shp":
shfl = shfl1
else:
arcpy.AddMessage("Extracting json outlet file from: " + shfl1)
basename = os.path.basename(shfl1) # get last part of the path
dirname = os.path.dirname(p) # get directory
arcpy.env.workspace = dirname # does not work without specifying the workspace
arcpy.FeaturesToJSON_conversion(shfl1, basename + ".json") # convert feature to json
shfl = os.path.join(dirname, basename + ".json")
arcpy.AddMessage("Using Outlets file: " + shfl)
masklyr = arcpy.GetParameterAsText(8)
if arcpy.Exists(masklyr):
desc = arcpy.Describe(masklyr)
mask = str(desc.catalogPath)
arcpy.AddMessage("Input Mask Grid: " + mask)
ad8lyr = arcpy.GetParameterAsText(9)
if arcpy.Exists(ad8lyr):
desc = arcpy.Describe(ad8lyr)
ad8 = str(desc.catalogPath)
arcpy.AddMessage("Input D8 Contributing Area for Drop Analysis: " + ad8)
# Input Number of Processes
inputProc = arcpy.GetParameterAsText(10)
arcpy.AddMessage("Number of Processes: " + inputProc)
# Outputs
ss = arcpy.GetParameterAsText(11)
arcpy.AddMessage("Output Stream Source Grid: " + ss)
ssa = arcpy.GetParameterAsText(12)
arcpy.AddMessage("Output Accumulated Stream Source Grid: " + ssa)
src = arcpy.GetParameterAsText(13)
arcpy.AddMessage("Output Stream Raster Grid: " + src)
drp=arcpy.GetParameterAsText(14)
if arcpy.Exists(drp):
arcpy.AddMessage("Output Drop Analysis Table: " + drp)
usedroprange = arcpy.GetParameterAsText(15)
arcpy.AddMessage("Select Threshold by Drop Analysis: " + usedroprange)
minthresh = arcpy.GetParameterAsText(16)
arcpy.AddMessage("Minimum Threshold Value: " + minthresh)
maxthresh =arcpy.GetParameterAsText(17)
arcpy.AddMessage("Maximum Threshold Value: " + maxthresh)
numthresh = arcpy.GetParameterAsText(18)
arcpy.AddMessage("Number of Threshold Values: " + numthresh)
logspace = arcpy.GetParameterAsText(19)
arcpy.AddMessage("Logarithmic Spacing: " + logspace)
# Construct first command
cmd = 'mpiexec -n ' + inputProc + ' PeukerDouglas -fel ' + '"' + fel + '"' + ' -ss ' + '"' + ss + '"' + \
' -par ' + weightcenter + ' ' + weightside + ' ' + weightdiag
arcpy.AddMessage("\nCommand Line: " + cmd)
# Submit command to operating system
os.system(cmd)
# Capture the contents of shell command and print it to the arcgis dialog box
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
message = "\n"
for line in process.stdout.readlines():
if isinstance(line, bytes): # true in Python 3
line = line.decode()
message = message + line
arcpy.AddMessage(message)
arcpy.CalculateStatistics_management(ss)
# Construct second command
cmd = 'mpiexec -n ' + inputProc + ' AreaD8 -p ' + '"' + p + '"' + ' -ad8 ' + '"' + ssa + '"'
cmd = cmd + ' -wg ' + '"' + ss + '"'
if arcpy.Exists(ogrlyr):
cmd = cmd + ' -o ' + '"' + shfl + '"'
if contcheck == 'false':
cmd = cmd + ' -nc'
arcpy.AddMessage("\nCommand Line: " + cmd)
# Submit command to operating system
os.system(cmd)
# Capture the contents of shell command and print it to the arcgis dialog box
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
message = "\n"
for line in process.stdout.readlines():
if isinstance(line, bytes): # true in Python 3
line = line.decode()
message = message + line
arcpy.AddMessage(message)
arcpy.CalculateStatistics_management(ssa)
if (usedroprange == 'true') and arcpy.Exists(ogrlyr):
# Construct third command
cmd = 'mpiexec -n ' + inputProc + ' DropAnalysis -fel ' + '"' + fel + '"' + ' -p ' + '"' + p + '"' + \
' -ad8 ' + '"' + ad8 + '"'
cmd = cmd + ' -ssa ' + '"' + ssa + '"' + ' -o ' + '"' + shfl + '"' + ' -drp ' + '"' + drp + '"' + \
' -par ' + minthresh + ' ' + maxthresh + ' ' + numthresh + ' '
if logspace == 'false':
cmd = cmd + '1'
else:
cmd = cmd + '0'
arcpy.AddMessage("\nCommand Line: " + cmd)
# Submit command to operating system
os.system(cmd)
# Capture the contents of shell command and print it to the arcgis dialog box
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
message = "\n"
for line in process.stdout.readlines():
if isinstance(line, bytes): # true in Python 3
line = line.decode()
message = message + line
arcpy.AddMessage(message)
# (threshold,rest)=line.split(' ',1)
drpfile = open(drp,"r")
theContents = drpfile.read()
beg, threshold = theContents.rsplit(' ', 1)
drpfile.close()
# Construct fourth command
cmd = 'mpiexec -n ' + inputProc + ' Threshold -ssa ' + '"' + ssa + '"' + ' -src ' + '"' + src + '"'
if (usedroprange == 'true') and arcpy.Exists(ogrlyr):
cmd = cmd + ' -thresh ' + threshold
else:
cmd = cmd + ' -thresh ' + accthresh
if arcpy.Exists(masklyr):
cmd = cmd + ' -mask ' + '"' + mask + '"'
arcpy.AddMessage("\nCommand Line: " + cmd)
# Submit command to operating system
os.system(cmd)
# Capture the contents of shell command and print it to the arcgis dialog box
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
message = "\n"
for line in process.stdout.readlines():
if isinstance(line, bytes): # true in Python 3
line = line.decode()
message = message + line
arcpy.AddMessage(message)
arcpy.CalculateStatistics_management(src)
# remove converted json file
if arcpy.Exists(ogrlyr):
extn_json = os.path.splitext(shfl)[1] # get extension of the converted json file
if extn_json == ".json":
os.remove(shfl)