-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGNAT.pyt
2275 lines (1879 loc) · 90.2 KB
/
GNAT.pyt
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Name: Geomorphic Network and Analysis Toolbox (GNAT) #
# Purpose: Tools for generating a stream network and for calculating #
# geomorphic attributes. #
# #
# Authors: Kelly Whitehead ([email protected]) #
# Jesse Langdon ([email protected]) #
# Jean Olson ([email protected] #
# South Fork Research, Inc #
# Seattle, Washington #
# #
# Created: 2015-Jan-08 #
# Version: 2.6.2 #
# Revised: 2018-Map-11 #
# Released: 2018-April-25 #
# #
# License: MIT License #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#!/usr/bin/env python
import os
from os import path, makedirs
import arcpy
from tools import CalculateGradient, CalculateThreadedness, CombineAttributes, DividePolygonBySegment, \
GenerateStreamOrder, GenerateNetworkAttributes, FindBraidedNetwork, FindSubnetworks, GenerateStreamBranches, \
Sinuosity, Segmentation, TransferAttributesToLine, ValleyPlanform, moving_window
from tools.FCT import Centerline
GNAT_version = "2.6.2"
strCatagoryStreamNetworkPreparation = "Analyze Network Attributes\\Step 1 - Stream Network Preparation"
strCatagoryStreamNetworkSegmentation = "Analyze Network Attributes\\Step 2 - Stream Network Segmentation"
strCatagoryGeomorphicAnalysis = "Analyze Network Attributes\\Step 3 - Geomorphic Attributes"
strCatagoryProjectManagement = "Riverscapes Project Management"
strCatagoryUtilities = "Utilities"
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "Geomorphic Network and Analysis Toolbox"
self.alias = 'GNAT'
self.description = "Tools for generating geomorphic attributes for a stream network."
# List of tool classes associated with this toolbox
self.tools = [FindSubnetworksTool,
GenerateNetworkAttributesTool,
GenerateStreamOrderTool,
StreamBranchesTool,
FindBraidedNetworkTool,
SinuosityAttributesTool,
SinuosityTool,
DividePolygonBySegmentsTool,
TransferLineAttributesTool,
FluvialCorridorCenterlineTool,
CombineAttributesTool,
SegmentationTool,
NewGNATProject,
LoadNetworkToProject,
CommitRealization,
CalculateGradientTool,
CalculateThreadednessTool,
MovingWindowSummaryTool]
# GNAT Project Management
class NewGNATProject(object):
"""Define parameter definitions"""
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Create a New GNAT Project"
self.description = "Create a New GNAT Project."
self.canRunInBackground = False
self.category = strCatagoryProjectManagement
def getParameterInfo(self):
"""Define parameter definitions"""
param0 = arcpy.Parameter(
displayName="Project Name",
name="projectName",
datatype="GPString",
parameterType="Required",
direction="Input")
param1 = arcpy.Parameter(
displayName="Project Folder",
name="projectFolder",
datatype="DEWorkspace",
parameterType="Required",
direction="Input")
param1.filter.list = ["File System"]
paramBoolNewFolder = arcpy.Parameter(
displayName="Create New Project Folder?",
name="boolNewFolder",
datatype="GPBoolean",
parameterType="Optional",
direction="Input")
param2 = arcpy.Parameter(
displayName="User Name (Operator)",
name="metaOperator",
datatype="GPString",
parameterType="Optional",
direction="Input")
param3 = arcpy.Parameter(
displayName="Region",
name="metaRegion",
datatype="GPString",
parameterType="Optional",
direction="Input")
param3.filter.list = ["CRB"]
param4 = arcpy.Parameter(
displayName="Watershed (HUC 8 Name)",
name="metaWatershed",
datatype="GPString",
parameterType="Optional",
direction="Input")
#TODO add param4.filter.list = [], load and read from program.xml
params = [param0,param1,paramBoolNewFolder,param2,param3,param4]
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, p, messages):
"""The source code of the tool."""
from Riverscapes import Riverscapes
arcpy.AddMessage(p[2].valueAsText)
projectFolder = p[1].valueAsText
if p[2].valueAsText == "true":
projectFolder = path.join(p[1].valueAsText,p[0].valueAsText)
makedirs(projectFolder)
GNATProject = Riverscapes.Project()
GNATProject.create(p[0].valueAsText,"GNAT",projectPath=projectFolder)
GNATProject.addProjectMetadata("GNAT_Project_Version","0.1")
GNATProject.addProjectMetadata("Operator",p[3].valueAsText)
GNATProject.addProjectMetadata("Region",p[4].valueAsText)
GNATProject.addProjectMetadata("Watershed",p[5].valueAsText)
GNATProject.addProjectMetadata("GIS","Arc/ESRI")
GNATProject.writeProjectXML()
return
class LoadNetworkToProject(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Load Input Datsets"
self.description = "Load Input Stream Network to GNAT Project. Tool Documentation: https://bitbucket.org/KellyWhitehead/geomorphic-network-and-analysis-toolbox/wiki/Tool_Documentation/MovingWindow"
self.canRunInBackground = False
self.category = strCatagoryProjectManagement
def getParameterInfo(self):
"""Define parameter definitions"""
p1 = paramStreamNetwork
paramNetworkTable = arcpy.Parameter(
displayName="Network Table",
name="tblNetwork",
datatype="DETable",
parameterType="Optional",
direction="Input")
params = [paramProjectXML,
p1,
paramNetworkTable]
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, p, messages):
"""The source code of the tool."""
from Riverscapes import Riverscapes
GNATProject = Riverscapes.Project(p[0].valueAsText)
pathProject = GNATProject.projectPath
# Create Project Paths if they do not exist
pathInputs = pathProject + "\\Inputs"
if not arcpy.Exists(pathInputs):
makedirs(pathInputs)
# KMW - The following is a lot of repeated code for each input. It contains file and folder creation and copying, rather than useing the project module to do this. This could be streamlined in the future, but
# is working at the moment.
if p[1].valueAsText: # Stream Network Input
pathStreamNetworks = pathInputs + "\\StreamNetworks"
nameStreamNetwork = arcpy.Describe(p[1].valueAsText).basename
if not arcpy.Exists(pathStreamNetworks):
makedirs(pathStreamNetworks)
id_streamnetwork = Riverscapes.get_input_id(pathStreamNetworks, "StreamNetwork")
pathStreamNetworkID = path.join(pathStreamNetworks, id_streamnetwork)
makedirs(pathStreamNetworkID)
arcpy.FeatureClassToFeatureClass_conversion(p[1].valueAsText, pathStreamNetworkID, nameStreamNetwork)
GNATProject.addInputDataset(nameStreamNetwork,
id_streamnetwork,
path.join(path.relpath(pathStreamNetworkID, pathProject),
nameStreamNetwork) + ".shp",
p[1].valueAsText)
if p[2].value:
id_streamnetworkTable = Riverscapes.get_input_id(pathStreamNetworks, "StreamNetworkTable")
extStreamNetworkTable = arcpy.Describe(p[2].valueAsText).extension
nameStreamNetworkTable = arcpy.Describe(p[2].valueAsText).basename
arcpy.TableToTable_conversion(p[2].valueAsText, pathStreamNetworkID, nameStreamNetworkTable)
GNATProject.addInputDataset(nameStreamNetworkTable,
id_streamnetworkTable,
path.join(path.relpath(pathStreamNetworkID, pathProject),
nameStreamNetworkTable) + "." + extStreamNetworkTable,
p[2].valueAsText)
# Write new XML
GNATProject.writeProjectXML(p[0].valueAsText)
return
class CommitRealization(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Commit Stream Network"
self.description = "Commit changes to the stream network as a new realization in a GNAT project."
self.canRunInBackground = False
self.category = strCatagoryProjectManagement
return
def getParameterInfo(self):
"""Define parameter definitions"""
paramRealization = arcpy.Parameter(
displayName="Realization Name",
name="realization",
datatype="GPString",
parameterType="Required",
direction="Input")
paramIDField = arcpy.Parameter(
displayName="Unique Reach ID Field",
name="reachIDfield",
datatype="Field",
parameterType="Optional",
direction="Input")
paramIDField.parameterDependencies = [paramStreamNetwork.name]
paramNetworkTable = arcpy.Parameter(
displayName="Network Table",
name="tblNetwork",
datatype="DETable",
parameterType="Optional",
direction="Input")
params = [paramProjectXML, # 0
paramRealization, # 1
paramStreamNetwork, # 2
paramIDField, # 3
paramNetworkTable] # 4
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."""
from Riverscapes import Riverscapes
if parameters[0].valueAsText:
GNATProject = Riverscapes.Project(parameters[0].valueAsText)
for realization in GNATProject.Realizations:
if realization == parameters[1].valueAsText:
parameters[1].setErrorMessage("Realization " + parameters[1].valueAsText + " already exists.")
return
return
def execute(self, p, messages):
"""The source code of the tool."""
from Riverscapes import Riverscapes
# if in project mode, create workspaces as needed.
if p[0].valueAsText:
GNATProject = Riverscapes.Project(p[0].valueAsText)
if p[1].valueAsText:
outPath = path.join(GNATProject.projectPath, "Outputs",p[1].valueAsText)
makedirs(outPath)
outputGNATNetwork = path.join(outPath, "GNAT_StreamNetwork") + ".shp"
outputNetworkTable = path.join(outPath , "GNAT_NetworkTable") + ".dbf"
# todo if not idfield, then create one named NetID
# Stream Network
idRawStreamNetwork = GNATProject.get_dataset_id(p[2].valueAsText)
arcpy.Copy_management(p[2].valueAsText,outputGNATNetwork)
datasetGNATNetwork = Riverscapes.Dataset()
datasetGNATNetwork.create("GNAT_StreamNetwork",
path.relpath(outputGNATNetwork, GNATProject.projectPath))
datasetGNATNetwork.id = p[1].valueAsText + "_GNAT_StreamNetwork"
#datasetGNATNetwork.type = "GNAT_StreamNetwork"
# Todo Get all fields as Meta for input or realization?
# Network Table (Optional)
idRawNetworkTable = None
datasetGNATNetworkTable = None
if p[4].value:
if arcpy.Exists(p[4].valueAsText):
idRawNetworkTable = GNATProject.get_dataset_id(p[3].valueAsText)
arcpy.Copy_management(p[4].valueAsText,outputNetworkTable)
datasetGNATNetworkTable = Riverscapes.Dataset()
datasetGNATNetworkTable.create("GNAT_NetworkTable",
path.relpath(outputNetworkTable, GNATProject.projectPath))
datasetGNATNetworkTable.type = "Table"
datasetGNATNetworkTable.id = p[1].valueAsText + "GNAT_NetworkTable"
realization = Riverscapes.GNATRealization()
realization.createGNATRealization(p[1].valueAsText,
idRawStreamNetwork,
datasetGNATNetwork,
idRawNetworkTable,
datasetGNATNetworkTable)
realization.productVersion = str(GNAT_version)
realization.parameters["FieldOriginalReachID"] = p[3].valueAsText
GNATProject.addRealization(realization)
GNATProject.writeProjectXML(p[0].valueAsText)
return
# Stream Network Prep Tools
class FindSubnetworksTool(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Find Subnetworks"
self.description = "Finds disconnected subnetworks within a stream network."
self.canRunInBackground = False
self.category = strCatagoryStreamNetworkPreparation
def getParameterInfo(self):
"""Define parameter definitions"""
param0 = arcpy.Parameter(
displayName="Input stream network shapefle",
name="InputStreamNetwork",
datatype="DEShapefile",
parameterType="Required",
direction="Input")
param0.filter.list = ["Polyline"]
param1 = arcpy.Parameter(
displayName="Output shapefile",
name="OutputStreamNetwork",
datatype="DEShapefile",
parameterType="Required",
direction="Output")
param2 = arcpy.Parameter(
displayName="Find topology errors",
name="BoolError",
datatype="GPBoolean",
parameterType="Optional",
direction="Input")
return [param0, param1, param2]
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, p):
"""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, p, messages):
"""The source code of the tool."""
reload(FindSubnetworks)
# testFType(p[0].valueAsText, 336) # check to see if canals have been removed from input feature class
FindSubnetworks.main(p[0].valueAsText,
p[1].valueAsText,
p[2].valueAsText)
return
class GenerateNetworkAttributesTool(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Generate Network Attributes"
self.description = "Generates a series of network attributes, including edge type, node type, river kilometers," \
"and stream order."
self.canRunInBackground = False
self.category = strCatagoryStreamNetworkPreparation
def getParameterInfo(self):
"""Define parameter definitions"""
param0 = arcpy.Parameter(
displayName="Input stream network polyline feature class",
name="InputStreamNetwork",
datatype="DEShapefile",
parameterType="Required",
direction="Input")
param0.filter.list = ["Polyline"]
param1 = arcpy.Parameter(
displayName="Primary stream name field (i.e. GNIS Name)",
name="StreamNameField",
datatype="GPString",
parameterType="Required",
direction="Input")
param2 = arcpy.Parameter(
displayName="Output polyline feature class",
name="OutputStreamNetwork",
datatype="DEShapefile",
parameterType="Required",
direction="Output")
param3 = arcpy.Parameter(
displayName="Calculate river kilometers",
name="BoolRiverKM",
datatype="GPBoolean",
parameterType="Optional",
direction="Input")
return [param0, param1, param2, param3]
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."""
if parameters[0].altered:
fields = arcpy.ListFields(parameters[0].value)
field_names = []
for field in fields:
field_names.append(field.name)
if field.name == "GNIS_Name" or field.name == "GNIS_NAME" or field.name == "GNIS_name" or field.name == "gnis_name":
parameters[1].value = field.name
parameters[1].filter.type = "ValueList"
parameters[1].filter.list = field_names
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, p, messages):
"""The source code of the tool."""
reload(GenerateNetworkAttributes)
GenerateNetworkAttributes.main(p[0].valueAsText,
p[1].valueAsText,
p[2].valueAsText,
p[3].valueAsText)
return
class GenerateStreamOrderTool(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Generate Strahler Stream Order"
self.description = "Generate Strahler stream order for the stream network"
self.canRunInBackground = True
self.category = strCatagoryStreamNetworkPreparation
def getParameterInfo(self):
"""Define parameter definitions"""
param0 = arcpy.Parameter(
displayName="Input stream network shapefile",
name="InputStreamNetwork",
datatype="DEShapefile",
parameterType="Required",
direction="Input")
param0.filter.list = ["Polyline"]
param1 = arcpy.Parameter(
displayName="Primary stream name field (i.e. GNIS Name)",
name="StreamNameField",
datatype="GPString",
parameterType="Required",
direction="Input")
param2 = arcpy.Parameter(
displayName="Output network shapefile with stream order",
name="OutputStreamNetwork",
datatype="DEShapefile",
parameterType="Required",
direction="Output")
param2.filter.list = ["Polyline"]
param3 = arcpy.Parameter(
displayName="Temporary workspace",
name="TempWorkspace",
datatype="DEWorkspace",
parameterType="Required",
direction="Input")
param3.filter.list = ["Workspace"]
return [param0, param1, param2, param3]
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."""
if parameters[0].altered:
fields = arcpy.ListFields(parameters[0].value)
field_names = []
for field in fields:
field_names.append(field.name)
if field.name == "GNIS_Name" or field.name == "GNIS_NAME" or field.name == "GNIS_name" or field.name == "gnis_name":
parameters[1].value = field.name
parameters[1].filter.type = "ValueList"
parameters[1].filter.list = field_names
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
testProjected(parameters[0])
testMValues(parameters[0])
return
def execute(self, p, messages):
"""The source code of the tool."""
reload(GenerateStreamOrder)
GenerateStreamOrder.main(p[0].valueAsText,
p[1].valueAsText,
p[2].valueAsText,
getTempWorkspace(p[3].valueAsText))
class StreamBranchesTool(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Generate Stream Branches"
self.description = "Generate stream branch IDs for the stream network."
self.canRunInBackground = True
self.category = strCatagoryStreamNetworkPreparation
def getParameterInfo(self):
"""Define parameter definitions"""
param0 = arcpy.Parameter(
displayName="Input Stream Network (with Stream Order)",
name="InputStreamNetwork",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
param0.filter.list = ["Polyline"]
param1 = arcpy.Parameter(
displayName="Input Stream Network Nodes",
name="InputNetworknodes",
datatype="GPFeatureLayer",
parameterType="Optional",
direction="Input")
param1.filter.list = ["Point", "Multipoint"]
param2 = arcpy.Parameter(
displayName="Primary Stream Name Field (i.e. GNIS Name)",
name="fieldStreamName",
datatype="GPString",
parameterType="Required",
direction="Input")
param3 = arcpy.Parameter(
displayName="Stream Order Field",
name="fieldStreamOrder",
datatype="GPString",
parameterType="Optional",
direction="Input")
param4 = arcpy.Parameter(
displayName="Output Line Network with Branch ID",
name="outputStreamOrderFC",
datatype="DEFeatureClass",
parameterType="Required",
direction="Output")
param4.filter.list = ["Polyline"]
param5 = arcpy.Parameter(
displayName="Dissolve Output Network by BranchID?",
name="boolDissolve",
datatype="GPBoolean",
parameterType="Optional",
direction="Input")
param6 = arcpy.Parameter(
displayName="Scratch Workspace",
name="InputTempWorkspace",
datatype="DEWorkspace",
parameterType="Optional",
direction="Input")
param6.filter.list = ["Local Database"]
return [param0, param1, param2, param3, param4, param5, param6]
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."""
if parameters[0].altered:
fields = arcpy.ListFields(parameters[0].value)
field_names = []
for field in fields:
field_names.append(field.name)
if field.name == "GNIS_Name":
parameters[2].value = field.name
if field.name == "_strmordr_":
parameters[3].value = field.name
parameters[2].filter.type = "ValueList"
parameters[2].filter.list = field_names
parameters[3].filter.type = "ValueList"
parameters[3].filter.list = field_names
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
testProjected(parameters[0])
testProjected(parameters[1])
testLayerSelection(parameters[0])
testLayerSelection(parameters[1])
testWorkspacePath(parameters[6])
return
def execute(self, p, messages):
"""The source code of the tool."""
reload(GenerateStreamBranches)
GenerateStreamBranches.main(p[0].valueAsText,
p[1].valueAsText,
p[2].valueAsText,
p[3].valueAsText,
p[4].valueAsText,
p[5].valueAsText,
getTempWorkspace(p[6].valueAsText))
return
# Stream Segmentation
class SegmentationTool(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Segment Stream Network"
self.description = "Segment a stream network polyline feature class."
self.canRunInBackground = True
self.category = strCatagoryStreamNetworkSegmentation
def getParameterInfo(self):
"""Define parameter definitions"""
reload(Segmentation)
paramInStreamNetwork = arcpy.Parameter(
displayName="Stream network polyline feature class",
name="InputStreamNetwork",
datatype="DEFeatureClass",
parameterType="Required",
direction="Input")
paramInStreamNetwork.filter.list = ["Polyline"]
paramSegmentLength = arcpy.Parameter(
displayName="Segment length",
name="InputSegmentDistance",
datatype="GPDouble",
parameterType="Required",
direction="Input")
paramSegmentLength.value = "200"
paramFieldStreamName = arcpy.Parameter(
displayName="Stream name field",
name="streamIndex",
datatype="Field",
parameterType="Required",
direction="Input")
paramFieldStreamName.parameterDependencies = [paramInStreamNetwork.name]
paramSegmentationMethod = arcpy.Parameter(
displayName="Segmentation method",
name="strSegmentationMethod",
datatype="GPString",
parameterType="Required",
direction="Input")
paramSegmentationMethod.filter.list = Segmentation.listStrSegMethod
paramBoolSplitAtConfluences = arcpy.Parameter(
displayName="Split stream network at confluences before segmenting",
name="boolNode",
datatype="GPBoolean",
parameterType="Optional",
direction="Input")
paramBoolRetainOrigAttributes = arcpy.Parameter(
displayName="Retain original attributes and geometry from input stream network",
name="boolKeepOrig",
datatype="GPBoolean",
parameterType="Optional",
direction="Input")
# TODO if project mode, this is always yes.
paramOutputSegmentedNetwork = arcpy.Parameter(
displayName="Output segmented line network",
name="outputStreamOrderFC",
datatype="DEFeatureClass",
parameterType="Required",
direction="Output")
return [paramInStreamNetwork, #p[0]
paramSegmentLength, #p[1]
paramFieldStreamName, #p[3] -> 2
paramSegmentationMethod, #p[4] -> 3
paramBoolSplitAtConfluences, #p[5] -> 4
paramBoolRetainOrigAttributes, #p[6] -> 5
paramOutputSegmentedNetwork, #p[7] -> 6
paramProjectXML, #p[8] -> 7
paramRealization, #p[9] -> 8
paramSegmentAnalysisName] #p[10] -> 9
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, p):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
from Riverscapes import Riverscapes
if p[7].value:
if arcpy.Exists(p[7].valueAsText):
GNATProject = Riverscapes.Project(p[7].valueAsText)
p[8].enabled = "True"
p[8].filter.list = GNATProject.Realizations.keys()
p[6].enabled = "False"
if p[8].value:
currentRealization = GNATProject.Realizations.get(p[8].valueAsText)
p[0].value = currentRealization.GNAT_StreamNetwork.absolutePath(GNATProject.projectPath)
p[9].enabled = "True"
if p[9].value:
p[6].value = path.join(GNATProject.projectPath, "Outputs", p[8].valueAsText, "Analyses",
p[9].valueAsText, "SegmentedNetwork") + ".shp"
else:
p[8].filter.list = []
p[8].value = ''
p[8].enabled = "False"
p[9].value = ""
p[6].enabled = "True"
populateFields(p[0],p[2],"GNIS_Name")
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
# todo Check if analysis name already exists
testProjected(parameters[0])
return
def execute(self, p, messages):
"""The source code of the tool."""
reload(Segmentation)
from Riverscapes import Riverscapes
output = p[6].valueAsText
# Where the tool outputs will be store for Riverscapes projects
if p[7].value:
GNATProject = Riverscapes.Project()
GNATProject.loadProjectXML(p[7].valueAsText)
if p[8].valueAsText:
makedirs(path.join(GNATProject.projectPath, "Outputs", p[8].valueAsText, "Analyses",
p[9].valueAsText))
output = path.join(GNATProject.projectPath, "Outputs", p[8].valueAsText, "Analyses",
p[9].valueAsText, "SegmentedNetwork") + ".shp"
# Main tool module
Segmentation.main(p[0].valueAsText,
p[1].valueAsText,
p[2].valueAsText,
p[3].valueAsText,
p[4].valueAsText,
p[5].valueAsText,
output)
# Add tool run to the Riverscapes project XML
if p[7].value:
if arcpy.Exists(p[7].valueAsText):
GNATProject = Riverscapes.Project(p[7].valueAsText)
outSegmentedNetwork = Riverscapes.Dataset()
outSegmentedNetwork.create(arcpy.Describe(output).basename,
path.join("Outputs", str(p[8].value), "Analyses", str(p[9].value),
arcpy.Describe(output).basename + ".shp"),
"SegmentedNetwork")
outSegmentedNetwork.id = "SegmentedNetwork"
realization = GNATProject.Realizations.get(p[8].valueAsText)
realization.newAnalysisNetworkSegmentation(p[9].valueAsText,
p[1].valueAsText,
"NONE",
p[2].valueAsText,
p[3].valueAsText,
p[4].valueAsText,
p[5].valueAsText,
p[6].valueAsText,
outSegmentedNetwork)
GNATProject.Realizations[p[8].valueAsText] = realization
GNATProject.writeProjectXML()
return
class MovingWindowSummaryTool(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Moving Window Summary"
self.description = "Generate Moving Window Segment for a stream network."
self.canRunInBackground = True
self.category = strCatagoryUtilities
def getParameterInfo(self):
"""Define parameter definitions"""
paramInStreamNetwork = arcpy.Parameter(
displayName="Stream network polyline feature class",
name="InputStreamNetwork",
datatype="DEFeatureClass",
parameterType="Required",
direction="Input")
paramInStreamNetwork.filter.list = ["Polyline"]
paramFieldStreamName = arcpy.Parameter(
displayName="Stream name or Branch field",
name="streamIndex",
datatype="Field",
parameterType="Required",
direction="Input")
paramFieldStreamName.parameterDependencies = [paramInStreamNetwork.name]
param_seeddistance = arcpy.Parameter(
displayName="Seed Point Distance",
name="InputSegmentDistance",
datatype="GPDouble",
parameterType="Required",
direction="Input")
param_seeddistance.value = "200"
param_windowsizes = arcpy.Parameter(
displayName="Window Sizes",
name="InputWindowSizes",
datatype="GPDouble",
parameterType="Required",
direction="Input",
multiValue=True)
param_stat_fields = arcpy.Parameter(
displayName="Calculate Statistics on Field(s)",
name="statfields",
datatype="GPString",
parameterType="Required",
direction="Input",
multiValue=True)
# param_stat_fields.filter.list = ['Text']
# param_stat_fields.parameterDependencies = [paramInStreamNetwork.name]
paramOutputSegmentedNetwork = arcpy.Parameter(
displayName="Output Moving Windows",
name="outputStreamOrderFC",
datatype="DEFeatureClass",
parameterType="Required",
direction="Output")
paramOutputSeedPoints = arcpy.Parameter(
displayName="Output Seed Points",
name="outputSeedPointFC",
datatype="DEFeatureClass",
parameterType="Required",
direction="Output")
return [paramInStreamNetwork, # p[0]
paramFieldStreamName, # p[1]
param_seeddistance, # p[2]
param_windowsizes, # p[3]
param_stat_fields, # p[4]
paramOutputSegmentedNetwork, # p[5]
paramOutputSeedPoints, # p[6]
paramProjectXML, # p[7]
paramRealization, # p[8]
paramSegmentAnalysisName, # p[9]
paramTempWorkspace] # p[10]
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, p):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
from Riverscapes import Riverscapes
if p[7].value:
if arcpy.Exists(p[7].valueAsText):
GNATProject = Riverscapes.Project(p[7].valueAsText)
p[8].enabled = "True"
p[8].filter.list = GNATProject.Realizations.keys()
p[5].enabled = "False"
p[6].enabled = "False"
if p[8].value: