-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLSDJunctionNetwork.cpp
8978 lines (7860 loc) · 349 KB
/
LSDJunctionNetwork.cpp
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
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// LSDJunctionNetwork
// Land Surface Dynamics ChannelNetwork
//
// An object within the University
// of Edinburgh Land Surface Dynamics group topographic toolbox
// for organizing channel routing under the Fastscape algorithm
// (see Braun and Willett, Geomorphology 2013, v180, p 170-179)
// It uses the algorithm to create channel junction networks
// that can be searched for network connectivity
//
//
// Developed by:
// Simon M. Mudd
// Martin D. Hurst
// David T. Milodowski
// Stuart W.D. Grieve
// Declan A. Valters
// Fiona Clubb
//
// Copyright (C) 2013 Simon M. Mudd 2013
//
// Developer can be contacted by simon.m.mudd _at_ ed.ac.uk
//
// Simon Mudd
// University of Edinburgh
// School of GeoSciences
// Drummond Street
// Edinburgh, EH8 9XP
// Scotland
// United Kingdom
//
// This program is free software;
// you can redistribute it and/or modify it under the terms of the
// GNU General Public License as published by the Free Software Foundation;
// either version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;
// without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the
// GNU General Public License along with this program;
// if not, write to:
// Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301
// USA
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// LSDJunctionNetwork.cpp
// LSDJunctionNetwork object
// LSD stands for Land Surface Dynamics
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// This object is written by
// Simon M. Mudd, University of Edinburgh
// David Milodowski, University of Edinburgh
// Martin D. Hurst, British Geological Survey
// Stuart Grieve, University of Edinburgh
// <your name here>
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Version 1.0.0 15/07/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//-----------------------------------------------------------------
//DOCUMENTATION URL: http://www.geos.ed.ac.uk/~s0675405/LSD_Docs/
//-----------------------------------------------------------------
#ifndef LSDJunctionNetwork_CPP
#define LSDJunctionNetwork_CPP
#include <vector>
#include <string>
#include <fstream>
#include <map>
#include <algorithm>
#include "TNT/tnt.h"
#include "LSDFlowInfo.hpp"
#include "LSDRaster.hpp"
#include "LSDChannel.hpp"
#include "LSDJunctionNetwork.hpp"
#include "LSDIndexChannel.hpp"
#include "LSDStatsTools.hpp"
#include "LSDShapeTools.hpp"
using namespace std;
using namespace TNT;
/// @brief the copy constructor
LSDJunctionNetwork& LSDJunctionNetwork::operator=(const LSDJunctionNetwork& rhs)
{
if (&rhs != this)
{
NRows = rhs.NRows;
NCols = rhs.NCols;
XMinimum = rhs.XMinimum;
YMinimum = rhs.YMinimum;
DataResolution = rhs.DataResolution;
NoDataValue = rhs.NoDataValue;
NJunctions = rhs.NJunctions;
GeoReferencingStrings = rhs.GeoReferencingStrings;
SourcesVector = rhs.SourcesVector;
BaseLevelJunctions = rhs.BaseLevelJunctions;
JunctionVector = rhs.JunctionVector;
StreamOrderVector = rhs.StreamOrderVector;
BLBasinVector = rhs.BLBasinVector;
NDonorsVector = rhs.NDonorsVector;
ReceiverVector = rhs.ReceiverVector;
DeltaVector = rhs.DeltaVector;
DonorStackVector = rhs.DonorStackVector;
SVector = rhs.SVector;
SVectorIndex = rhs.SVectorIndex;
NContributingJunctions = rhs.NContributingJunctions;
StreamOrderArray = rhs.StreamOrderArray.copy();
JunctionArray = rhs.JunctionArray.copy();
JunctionIndexArray = rhs.JunctionIndexArray.copy();
}
return *this;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This constructor does nothing but allows copying of these objects
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDJunctionNetwork::create( void )
{
cout << "I am an empty LSDJunctionNetwork" << endl;
vector<int> emptyvec;
SourcesVector = emptyvec;
BaseLevelJunctions = emptyvec;
JunctionVector = emptyvec;
StreamOrderVector = emptyvec;
BLBasinVector = emptyvec;
NDonorsVector = emptyvec;
ReceiverVector = emptyvec;
DeltaVector = emptyvec;
DonorStackVector = emptyvec;
SVector = emptyvec;
SVectorIndex = emptyvec;
NContributingJunctions = emptyvec;
Array2D<int> emptyarray(0,0);
StreamOrderArray = emptyarray.copy();
JunctionArray = emptyarray.copy();
JunctionIndexArray = emptyarray.copy();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// create
// this defines a channel network based on a FlowInfo object
// and a list of source nodes
//
// SMM 01/09/2012
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDJunctionNetwork::create(vector<int> Sources, LSDFlowInfo& FlowInfo)
{
NRows = FlowInfo.NRows;
NCols = FlowInfo.NCols;
XMinimum = FlowInfo.XMinimum;
YMinimum = FlowInfo.YMinimum;
DataResolution = FlowInfo.DataResolution;
NoDataValue = FlowInfo.NoDataValue;
GeoReferencingStrings = FlowInfo.GeoReferencingStrings;
SourcesVector = Sources;
// start arrays where the data all begins as nodata
Array2D<int> TempLinkArray(NRows,NCols,NoDataValue);
JunctionArray = TempLinkArray.copy();
StreamOrderArray = TempLinkArray.copy();
JunctionIndexArray = TempLinkArray.copy();
vector<int> TempVector;
JunctionVector = TempVector;
BaseLevelJunctions = TempVector;
// we loop through the sources file
// for each source we burn down to either base level
// or the next channel
//
// with link numbers by adding integers
//
int n_sources = SourcesVector.size();
int current_node;
int current_row,current_col;
int receiver_node;
int baselevel_switch; // 0 if not a base level node, 1 if so
int current_stream_order;
int junction_switch;
int donor_node, donor_row,donor_col;
int n_current_stream_order_donors;
// loop through sources.
// this loop sets the stream orders and identifies the
// junctions
// it is the first of two loops through the stream network
// the second will generate the stream link information
for(int src = 0; src<n_sources; src++)
{
baselevel_switch =0; // 0 == not base level
junction_switch = 0; // 0 == no junction so far
current_node = SourcesVector[src];
current_row = FlowInfo.RowIndex[current_node];
current_col = FlowInfo.ColIndex[current_node];
receiver_node = FlowInfo.ReceiverVector[current_node];
current_stream_order = 1;
if (current_node == receiver_node)
{
baselevel_switch = 1;
}
// follow node down through receivers until it hits a base level node
// the switch is set to 2 because if there is a baselevel node
// the algorithm has to go through the loop once to register the
// 'downslope node' as the baselevel
while ( baselevel_switch <2 )
{
// check if this node has already been added to the channel network
// if it hasn't, then this becomes a channel of order of the current order
if(StreamOrderArray[current_row][current_col] == NoDataValue)
{
StreamOrderArray[current_row][current_col] = current_stream_order;
}
// if it isn't a nodata node:
// note that the junction switch starts out as a zero.
// the channel is followed looking at nodata values in the stream order array
// once it hits the first junction, the StreamOrderArray has finite values,
// so this logic is triggered.
else if(StreamOrderArray[current_row][current_col] != NoDataValue)
{
// each source contributes a junction unless it is the
// only source to go to a given baselevel node
// check to see if this is the first time the channel
// has hit another channel. If so, add the junction
// and set the junction switch to 1 so that no further
// junctions are added
// also if it is a junction check to see if the stream order has been incremented
// if it has not, the base level switch is turn to 2 and the
// algorithm exits this loop and goes to the next source
// if it has, this and all downstream nodes take on the new stream order
// if junction switch is zero it means this is the first visit of a previously visited channel
if (junction_switch == 0)
{
junction_switch = 1;
JunctionArray[current_row][current_col] = 1;
// if it is the the first junction for this source, the current_stream_order
// is one. Therefore any junction will result in a stream order
// of at least 2.
// If the junction is currently at a stream order of 1, then it
// gets incremented
if (StreamOrderArray[current_row][current_col] == current_stream_order)
{
current_stream_order ++;
StreamOrderArray[current_row][current_col]= current_stream_order;
}
// if the junction is two or greater, the loop exits since there can
// be no more incrementing of the stream order
else
{
baselevel_switch = 2;
}
}
else
{
// first, we check to see if it is not a junction. if not, we update the
// stream order. If the stream order hasn't changed, then something
// is amiss since there is no point moving downstream
// when the current stream order is the same as the previous stream order,
// since it can't increase stream order downstream
// nodes following downstream will be at the current stream order
if (JunctionArray[current_row][current_col] != 1)
{
// THIS IS NOT A JUNCTION
// if the current stream order is bigger than the existing stream order
// at this point, then increase the existing stream order
if ( current_stream_order > StreamOrderArray[current_row][current_col])
{
StreamOrderArray[current_row][current_col] = current_stream_order;
}
else
{
baselevel_switch = 2;
}
}
// if it is a junction, see if there is an increment
// in the current stream order
// the node it has come from has an updated current stream order
// so just look at the donor nodes to
// see if there are two or more
// donors at a higher stream order
else if (JunctionArray[current_row][current_col] == 1)
{
// THIS IS A JUNCTION
// first, check to see if this has a higher stream order than the current stream
// order
if (StreamOrderArray[current_row][current_col] > current_stream_order)
{
// yes, the stream order at this junction is higher than the current stream order
// even if the junction is incremented the downstream search would
// stop here, so get out with the baselevel switch
baselevel_switch = 2;
}
else if ( StreamOrderArray[current_row][current_col] == current_stream_order)
{
// this means that the current stream order is equal to or less than the streamorder
// currently at this node this means you need to check and see if the thing is incremented
n_current_stream_order_donors = 0;
for(int dnode = 0; dnode<FlowInfo.NDonorsVector[current_node]; dnode++)
{
donor_node = FlowInfo.DonorStackVector[ FlowInfo.DeltaVector[current_node]+dnode];
// ignore the base level donor
if (donor_node != current_node)
{
donor_row = FlowInfo.RowIndex[ donor_node ];
donor_col = FlowInfo.ColIndex[ donor_node ];
if (StreamOrderArray[donor_row][donor_col] == current_stream_order)
{
n_current_stream_order_donors++;
}
}
}
// now check to see if the stream order has increased
if (n_current_stream_order_donors >= 2)
{
current_stream_order++;
StreamOrderArray[current_row][current_col] = current_stream_order;
}
else // if it hasn't, the loop ends here.
{
baselevel_switch = 2;
}
}
else if (StreamOrderArray[current_row][current_col] < current_stream_order)
{
// the current stream order is higher than the stream order at this point.
// the node needs to update its stream order and keep going
StreamOrderArray[current_row][current_col] = current_stream_order;
}
else
{
cout << "something about the logic has gone wrong. " <<endl;
cout << "hhh node: " << current_node << ", current_stream_order " << current_stream_order
<< " array: " << StreamOrderArray[current_row][current_col]<<" and src: " << SourcesVector[src] << endl;
}
} // end logic for if this is a downstream junction
} // end logic for if this is not the first junction
} // end logic for if this is not a NoData node
// get the next current node, which is this nodes receiver
current_node = FlowInfo.ReceiverVector[current_node];
// get the next receiver node, which is the next node
receiver_node = FlowInfo.ReceiverVector[current_node];
current_row = FlowInfo.RowIndex[current_node];
current_col = FlowInfo.ColIndex[current_node];
// if this is a baselevel node
if (current_node == receiver_node)
{
baselevel_switch ++;
}
} // end flow to baselevel loop
} // end sources loop
// now you need to loop through the sources once more, creating links
// each link has a starting node, and ending node
// a stream order
// a starting link and an ending link
//
// this should be arranged in an analagous way to the fastscape algorithm
// all sources are on 1st order links
int this_junction = -1;
for(int src = 0; src<n_sources; src++)
{
this_junction++; // increment the last junction
baselevel_switch =0; // 0 == not base level
junction_switch = 0; // 0 == no junction so far
current_node = SourcesVector[src];
current_row = FlowInfo.RowIndex[current_node];
current_col = FlowInfo.ColIndex[current_node];
receiver_node = FlowInfo.ReceiverVector[current_node];
//cout << "LINE 257 ChNet, SOURCE: " << src << " n_src: " << n_sources << " current_node: " << current_node
// << " and rnode: " << receiver_node << endl;
//each source is a junction node. Push back the junction vector
JunctionVector.push_back(current_node);
// set the junction Index Array
JunctionIndexArray[current_row][current_col] = this_junction;
// stream order only increases at junctions. So the junction node has a stream
// order that remains the same until it gets to the next junction, where it possibly
// could change
StreamOrderVector.push_back( StreamOrderArray[current_row][current_col] );
// check if this is a baselevel node
if(receiver_node == current_node)
{
baselevel_switch = 1; // turn the baselevel switch on
ReceiverVector.push_back(this_junction); // the Receiver node is iteself
// this logic only applies to sources, which cannot lie downstream of another source
// ***THIS MUST BE ENFORCED BY THE GET_SOURCES ALGORITHM***
// this means that if a source is also a baselevel, then this is the one and only time
// this baselevel junction is added, so add it to the baselevel vector
BaseLevelJunctions.push_back(this_junction);
}
// the next element is the receiver junction, we need to follow the path to this receiver
// the routine continues until the junction has been visited or until it hits
// a baselevel node
//cout << "LINE 280" << endl;
while (baselevel_switch == 0 && junction_switch <2)
{
//cout << "Line 283" << endl;
//cout << "Line 286, current node = " << current_node << " and rode: " << receiver_node << endl;
current_node = receiver_node;
//cout << "Line 288, current node = " << current_node << " and rode: " << receiver_node << endl;
current_row = FlowInfo.RowIndex[current_node];
current_col = FlowInfo.ColIndex[current_node];
receiver_node = FlowInfo.ReceiverVector[current_node];
// first we need logic for if this is a baselevel node
if (current_node == receiver_node)
{
//cout << "source: " << src << " and BASELEVEL, node: " << current_node << " rnode: " << receiver_node << endl;
// check to see if it has a junction index number.
if(JunctionIndexArray[current_row][current_col] == NoDataValue)
{
// it doens't have a JunctionIndexNumber. This is a new
// junction
this_junction++;
// this junction has the this_junction index. Set the JunctionIndexArray
JunctionIndexArray[current_row][current_col] = this_junction;
// the receiver node of the previous junction is the new junction
ReceiverVector.push_back( JunctionIndexArray[current_row][current_col] );
//push back the junction vector
JunctionVector.push_back(current_node);
// because this is a baselevel node, the Receiver of this junction
// is iteself
ReceiverVector.push_back( JunctionIndexArray[current_row][current_col] );
// the stream order of this node is also determined by the node
StreamOrderVector.push_back( StreamOrderArray[current_row][current_col] );
// finally, this is the first time we have visted this baselevel node.
// So it gets added to the baselevel vector
BaseLevelJunctions.push_back(this_junction);
}
else // this junction does have an index number, no new junction is created
{
// the receiver node of the previous junction is the new junction
ReceiverVector.push_back( JunctionIndexArray[current_row][current_col] );
}
junction_switch = 2;
baselevel_switch = 1; // this is a baselevel. It will exit the
// loop and move to the next source
}
else // this is not a baselevel node
{
//cout << "LINE 330, not baselevel; src: " << src << " and node: " << current_node << " rnde: " << receiver_node << endl;
// the node in the junction array is zero if it is not a
// junction, 1 if it is an unvisited junction, and 2 or more if it
// is a visited junction
if(JunctionArray[current_row][current_col] != NoDataValue)
{
//cout << "LINE 338, found a junction at node: " << current_node
// << " JArray: " << JunctionArray[current_row][current_col] << endl;
junction_switch = JunctionArray[current_row][current_col];
JunctionArray[current_row][current_col] ++; // increment the junction array
// it will be greater than 1 if
// the junction has been visited
// if this junction has been visited, it will have a junction number
// include the receiver vector
if (JunctionIndexArray[current_row][current_col] != NoDataValue )
{
ReceiverVector.push_back( JunctionIndexArray[current_row][current_col] );
// the loop will not continue; it will move onto the next
// source since it has visited an already visited junction
}
else // the junction has not been visited
{
// this is a new junction. Increment the 'last junction' int
this_junction++;
// this junction has the this_junction index. Set the JunctionIndexArray
JunctionIndexArray[current_row][current_col] = this_junction;
// the receiver node of the previous junction is the new junction
ReceiverVector.push_back( JunctionIndexArray[current_row][current_col] );
//push back the junction vector; this is a new junction
JunctionVector.push_back(current_node);
// get the stream order of this new junction
StreamOrderVector.push_back( StreamOrderArray[current_row][current_col] );
}
} // end logic for is this a junction
} // end logic for not a baselevel node
} // end baselevel logic
} // end sources loop
//cout << "ChanNet; LINE 368; sz ReceiverVec: " << ReceiverVector.size() << " sz JuncVec: " << JunctionVector.size()
// << " sz SOVec: " << StreamOrderVector.size() << endl;
//for(int i = 0; i< int(BaseLevelJunctions.size()); i++)
//{
// cout << "LINE 382 bl node["<<i<<"]: " << BaseLevelJunctions[i] << endl;
//}
//cout << "LINE 385: links data " << endl;
//for (int i = 0; i< int(StreamOrderVector.size()); i++)
//{
// cout << "Junc: " << i << " node: " << JunctionVector[i]
// << " receiv: " << ReceiverVector[i] << " Order: " << StreamOrderVector[i] << endl;
//}
// get the number of junctions
NJunctions = int(JunctionVector.size());
// now we implement the fastscape algorithm
// set the sizes of the member vectors
vector<int> ndn_vec(NJunctions,0);
vector<int> ndn_nodata_vec(NJunctions,NoDataValue);
vector<int> ndn_plusone_vec(NJunctions+1,0);
vector<int> w_vector(NJunctions,0);
NDonorsVector = ndn_vec;
DonorStackVector = ndn_vec;
DeltaVector = ndn_plusone_vec;
SVector = ndn_nodata_vec;
BLBasinVector = ndn_nodata_vec;
// first create the number of donors vector
// from braun and willett eq. 5
for(int i = 0; i<NJunctions; i++)
{
NDonorsVector[ ReceiverVector[i] ]++;
}
// now create the delta vector
// this starts on the last element and works its way backwards
// from Braun and Willett eq 7 and 8
DeltaVector[NJunctions] = NJunctions;
for(int i = NJunctions; i>0; i--)
{
DeltaVector[i-1] = DeltaVector[i] - NDonorsVector[i-1];
}
// now the DonorStack and the r vectors. These come from Braun and Willett
// equation 9.
// Note that in the manscript I have there is a typo in eqaution 9
// (Jean Braun's code is correct)
// it should be w_{r_i} = w_{r_i}+1
int r_index;
int w_index;
int delta_index;
for (int i = 0; i<NJunctions; i++)
{
r_index = ReceiverVector[i];
delta_index = DeltaVector[ r_index ];
w_index = w_vector[ r_index ];
DonorStackVector[ delta_index+w_index ] = i;
w_vector[r_index] += 1;
}
// now go through the base level node list, building the drainage tree for each of these nodes as one goes along
int n_base_level_nodes;
n_base_level_nodes = BaseLevelJunctions.size();
int k;
int j_index;
int begin_delta_index, end_delta_index;
int l_index;
j_index = 0;
for (int i = 0; i<n_base_level_nodes; i++)
{
k = BaseLevelJunctions[i]; // set k to the base level node
// This doesn't seem to be in Braun and Willet but to get the ordering correct you
// need to make sure that the base level node appears first in the donorstack
// of nodes contributing to the baselevel node.
// For example, if base level node is 4, with 4 donors
// and the donor stack has 3 4 8 9
// the code has to put the 4 first.
if (DonorStackVector[ DeltaVector[k] ] != k)
{
int this_index = DonorStackVector[ DeltaVector[k] ];
int bs_node = k;
for(int ds_node = 1; ds_node < NDonorsVector[k]; ds_node++)
{
if( DonorStackVector[ DeltaVector[k] + ds_node ] == bs_node )
{
DonorStackVector[ DeltaVector[k] ] = k;
DonorStackVector[ DeltaVector[k] + ds_node ] = this_index;
}
}
}
// now run recursive algorithm
begin_delta_index = DeltaVector[k];
end_delta_index = DeltaVector[k+1];
for (int delta_index = begin_delta_index; delta_index<end_delta_index; delta_index++)
{
l_index = DonorStackVector[delta_index];
add_to_stack(l_index, j_index, k);
}
}
// now run the indexing and accumulation routine
vector<int> vectorized_contributing_pixels(NJunctions,1);
SVectorIndex = vectorized_contributing_pixels;
int receiver_junction;
int donor_junction;
// loop through the s vector, adding pixels to receiver nodes
for(int junc = NJunctions-1; junc>=0; junc--)
{
donor_junction = SVector[junc];
receiver_junction = ReceiverVector[ donor_junction ];
// every node is visited once and only once so we can map the
// unique positions of the nodes to the SVector
SVectorIndex[donor_junction] = junc;
// add the upslope area (note no action is taken
// for base level nodes since they donate to themselves and
// we must avoid float counting
if (donor_junction != receiver_junction)
{
vectorized_contributing_pixels[ receiver_junction ] += vectorized_contributing_pixels[ donor_junction ];
}
}
//cout << "LINE 525 did area calcs " << endl;
NContributingJunctions = vectorized_contributing_pixels;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// This function gets the UTM zone
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDJunctionNetwork::get_UTM_information(int& UTM_zone, bool& is_North)
{
// set up strings and iterators
map<string,string>::iterator iter;
//check to see if there is already a map info string
string mi_key = "ENVI_map_info";
iter = GeoReferencingStrings.find(mi_key);
if (iter != GeoReferencingStrings.end() )
{
string info_str = GeoReferencingStrings[mi_key] ;
// now parse the string
vector<string> mapinfo_strings;
istringstream iss(info_str);
while( iss.good() )
{
string substr;
getline( iss, substr, ',' );
mapinfo_strings.push_back( substr );
}
UTM_zone = atoi(mapinfo_strings[7].c_str());
//cout << "Line 1041, UTM zone: " << UTM_zone << endl;
//cout << "LINE 1042 LSDRaster, N or S: " << mapinfo_strings[7] << endl;
// find if the zone is in the north
string n_str = "n";
string N_str = "N";
is_North = false;
size_t found = mapinfo_strings[8].find(N_str);
if (found!=std::string::npos)
{
is_North = true;
}
found = mapinfo_strings[8].find(n_str);
if (found!=std::string::npos)
{
is_North = true;
}
//cout << "is_North is: " << is_North << endl;
}
else
{
UTM_zone = NoDataValue;
is_North = false;
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// This function returns the x and y location of a row and column
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDJunctionNetwork::get_x_and_y_locations(int row, int col, double& x_loc, double& y_loc)
{
x_loc = XMinimum + float(col)*DataResolution + 0.5*DataResolution;
// Slightly different logic for y because the DEM starts from the top corner
y_loc = YMinimum + float(NRows-row)*DataResolution - 0.5*DataResolution;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// This function returns the x and y location of a row and column
// Same as above but with floats
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDJunctionNetwork::get_x_and_y_locations(int row, int col, float& x_loc, float& y_loc)
{
x_loc = XMinimum + float(col)*DataResolution + 0.5*DataResolution;
// Slightly different logic for y because the DEM starts from the top corner
y_loc = YMinimum + float(NRows-row)*DataResolution - 0.5*DataResolution;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// Function to convert a node position with a row and column to a lat
// and long coordinate
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDJunctionNetwork::get_lat_and_long_locations(int row, int col, double& lat,
double& longitude, LSDCoordinateConverterLLandUTM Converter)
{
// get the x and y locations of the node
double x_loc,y_loc;
get_x_and_y_locations(row, col, x_loc, y_loc);
// get the UTM zone of the node
int UTM_zone;
bool is_North;
get_UTM_information(UTM_zone, is_North);
//cout << endl << endl << "Line 1034, UTM zone is: " << UTM_zone << endl;
if(UTM_zone == NoDataValue)
{
lat = NoDataValue;
longitude = NoDataValue;
}
else
{
// set the default ellipsoid to WGS84
int eId = 22;
double xld = double(x_loc);
double yld = double(y_loc);
// use the converter to convert to lat and long
double Lat,Long;
Converter.UTMtoLL(eId, yld, xld, UTM_zone, is_North, Lat, Long);
lat = Lat;
longitude = Long;
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDJunctionNetwork::get_x_and_y_from_latlong(vector<float> latitude, vector<float> longitude,
vector<float>& UTME,vector<float>& UTMN)
{
// initilise the converter
LSDCoordinateConverterLLandUTM Converter;
int N_samples = int(latitude.size());
// set up some temporary vectors
vector<float> this_UTMN(N_samples,0);
vector<float> this_UTME(N_samples,0);
double this_Northing;
double this_Easting;
int UTM_zone;
bool is_North;
get_UTM_information(UTM_zone, is_North);
// loop throught the samples collecting UTM information
int eId = 22; // defines the ellipsiod. This is WGS
for(int i = 0; i<N_samples; i++)
{
cout << "Converting point " << i << " to UTM." << endl;
Converter.LLtoUTM_ForceZone(eId, latitude[i], longitude[i],
this_Northing, this_Easting, UTM_zone);
this_UTMN[i] = this_Northing;
this_UTME[i] = this_Easting;
cout << "Easting: " << this_Easting << " and northing: " << this_Northing << endl;
}
UTME = this_UTME;
UTMN = this_UTMN;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// recursive add_to_stack routine, from Braun and Willett eq. 12 and 13
//
// SMM 01/09/2012
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDJunctionNetwork::add_to_stack(int lm_index, int& j_index, int bl_node)
{
//cout << "j_index: " << j_index << " and s_vec: " << lm_index << endl;
SVector[j_index] = lm_index;
BLBasinVector[j_index] = bl_node;
j_index++;
int begin_m,end_m;
int l_index;
// if donating to itself, need escape hatch
if ( lm_index == bl_node)
{
begin_m = 0;
end_m = 0;
}
else
{
begin_m = DeltaVector[lm_index];
end_m = DeltaVector[ lm_index+1];
}
//cout << "lm_index: " << lm_index << " begin_m: " << begin_m << " end m: " << end_m << endl;
for( int m_index = begin_m; m_index<end_m; m_index++)
{
//cout << "recursion, begin_m: " << begin_m << " and end_m: " << end_m << endl;
l_index = DonorStackVector[m_index];
add_to_stack(l_index, j_index, bl_node);
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// This function returns a integer vector containing all the junction numbers upslope
// of of the junction with number junction_number_outlet
//
// SMM 01/09/2012
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
vector<int> LSDJunctionNetwork::get_upslope_junctions(int junction_number_outlet)
{
vector<int> us_junctions;
if(junction_number_outlet < 0 || junction_number_outlet > NJunctions-1)
{
cout << "Tried LSDJunctionNetwork::get_upslope_junctions but the"
<< " junction number does not exist" << endl;
exit(0);
}
int start_SVector_junction = SVectorIndex[junction_number_outlet];
int end_SVector_junction = start_SVector_junction+NContributingJunctions[junction_number_outlet];
for(int junction = start_SVector_junction; junction < end_SVector_junction; junction++)
{
us_junctions.push_back(SVector[junction]);
}
return us_junctions;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// This function returns a integer vector containing all the junction numbers upslope
// of of the junction with number junction_number_outlet of a specified order
//
// FJC 13/08/19
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
vector<int> LSDJunctionNetwork::get_upslope_junctions_by_order(int junction_number_outlet, int stream_order)
{
vector<int> us_junctions;
if(junction_number_outlet < 0 || junction_number_outlet > NJunctions-1)
{
cout << "Tried LSDJunctionNetwork::get_upslope_junctions but the"
<< " junction number does not exist" << endl;
exit(0);
}
int start_SVector_junction = SVectorIndex[junction_number_outlet];
int end_SVector_junction = start_SVector_junction+NContributingJunctions[junction_number_outlet];
for(int junction = start_SVector_junction; junction < end_SVector_junction; junction++)
{
int this_jn = SVector[junction];
int this_SO = get_StreamOrder_of_Junction(this_jn);
if (this_SO == stream_order)
{
us_junctions.push_back(SVector[junction]);
}
}
return us_junctions;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This function takes a junction and finds all the source junction upstream of the
// junction.
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
vector<int> LSDJunctionNetwork::get_all_source_junctions_of_an_outlet_junction(int junction_number_outlet)
{
vector<int> us_junctions = get_upslope_junctions(junction_number_outlet);
vector<int> source_junctions;
int n_upslope_junctions = int(us_junctions.size());
for (int j = 0; j<n_upslope_junctions; j++)
{
// if the junction has no donors, it is a source
if (NDonorsVector[ us_junctions[j] ] == 0)
{
source_junctions.push_back(us_junctions[j]);
}
}
return source_junctions;
}