forked from Riverscapes/pyBRAT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBRAT_Braid_Handler.py
More file actions
341 lines (271 loc) · 11.7 KB
/
Copy pathBRAT_Braid_Handler.py
File metadata and controls
341 lines (271 loc) · 11.7 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
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
# -------------------------------------------------------------------------------
# Name: BRAT Braid Handler
# Purpose: Handles the braid clusters in a stream network
#
# Author: Braden Anderson
#
# Created: 03/2018
# -------------------------------------------------------------------------------
import arcpy
import os
from StreamObjects import Cluster, BraidStream
cluster_id = 0 # Provides a consistent way to refer to clusters, that give more information that a UUID
CLUSTERFIELDNAME = "ClusterID"
def main(inputNetwork):
"""
The main function, where we go through the stream network and handle whatever we need to
:param inputNetwork: The stream network that we want to give mainstem values
:return: None
"""
if not hasClusterIDs(inputNetwork):
arcpy.AddMessage("Finding clusters...")
clusters = findClusters(inputNetwork)
handleClusters(inputNetwork, clusters)
else:
arcpy.AddMessage("Finding clusters based on the ClusterID field")
clusters = getClustersFromIDs(inputNetwork)
for i in range(len(clusters)):
clusters[i].id = i+1
updateNetworkDrainageValues(inputNetwork, clusters)
makeLayers(inputNetwork)
def getClustersFromIDs(inputNetwork):
"""
Returns an array of clusters based on the Cluster ID elements that they already have
:param inputNetwork: The stream network
:return: List of clusters
"""
clusters = []
fields = ['SHAPE@', CLUSTERFIELDNAME, "iGeo_DA", 'ReachID']
with arcpy.da.SearchCursor(inputNetwork, fields) as cursor:
for polyline, clusterID, drainageArea, segID in cursor:
if clusterID != -1:
newStream = BraidStream(polyline, segID, drainageArea)
addStreamToClustersWithID(newStream, clusterID, clusters)
return clusters
def addStreamToClustersWithID(newStream, clusterID, clusters):
"""
Adds clusters to the stream based on the
:param newStream: The stream we want to add
:param clusterID: The cluster we want to add it to
:param clusters: The clusters we've already made
:return: None
"""
for cluster in clusters:
if cluster.id == clusterID:
cluster.addStream(newStream)
return #if we found the stream, we end now
# If the for loop didn't return, add a new cluster with the new stream as the first stream
newCluster = Cluster(clusterID)
newCluster.addStream(newStream)
clusters.append(newCluster)
def hasClusterIDs(inputNetwork):
fields = arcpy.ListFields(inputNetwork)
for field in fields:
if field.name == CLUSTERFIELDNAME:
return True
return False
def findClusters(inputNetwork):
"""
Where we find all the clusters in the stream network
:param inputNetwork: The stream network whose clusters we want to find
:return: An array of clusters
"""
clusters = []
fields = ['SHAPE@', 'ReachID', 'iGeo_DA', 'IsBraided']
with arcpy.da.SearchCursor(inputNetwork, fields) as cursor:
for polyline, stream_id, drainageArea, isBraided in cursor:
if isBraided:
addStreamToClusters(clusters, polyline, stream_id, drainageArea)
return clusters
def addStreamToClusters(clusters, polyline, stream_id, drainageArea):
"""
Adds the stream to the list of clusters that we have
:param clusters: A list of clusters
:param polyline: The geoprocessing object that contains a bunch of points we need
:param stream_id: The stream's ID, which we'll use to compare against the original stream when it comes time to update it
:param drainageArea: The stream's drainage area
:return: None
"""
newStream = BraidStream(polyline, stream_id, drainageArea)
connectedClusters = findConnectedClusters(clusters, newStream)
if len(connectedClusters) == 0:
global cluster_id # Allows us to modify cluster_id, so it always keeps count properly
new_cluster = Cluster(cluster_id)
cluster_id += 1
new_cluster.addStream(newStream)
clusters.append(new_cluster)
elif len(connectedClusters) == 1:
i = connectedClusters[0]
clusters[i].addStream(newStream)
elif len(connectedClusters) == 2:
cluster_one = clusters[connectedClusters[0]]
cluster_two = clusters[connectedClusters[1]]
new_cluster = mergeClusters(cluster_one, cluster_two, newStream)
clusters.remove(cluster_one)
clusters.remove(cluster_two)
clusters.append(new_cluster)
def findConnectedClusters(clusters, newStream):
"""
Finds all the clusters that the polyline connects to, and returns an array of all those available
:param clusters: A list of clusters
:param newStream: The stream we care about
:return: A list of indexes for clusters that are connected to the stream
"""
connectedClusters = []
for i in range(len(clusters)):
if isInCluster(newStream, clusters[i]):
connectedClusters.append(i)
return connectedClusters
def isInCluster(stream, cluster):
"""
Returns true if the stream is connected to the cluster, false otherwise
:param stream: The stream we care about
:param cluster: The cluster we're examining
:return: Boolean
"""
streamEndpoints = []
boundary = stream.polyline.boundary()
streamEndpoints.append(boundary.firstPoint)
streamEndpoints.append(boundary.lastPoint)
clusterEndpoints = cluster.endpoints
for clusterEndpoint in clusterEndpoints:
for streamEndpoint in streamEndpoints:
if clusterEndpoint.equals(streamEndpoint):
return True
return False
def mergeClusters(cluster_one, cluster_two, new_stream):
"""
Merges two clusters and a new stream and returns them up
:param cluster_one: The first cluster to merge
:param cluster_two: The second cluster to merge
:param new_stream: The new stream that connects them
:return: A new merged stream
"""
global cluster_id # Allows us to modify cluster_id, so it always keeps count properly
new_cluster = Cluster(cluster_id)
cluster_id += 1
new_cluster.merge(cluster_one, cluster_two)
new_cluster.addStream(new_stream)
return new_cluster
def handleClusters(inputNetwork, clusters):
"""
Takes the clusters and applies the drainage area that we want to it
:param inputNetwork: The network that we were given at first
:param clusters: The list of clusters that we're working with
:return: None
"""
addClusterID(inputNetwork, clusters)
updateNetworkDrainageValues(inputNetwork, clusters)
def addClusterID(inputNetwork, clusters):
"""
Adds cluster ID attribute to the input network
:param inputNetwork: The network to add this info to
:param clusters: The list of clusters that we're dealing with
:return: None
"""
arcpy.AddMessage("Adding clusterID to the input network...")
listFields = arcpy.ListFields(inputNetwork, CLUSTERFIELDNAME)
if len(listFields) is not 1:
arcpy.AddField_management(inputNetwork, CLUSTERFIELDNAME, "SHORT", "", "", "", "", "NULLABLE")
arcpy.CalculateField_management(inputNetwork, CLUSTERFIELDNAME, -1, "PYTHON")
for i in range(len(clusters)):
clusters[i].id = i + 1
with arcpy.da.UpdateCursor(inputNetwork, ['ReachID', CLUSTERFIELDNAME, 'IsBraided']) as cursor:
for row in cursor:
if row[2] == 1: # If the stream is braided. If it isn't, we don't care about its cluster id
for cluster in clusters:
if cluster.containsStream(row[0]):
row[1] = cluster.id
cursor.updateRow(row)
def updateNetworkDrainageValues(inputNetwork, clusters):
"""
Updates all the streams in our clusters based on whether or not they are mainstems
:param inputNetwork: The network we gave as an input
:param clusters: The list of clusters for us to work with
:return: None
"""
arcpy.AddMessage("Updating Drainage Area Values...")
with arcpy.da.UpdateCursor(inputNetwork, ['ReachID', 'IsMainstem', 'iGeo_DA', 'IsBraided']) as cursor:
for row in cursor:
if row[3] == 1:
updateStreamDrainageValue(clusters, row, cursor)
def updateStreamDrainageValue(clusters, row, cursor):
"""
Updates the drainage area values for a single stream in the network
:param clusters: The list of clusters we got earlier
:param row: The row of the input network that we want to update
:param cursor: The cursor we're using for the stream network
:return: None
"""
sidechannelDAValue = 25.0
for cluster in clusters:
if cluster.containsStream(row[0]):
if row[1] == 0: # if it's a side channel
row[2] = sidechannelDAValue
else:
row[2] = cluster.maxDA
cursor.updateRow(row)
def makeLayers(inputNetwork):
"""
Makes the layers for the modified output
:param inputNetwork: The path to the network that we'll make a layer from
:return:
"""
arcpy.AddMessage("Making layers...")
intermediates_folder = os.path.dirname(inputNetwork)
braid_folder_name = findAvailableNum(intermediates_folder) + "_BraidHandler"
braid_folder = makeFolder(intermediates_folder, braid_folder_name)
tribCodeFolder = os.path.dirname(os.path.abspath(__file__))
symbologyFolder = os.path.join(tribCodeFolder, 'BRATSymbology')
mainstemSymbology = os.path.join(symbologyFolder, "Mainstems.lyr")
makeLayer(braid_folder, inputNetwork, "Mainstem_Braids", mainstemSymbology, isRaster=False)
def makeLayer(output_folder, layer_base, new_layer_name, symbology_layer, isRaster, description="Made Up Description"):
"""
Creates a layer and applies a symbology to it
:param output_folder: Where we want to put the folder
:param layer_base: What we should base the layer off of
:param new_layer_name: What the layer should be called
:param symbology_layer: The symbology that we will import
:param isRaster: Tells us if it's a raster or not
:param description: The discription to give to the layer file
:return: The path to the new layer
"""
new_layer = new_layer_name + "_lyr"
new_layer_save = os.path.join(output_folder, new_layer_name + ".lyr")
if isRaster:
arcpy.MakeRasterLayer_management(layer_base, new_layer)
else:
arcpy.MakeFeatureLayer_management(layer_base, new_layer)
arcpy.ApplySymbologyFromLayer_management(new_layer, symbology_layer)
arcpy.SaveToLayerFile_management(new_layer, new_layer_save)
new_layer_instance = arcpy.mapping.Layer(new_layer_save)
new_layer_instance.description = description
new_layer_instance.save()
return new_layer_save
def makeFolder(pathToLocation, newFolderName):
"""
Makes a folder and returns the path to it
:param pathToLocation: Where we want to put the folder
:param newFolderName: What the folder will be called
:return: String
"""
newFolder = os.path.join(pathToLocation, newFolderName)
if not os.path.exists(newFolder):
os.mkdir(newFolder)
return newFolder
def findAvailableNum(folderRoot):
"""
Tells us the next number for a folder in the directory given
:param folderRoot: Where we want to look for a number
:return: A string, containing a number
"""
takenNums = [fileName[0:2] for fileName in os.listdir(folderRoot)]
POSSIBLENUMS = range(1, 100)
for i in POSSIBLENUMS:
stringVersion = str(i)
if i < 10:
stringVersion = '0' + stringVersion
if stringVersion not in takenNums:
return stringVersion
arcpy.AddWarning("There were too many files at " + folderRoot + " to have another folder that fits our naming convention")
return "100"