-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolbox.pyt
More file actions
304 lines (263 loc) · 13.8 KB
/
Toolbox.pyt
File metadata and controls
304 lines (263 loc) · 13.8 KB
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
import arcpy
from arcpy.sa import *
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "Hydrology"
self.alias = ""
# List of tool classes associated with this toolbox
self.tools = [Hydrology]
class Hydrology(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Hydrology"
self.description = "Description"
self.canRunInBackground = True
def getParameterInfo(self):
"""Define parameter definitions"""
in_dem = arcpy.Parameter( #Тут мы принимаем ЦМР
displayName="Input projected DEM",
name="in_features",
datatype="GPRasterLayer",
parameterType="Required",
direction="Input")
out_dem = arcpy.Parameter( #Выбор места сохранения гидрологически-корректной ЦМР
displayName="Output hydrologically correct DEM",
name="out_features",
datatype="GPRasterLayer",
parameterType="Optional",
direction="Output")
out_dem.category = "0.Fill DEM"
out_flow_dir = arcpy.Parameter( #Выбор места сохранения растра направления стока
displayName="Output Flow Direction",
name="out_flow_dir",
datatype="GPRasterLayer",
parameterType="Optional",
direction="Output")
out_flow_dir.category = "1.Flow Direction"
out_flow_accum = arcpy.Parameter( #Выбор места сохранения растра аккумуляции стока
displayName="Output Flow Accumulation",
name="out_flow_accum",
datatype="GPRasterLayer",
parameterType="Optional",
direction="Output")
out_flow_accum.category = "2.Flow Accumulation"
out_stream_network = arcpy.Parameter( #Выбор места сохранения растра сети водотоков
displayName="Output Stream Network Raster",
name="out_stream_network",
datatype="DERasterDataset",
parameterType="Optional",
direction="Output")
out_stream_network.category = "3.Stream Group"
in_threshold_stream_network = arcpy.Parameter( #Тут мы задаем порог для выделения водотоков
displayName="Threshold For Stream Network Raster",
name="in_threshold_stream_network",
datatype="GPLong",
parameterType="Optional",
direction="Input")
in_threshold_stream_network.value = 2000
in_threshold_stream_network.category = "3.Stream Group"
out_stream_order = arcpy.Parameter( #Тут мы сохраняем сеть водотоков в виде растра
displayName="Output Stream Order Raster",
name="out_stream_order",
datatype="GPRasterLayer",
parameterType="Optional",
direction="Output")
out_stream_order.category = "3.Stream Group"
out_stream_feature = arcpy.Parameter( #Выбор места сохранения водотоков в виде вектора (линии)
displayName="Output Stream Feature",
name="out_stream_feature",
datatype="DEFeatureClass",
parameterType="Optional",
direction="Output")
out_stream_feature.category = "3.Stream Group"
out_basin_raster = arcpy.Parameter( #Тут мы сохраняем растр бассейнов
displayName="Output Basin Raster",
name="out_basin_raster",
datatype="GPRasterLayer",
parameterType="Optional",
direction="Output")
out_basin_raster.category = "4.Basin Group"
out_basin_feature = arcpy.Parameter( #Выбор места сохранения бассейнов в виде векторов (полигоны)
displayName="Output Basin Feature",
name="out_basin_feature",
datatype="DEFeatureClass",
parameterType="Optional",
direction="Output")
out_basin_feature.category = "4.Basin Group"
in_force_flow = arcpy.Parameter(
displayName="Select Force Flow Direction",
name="in_force_flow",
datatype="GPString",
parameterType="Optional",
direction="Input")
in_force_flow.filter.type = "ValueList"
in_force_flow.filter.list = ["NORMAL", "FORCE"]
in_force_flow.value = "NORMAL"
in_force_flow.category = "1.Flow Direction"
in_flow_direction_type = arcpy.Parameter(
displayName="Select Flow Direction Type",
name="in_flow_direction_type",
datatype="GPString",
parameterType="Optional",
direction="Input")
in_flow_direction_type.filter.type = "ValueList"
in_flow_direction_type.filter.list = ["D8", "MFD", "DINF"]
in_flow_direction_type.value = "D8"
in_flow_direction_type.category = "1.Flow Direction"
in_flow_accum_data_type = arcpy.Parameter(
displayName="Select Flow Accumulation Data Type",
name="in_flow_accum_data_type",
datatype="GPString",
parameterType="Optional",
direction="Input")
in_flow_accum_data_type.filter.type = "ValueList"
in_flow_accum_data_type.filter.list = ["FLOAT", "INTEGER", "DOUBLE"]
in_flow_accum_data_type.value = "FLOAT"
in_flow_accum_data_type.category = "2.Flow Accumulation"
in_flow_accum_direction_type = arcpy.Parameter(
displayName="Select Flow Direction Type",
name="flow_accum_direction_type",
datatype="GPString",
parameterType="Optional",
direction="Input")
in_flow_accum_direction_type.filter.type = "ValueList"
in_flow_accum_direction_type.filter.list = ["D8", "MFD", "DINF"]
in_flow_accum_direction_type.value = "D8"
in_flow_accum_direction_type.category = "2.Flow Accumulation"
in_stream_order_method = arcpy.Parameter(
displayName="Select Stream Order Method",
name="in_stream_order_method",
datatype="GPString",
parameterType="Optional",
direction="Input")
in_stream_order_method.filter.type = "ValueList"
in_stream_order_method.filter.list = ["STRAHLER", "SHREVE"]
in_stream_order_method.value = "STRAHLER"
in_stream_order_method.category = "3.Stream Group"
in_stream_feature_simplify = arcpy.Parameter(
displayName="Select Simplify Method For Stream To Feature",
name="in_stream_feature_simplify",
datatype="GPString",
parameterType="Optional",
direction="Input")
in_stream_feature_simplify.filter.type = "ValueList"
in_stream_feature_simplify.filter.list = ["SIMPLIFY", "NO_SIMPLIFY"]
in_stream_feature_simplify.value = "NO_SIMPLIFY"
in_stream_feature_simplify.category = "3.Stream Group"
in_basin_feature_simplify = arcpy.Parameter(
displayName="Select Simplify Method For Basin To Feature",
name="in_basin_feature_simplify",
datatype="GPString",
parameterType="Optional",
direction="Input")
in_basin_feature_simplify.filter.type = "ValueList"
in_basin_feature_simplify.filter.list = ["SIMPLIFY", "NO_SIMPLIFY"]
in_basin_feature_simplify.value = "NO_SIMPLIFY"
in_basin_feature_simplify.category = "4.Basin Group"
in_fill_method = arcpy.Parameter(
displayName="Select Fill Method",
name="in_fill_method",
datatype="GPString",
parameterType="Optional",
direction="Input")
in_fill_method.filter.type = "ValueList"
in_fill_method.filter.list = ["PRATIK MUCHKARNI", "VLADIMIR S. DEKHNICH"]
in_fill_method.value = "PRATIK MUCHKARNI"
in_fill_method.category = "0.Fill DEM"
in_area_of_lakes = arcpy.Parameter( #Тут мы задаем порог для выделения озер
displayName="Minimum area of lakes only for 'VLADIMIR S. DEKHNICH' method in square kilometers",
name="in_area_of_lakes",
datatype="GPDouble",
parameterType="Optional",
direction="Input")
in_area_of_lakes.value = 1
in_area_of_lakes.category = "0.Fill DEM"
params = [in_dem, out_dem, out_flow_dir, out_flow_accum, out_stream_network, in_threshold_stream_network, out_stream_order, out_stream_feature, out_basin_raster, out_basin_feature,
in_force_flow, in_flow_direction_type, in_flow_accum_data_type, in_flow_accum_direction_type, in_stream_order_method, in_stream_feature_simplify, in_basin_feature_simplify,
in_fill_method, in_area_of_lakes]
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
in_dem = parameters[0].valueAsText
out_dem = parameters[1].valueAsText
out_flow_dir = parameters[2].valueAsText
out_flow_accum = parameters[3].valueAsText
out_stream_network = parameters[4].valueAsText
in_threshold_stream_network = int(parameters[5].valueAsText)
out_stream_order = parameters[6].valueAsText
out_stream_feature = parameters[7].valueAsText
out_basin_raster = parameters[8].valueAsText
out_basin_feature = parameters[9].valueAsText
in_force_flow = parameters[10].valueAsText
in_flow_direction_type = parameters[11].valueAsText
in_flow_accum_data_type = parameters[12].valueAsText
in_flow_accum_direction_type = parameters[13].valueAsText
in_stream_order_method = parameters[14].valueAsText
in_stream_feature_simplify = parameters[15].valueAsText
in_basin_feature_simplify = parameters[16].valueAsText
in_fill_method = parameters[17].valueAsText
in_area_of_lakes = parameters[18].valueAsText.replace(",", ".")
# 1. Fill DEM
if in_fill_method == "PRATIK MUCHKARNI":
outFill = Fill(Float(in_dem))
if out_dem:
outFill.save(out_dem)
elif in_fill_method == "VLADIMIR S. DEKHNICH":
outFlowDirection_alternative = FlowDirection(in_dem)
outSink_alternative = Sink(outFlowDirection_alternative)
outSinkFeature = "in_memory/outSinkFeature"
arcpy.RasterToPolygon_conversion(outSink_alternative, outSinkFeature, "NO_SIMPLIFY")
arcpy.management.AddField(outSinkFeature, "Area", "DOUBLE")
arcpy.CalculateField_management(outSinkFeature, "Area", "!SHAPE.area@SQUAREKILOMETERS!", "PYTHON_9.3")
sink_filtered = "in_memory/outSinkFeature_filtered"
arcpy.analysis.Select(outSinkFeature, sink_filtered, '"Area" > {0}'.format(in_area_of_lakes))
outZonalStatistics = ZonalStatistics(sink_filtered, 'OBJECTID', in_dem,"MEAN", "NODATA")
outFill = Fill(Float(in_dem))
arcpy.Mosaic_management(Int(outZonalStatistics), outFill,"LAST")
if out_dem:
outFill.save(out_dem)
# 2. Flow Direction
outFlowDirection = FlowDirection(outFill, in_force_flow, '', in_flow_direction_type)
if out_flow_dir:
outFlowDirection.save(out_flow_dir)
# 3. Flow Accumulation
outFlowAccumulation = FlowAccumulation(outFlowDirection, '', in_flow_accum_data_type, in_flow_accum_direction_type)
if out_flow_accum:
outFlowAccumulation.save(out_flow_accum)
# 4. Stream Network
if out_stream_network:
outStreamNetwork = Con(outFlowAccumulation > in_threshold_stream_network, 1)
outStreamNetwork.save(out_stream_network)
# 4. Stream Order Raster
if out_stream_order:
outStreamNetwork = Con(outFlowAccumulation > in_threshold_stream_network, 1)
outStreamOrder = StreamOrder(outStreamNetwork, outFlowDirection, in_stream_order_method)
outStreamOrder.save(out_stream_order)
# 5. Stream to Feature
if out_stream_feature:
outStreamNetwork = Con(outFlowAccumulation > in_threshold_stream_network, 1)
outStreamOrder = StreamOrder(outStreamNetwork, outFlowDirection, in_stream_order_method)
StreamToFeature(outStreamOrder, outFlowDirection, out_stream_feature, in_stream_feature_simplify)
# 6. Basin raster
if out_basin_raster:
outBasin = Basin(outFlowDirection)
outBasin.save(out_basin_raster)
# 7. Basin out_stream_feature
if out_basin_feature:
outBasin = Basin(outFlowDirection)
outBasinFeature = arcpy.RasterToPolygon_conversion(outBasin, out_basin_feature, in_basin_feature_simplify)
return