-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddChannelCatchments.py
More file actions
152 lines (117 loc) · 4.25 KB
/
addChannelCatchments.py
File metadata and controls
152 lines (117 loc) · 4.25 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
# ===========
# Description
# ===========
# This script depends on the output from the "splitcatchments.py" script.
# Catchments for the main stem channels are created and merged with the
# existing split catchments layer.
# ==============
# Load libraries
# ==============
import arcpy
from arcpy import env
from arcpy.sa import *
# ==============
# Specify inputs
# ==============
# Base directory
baseDirectory = "C:/KPONEIL/HRD/V2.1"
# Trunacted flowlines stream grid
flowGrid = "C:/KPONEIL/HRD/V2.1/spatial/source.gdb/strLnkFinal"
# =====================
# Establish directories
# =====================
# Workspace geodatabase
workspace_db = baseDirectory + "/spatial/addChannelCatchments.gdb"
if not arcpy.Exists(workspace_db): arcpy.CreateFileGDB_management (baseDirectory + "/spatial", "addChannelCatchments", "CURRENT")
# Products directory
product_directory = baseDirectory + "/products"
if not arcpy.Exists(product_directory): arcpy.CreateFolder_management(baseDirectory, "products")
# Product geodatabase
products_db = product_directory + "/hydrography.gdb"
if not arcpy.Exists(products_db): arcpy.CreateFileGDB_management (product_directory, "hydrography", "CURRENT")
# Working directory from catchment splitting
split_db = baseDirectory + "/spatial/splitCatchments.gdb"
# ================
# Define functions
# ================
# Delete all fields except those specified
def deleteExtraFields(layer, fieldsToKeep):
fields = arcpy.ListFields(layer)
dropFields = [x.name for x in fields if x.name not in fieldsToKeep]
arcpy.DeleteField_management(layer, dropFields)
# ==========
# Processing
# ==========
# Mainstem catchments
cats = split_db + "/cats"
# Create main channel polygons
# ----------------------------
# Convert stream grid to polygon layer
selectFlowGrid = ExtractByMask(flowGrid, cats)
selectFlowGrid.save(workspace_db + "/select_flow_grid")
flowPolygons = arcpy.RasterToPolygon_conversion(selectFlowGrid,
workspace_db + "/flow_polygons",
"NO_SIMPLIFY",
"VALUE")
# Assign FEATUREIDs
flowPolygonsIDs = arcpy.SpatialJoin_analysis(flowPolygons,
cats,
workspace_db + "/flow_polygons_FIDs",
"JOIN_ONE_TO_ONE",
"KEEP_ALL",
"#",
"HAVE_THEIR_CENTER_IN")
# Create final polygons
mainChannelCats = arcpy.Dissolve_management(flowPolygonsIDs,
workspace_db + "/main_channel_cats",
"FEATUREID",
"",
"MULTI_PART")
# Add NextDownID field
arcpy.AddField_management(mainChannelCats,
"NextDownID",
"DOUBLE")
arcpy.MakeFeatureLayer_management (mainChannelCats,
"mainChannelCatsLyr")
arcpy.AddJoin_management("mainChannelCatsLyr",
"FEATUREID",
cats,
"FEATUREID")
arcpy.CalculateField_management("mainChannelCatsLyr",
"main_channel_cats.NextDownID",
"!cats.NextDownID!",
"PYTHON_9.3")
arcpy.RemoveJoin_management("mainChannelCatsLyr",
"cats")
mainChannelCatsIDs = arcpy.FeatureClassToFeatureClass_conversion("mainChannelCatsLyr",
workspace_db,
"main_channel_cats_IDs")
arcpy.AddField_management(mainChannelCatsIDs,
"NativeID",
"DOUBLE")
arcpy.CalculateField_management(mainChannelCatsIDs,
"NativeID",
"!FEATUREID!",
"PYTHON_9.3")
# Create side catchment polygons
# ------------------------------
sideCats = arcpy.Erase_analysis(split_db + "/mainstem_split_catchments",
mainChannelCats,
workspace_db + "/side_cats")
arcpy.AddField_management(sideCats,
"NextDownID",
"DOUBLE")
arcpy.CalculateField_management(sideCats,
"NextDownID",
"!NativeID!", "PYTHON_9.3")
# Combine catchment layers
# ------------------------
# Join mainstem and side catchments
combinedCats = arcpy.Merge_management([mainChannelCatsIDs, sideCats],
workspace_db + "/combined_cats")
# ====================
# Export final product
# ====================
arcpy.FeatureClassToFeatureClass_conversion(combinedCats,
product_directory,
"mainstemCatchments.shp")