-
Notifications
You must be signed in to change notification settings - Fork 0
/
LSDRasterMaker.cpp
1697 lines (1453 loc) · 46.6 KB
/
LSDRasterMaker.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
///=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
///
/// LSDRasterMaker.cpp
/// header for the RasterMaker object
/// The raster maker is a series of simple functions to make some rasters
/// with different properties.
/// The initial use is mainly to make rasters for use in the raster model
/// for uplift and K
///=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
///
/// This object is written by
/// @author Simon M. Mudd, University of Edinburgh
///
///=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
///
/// Version 0.0.1 01/09/2017
///=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <ctime>
#include <cmath>
#include "TNT/tnt.h"
#include "LSDRaster.hpp"
#include "LSDIndexRaster.hpp"
#include "LSDRasterMaker.hpp"
#include "LSDStatsTools.hpp"
#include "LSDSpatialCSVReader.hpp"
using namespace std;
using namespace TNT;
#ifndef LSDRasterMaker_CPP
#define LSDRasterMaker_CPP
void LSDRasterMaker::create()
{
NRows = 100;
NCols = 100;
DataResolution = 10;
NoDataValue = -9999;
XMinimum = 0;
YMinimum = 0;
RasterData = Array2D <float> (NRows, NCols, 0.0);
int zone = 1;
string NorS = "N";
impose_georeferencing_UTM(zone, NorS);
}
// this creates a raster using an infile
void LSDRasterMaker::create(string filename, string extension)
{
read_raster(filename,extension);
}
void LSDRasterMaker::create(int NRows, int NCols)
{
this->NRows = NRows;
this->NCols = NCols;
this->DataResolution = 10;
this->NoDataValue = -9999;
XMinimum = 0;
YMinimum = 0;
RasterData = Array2D <float> (NRows, NCols, 0.0);
int zone = 1;
string NorS = "N";
impose_georeferencing_UTM(zone, NorS);
}
// this creates a LSDRasterModel raster from another LSDRaster
void LSDRasterMaker::create(LSDRaster& An_LSDRaster)
{
cout << "Lets get some info!" << endl;
NRows = An_LSDRaster.get_NRows();
NCols = An_LSDRaster.get_NCols();
XMinimum = An_LSDRaster.get_XMinimum();
YMinimum = An_LSDRaster.get_YMinimum();
DataResolution = An_LSDRaster.get_DataResolution();
NoDataValue = An_LSDRaster.get_NoDataValue();
GeoReferencingStrings = An_LSDRaster.get_GeoReferencingStrings();
RasterData = An_LSDRaster.get_RasterData();
}
// This returns the data in the raster model as a raster
LSDRaster LSDRasterMaker::return_as_raster()
{
LSDRaster NewRaster(NRows, NCols, XMinimum, YMinimum,
DataResolution, NoDataValue, RasterData,
GeoReferencingStrings);
return NewRaster;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This resizes and resets the model
// This overloaded version also resets the data resolution
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDRasterMaker::resize_and_reset( int new_rows, int new_cols, float new_resolution, float new_value )
{
// set up some empty arrays
Array2D<float> empty_array_sized(new_rows,new_cols,new_value);
// reset the size of the RasterData
RasterData = empty_array_sized.copy();
// reset the rows and columns
NRows = new_rows;
NCols = new_cols;
DataResolution = new_resolution;
int zone = 1;
string NorS = "N";
impose_georeferencing_UTM(zone, NorS);
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// Gets the row and column of a point
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDRasterMaker::get_row_and_col_of_a_point(float X_coordinate,float Y_coordinate,int& row, int& col)
{
int this_row = NoDataValue;
int this_col = NoDataValue;
// Shift origin to that of dataset
float X_coordinate_shifted_origin = X_coordinate - XMinimum;
float Y_coordinate_shifted_origin = Y_coordinate - YMinimum;
// Get row and column of point
int col_point = int(X_coordinate_shifted_origin/DataResolution);
int row_point = (NRows - 1) - int(ceil(Y_coordinate_shifted_origin/DataResolution)-0.5);
//cout << "Getting row and col, " << row_point << " " << col_point << endl;
if(col_point >= 0 && col_point <= NCols-1)
{
this_col = col_point;
}
if(row_point >= 0 && row_point <= NRows -1)
{
this_row = row_point;
}
row = this_row;
col = this_col;
}
void LSDRasterMaker::get_row_and_col_of_a_point(double X_coordinate,double Y_coordinate,int& row, int& col)
{
int this_row = NoDataValue;
int this_col = NoDataValue;
// Shift origin to that of dataset
double X_coordinate_shifted_origin = X_coordinate - XMinimum;
double Y_coordinate_shifted_origin = Y_coordinate - YMinimum;
// Get row and column of point
int col_point = int(X_coordinate_shifted_origin/DataResolution);
int row_point = (NRows - 1) - int(ceil(Y_coordinate_shifted_origin/DataResolution)-0.5);
//cout << "Getting row and col, " << row_point << " " << col_point << endl;
if(col_point >= 0 && col_point <= NCols-1)
{
this_col = col_point;
}
if(row_point >= 0 && row_point <= NRows -1)
{
this_row = row_point;
}
row = this_row;
col = this_col;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Gets the minimum and maximum values in the raster
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
vector<float> LSDRasterMaker::minimum_and_maximum_value()
{
// The vector min max will contain minimum and maximum values. It is initiated
// with a very high minimum and a very low maximum to guarantee that one will
// always get sensible if the raster has non-nodata values.
vector<float> min_max;
min_max.push_back(1e12);
min_max.push_back(-9998.0);
for(int row = 0; row < NRows; row++)
{
for(int col = 0; col < NCols; col++)
{
if(RasterData[row][col] != NoDataValue)
{
if(RasterData[row][col] > min_max[1])
{
min_max[1]= RasterData[row][col];
}
if(RasterData[row][col] < min_max[0])
{
min_max[0]= RasterData[row][col];
}
}
}
}
return min_max;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This function resets all non nodata nodes to a constant value
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDRasterMaker::set_to_constant_value(float new_value)
{
// now loop through the matrix rescaling the values.
for (int row = 0; row< NRows; row++)
{
for(int col = 0; col < NCols; col++)
{
if(RasterData[row][col] != NoDataValue)
{
RasterData[row][col] = new_value;
}
}
}
}
// Add a float to all pixels in the raster
void LSDRasterMaker::add_value(float value_to_add)
{
// now loop through the matrix rescaling the values.
for (int row = 0; row< NRows; row++)
{
for(int col = 0; col < NCols; col++)
{
if(RasterData[row][col] != NoDataValue)
{
RasterData[row][col] = RasterData[row][col]+value_to_add;
}
}
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This function add strips of a given value.
// This happily overwrites NoData
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDRasterMaker::add_strip(int start_row_or_col, int end_row_or_col, bool horizontal, float value)
{
if (start_row_or_col < 0)
{
start_row_or_col = 0;
}
if (horizontal)
{
if(start_row_or_col >= NRows)
{
start_row_or_col = NRows-1;
}
if(end_row_or_col > NRows)
{
end_row_or_col = NRows-1;
}
}
else
{
if(start_row_or_col >= NCols)
{
start_row_or_col = NCols-1;
}
if(end_row_or_col > NCols)
{
end_row_or_col = NCols-1;
}
}
if (horizontal)
{
for (int row = start_row_or_col; row <= end_row_or_col; row++)
{
for (int col = 0; col < NCols; col++)
{
RasterData[row][col] = value;
}
}
}
else
{
for (int row = 0; row < NRows; row++)
{
for (int col = start_row_or_col; end_row_or_col<= NCols; col++)
{
RasterData[row][col] = value;
}
}
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This function takes the existing raster data and then linearly scales it
// to new minimum and maximum values.
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDRasterMaker::scale_to_new_minimum_and_maximum_value(float new_minimum, float new_maximum)
{
// first we get the existing minimum and maximum
vector<float> min_max = minimum_and_maximum_value();
float scaling_fraction;
float original_range = min_max[1] - min_max[0];
float new_range = new_maximum-new_minimum;
// now loop through the matrix rescaling the values.
for (int row = 0; row< NRows; row++)
{
for(int col = 0; col < NCols; col++)
{
if(RasterData[row][col] != NoDataValue)
{
// first find where the value is between min and max
if(original_range == 0)
{
scaling_fraction = 0;
}
else
{
scaling_fraction = (RasterData[row][col] - min_max[0])/original_range;
}
RasterData[row][col] = (scaling_fraction*new_range + new_minimum);
}
}
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
////------------------------------------------------------------------------------
//// impose_channels: this imposes channels onto the landscape
//// You need to print a channel to csv and then load the data
////------------------------------------------------------------------------------
void LSDRasterMaker::impose_channels(LSDSpatialCSVReader& source_points_data, string column_name)
{
// string column_name = "elevation(m)";
Array2D<float> zeta=RasterData.copy();
// Step one, create donor "stack" etc. via FlowInfo
LSDRaster temp(NRows, NCols, XMinimum, YMinimum, DataResolution, NoDataValue, zeta, GeoReferencingStrings);
// Get the local coordinate as well as the elevations
vector<float> UTME, UTMN;
source_points_data.get_x_and_y_from_latlong(UTME,UTMN);
vector<float> elev = source_points_data.data_column_to_float(column_name);
// make the map
cout << "I am making an elevation map. This will not work if points in the raster lie outside of the csv channel points." << endl;
int row,col;
for(int i = 0; i< int(elev.size()); i++)
{
source_points_data.get_row_and_col_of_a_point(UTME[i],UTMN[i],row, col);
zeta[row][col] = elev[i];
}
this->RasterData = zeta.copy();
RasterData = zeta.copy();
}
////------------------------------------------------------------------------------
//// This takes a raster an increases the elevation of nodata pixels at the edge nodes
//// so that the outside pixels always drain inwards
////------------------------------------------------------------------------------
void LSDRasterMaker::buffer_basin_to_single_outlet(LSDSpatialCSVReader& source_points_data, float slope)
{
string column_name = "elevation(m)";
float elev_diff = 10; //DataResolution*sqrt(2)*slope;
vector<int> min_elev_nd_rows;
vector<int> min_elev_nd_cols;
Array2D<float> zeta=RasterData.copy();
Array2D<float> old_zeta = RasterData.copy();
// Get the local coordinate as well as the elevations
vector<float> UTME, UTMN;
source_points_data.get_x_and_y_from_latlong(UTME,UTMN);
int min_row,min_col;
int rp1,rm1,cp1,cm1;
vector<float> elev;
if ( UTME.size() == 1)
{
cout << "Only one element!" << endl;
elev.push_back(0);
}
else
{
elev = source_points_data.data_column_to_float(column_name);
cout << "There are " << elev.size() << " data points" << endl;
}
// find the minimum elevation
int n_elev = int(elev.size());
float min_elev = 10000000000;
int node_of_min_elev = 0;
cout << "n_elev: " << n_elev << endl;
for(int i = 0; i< n_elev; i++)
{
cout << "elev: " << elev[i] << endl;
if(elev[i] < min_elev)
{
min_elev = elev[i];
node_of_min_elev = i;
}
}
get_row_and_col_of_a_point(UTME[node_of_min_elev],UTMN[node_of_min_elev],min_row, min_col);
cout << "The minimum elevation of the source point is: " << min_elev << " at node: " << node_of_min_elev << endl;
cout << "minimum elev in source points is at " << UTME[node_of_min_elev] << " , " << UTMN[node_of_min_elev] << endl;
// now logic for finding the nodata around the minimum elevation
rp1 = min_row+1;
if(rp1 == NRows)
{
rp1 = min_row;
}
rm1 = min_row-1;
if (rm1 == -1)
{
rm1 = 0;
}
cp1 = min_col+1;
if (cp1 == NCols)
{
cp1 = min_col;
}
cm1 = min_col-1;
if (cm1 == -1)
{
cm1 = 0;
}
// now search all adjacent nodes
if ( old_zeta[rp1][cp1] == NoDataValue)
{
cout << "Buffering, found ndv" << endl;
min_elev_nd_rows.push_back(rp1);
min_elev_nd_cols.push_back(cp1);
}
if ( old_zeta[rp1][min_col] == NoDataValue)
{
cout << "Buffering, found ndv" << endl;
min_elev_nd_rows.push_back(rp1);
min_elev_nd_cols.push_back(min_col);
}
if ( old_zeta[rp1][cm1] == NoDataValue)
{
cout << "Buffering, found ndv" << endl;
min_elev_nd_rows.push_back(rp1);
min_elev_nd_cols.push_back(cm1);
}
if ( old_zeta[min_row][cp1] == NoDataValue)
{
cout << "Buffering, found ndv" << endl;
min_elev_nd_rows.push_back(min_row);
min_elev_nd_cols.push_back(cp1);
}
if ( old_zeta[min_row][cm1] == NoDataValue)
{
cout << "Buffering, found ndv" << endl;
min_elev_nd_rows.push_back(min_row);
min_elev_nd_cols.push_back(cm1);
}
if ( old_zeta[rm1][cp1] == NoDataValue)
{
cout << "Buffering, found ndv" << endl;
min_elev_nd_rows.push_back(rm1);
min_elev_nd_cols.push_back(cm1);
}
if ( old_zeta[rm1][min_col] == NoDataValue)
{
cout << "Buffering, found ndv" << endl;
min_elev_nd_rows.push_back(rm1);
min_elev_nd_cols.push_back(min_col);
}
if ( old_zeta[rm1][cm1] == NoDataValue)
{
cout << "Buffering, found ndv" << endl;
min_elev_nd_rows.push_back(rm1);
min_elev_nd_cols.push_back(cm1);
}
// Now crinkle up the side
for(int row = 0; row< NRows; row++)
{
for(int col = 0; col<NCols; col++)
{
if (row == min_row && col == min_col)
{
cout << "Found the minimum elevation" << endl;
}
else
{
if (old_zeta[row][col] != NoDataValue)
{
// now search neighbouring nodes for nodata
rp1 = row+1;
if(rp1 == NRows)
{
rp1 = row;
}
rm1 = row-1;
if (rm1 == -1)
{
rm1 = 0;
}
cp1 = col+1;
if (cp1 == NCols)
{
cp1 = col;
}
cm1 = col-1;
if (cm1 == -1)
{
cm1 = 0;
}
// now search all adjacent nodes
if ( old_zeta[rp1][cp1] == NoDataValue)
{
zeta[rp1][cp1] = old_zeta[row][col]+elev_diff;
}
if ( old_zeta[rp1][col] == NoDataValue)
{
zeta[rp1][col] = old_zeta[row][col]+elev_diff;
}
if ( old_zeta[rp1][cm1] == NoDataValue)
{
zeta[rp1][cm1] = old_zeta[row][col]+elev_diff;
}
if ( old_zeta[row][cp1] == NoDataValue)
{
zeta[row][cp1] = old_zeta[row][col]+elev_diff;
}
if ( old_zeta[row][cm1] == NoDataValue)
{
zeta[row][cm1] = old_zeta[row][col]+elev_diff;
}
if ( old_zeta[rm1][cp1] == NoDataValue)
{
zeta[rm1][cp1] = old_zeta[row][col]+elev_diff;
}
if ( old_zeta[rm1][col] == NoDataValue)
{
zeta[rm1][col] = old_zeta[row][col]+elev_diff;
}
if ( old_zeta[rm1][cm1] == NoDataValue)
{
zeta[rm1][cm1] = old_zeta[row][col]+elev_diff;
}
}
}
}
}
// now return the data around the minimum value to nodata
for (int i = 0; i< int(min_elev_nd_rows.size()); i++)
{
//cout << "yoyoma" << endl;
zeta[ min_elev_nd_rows[i] ][ min_elev_nd_cols[i] ] = NoDataValue;
}
this->RasterData = zeta.copy();
RasterData = zeta.copy();
}
void LSDRasterMaker::buffer_basin_to_single_outlet(float slope)
{
Array2D<float> zeta=RasterData.copy();
vector<int> edge_rows;
vector<int> edge_cols;
float elev_diff = DataResolution*sqrt(2)*slope;
// we look for any edge nodes. We don't look along the edge of the DEM
// any node that is not itself nodata but has an edge pixel as nodata
// gets added as an edge node
int rp1,rm1,cp1,cm1;
bool is_edge_node;
int min_edge_row = 0;
int min_edge_col = 0;
float min_edge_elev = 99999999999999;
for(int row = 1; row < NRows-1; row++)
{
for(int col = 1; col < NCols-1; col++)
{
if (zeta[row][col] != NoDataValue)
{
is_edge_node = false;
rp1 = row+1;
rm1 = row-1;
cp1 = col+1;
cm1 = col-1;
// now search all adjacent nodes
if ( zeta[rp1][cp1] == NoDataValue)
{
is_edge_node = true;
}
if ( zeta[rp1][col] == NoDataValue)
{
is_edge_node = true;
}
if ( zeta[rp1][cm1] == NoDataValue)
{
is_edge_node = true;
}
if ( zeta[row][cp1] == NoDataValue)
{
is_edge_node = true;
}
if ( zeta[row][cm1] == NoDataValue)
{
is_edge_node = true;
}
if ( zeta[rm1][cp1] == NoDataValue)
{
is_edge_node = true;
}
if ( zeta[rm1][col] == NoDataValue)
{
is_edge_node = true;
}
if ( zeta[rm1][cm1] == NoDataValue)
{
is_edge_node = true;
}
if (is_edge_node)
{
edge_rows.push_back(row);
edge_cols.push_back(col);
if (min_edge_elev > zeta[row][col])
{
min_edge_elev = zeta[row][col];
min_edge_row = row;
min_edge_col = col;
}
}
}
}
}
// Okay, now look through the edge rows and columns, buffering up the nodes.
int n_edge_nodes = int(edge_rows.size());
int row,col;
for(int i = 0; i< n_edge_nodes; i++)
{
row = edge_rows[i];
col = edge_cols[i];
rp1 = row+1;
rm1 = row-1;
cp1 = col+1;
cm1 = col-1;
if (row == min_edge_row && col == min_edge_col)
{
cout << "Found the minimum elevation edge node." << endl;
}
else
{
// now search all adjacent nodes
if ( zeta[rp1][cp1] == NoDataValue)
{
zeta[rp1][cp1] = zeta[row][col]+elev_diff;
}
if ( zeta[rp1][col] == NoDataValue)
{
zeta[rp1][col] = zeta[row][col]+elev_diff;
}
if ( zeta[rp1][cm1] == NoDataValue)
{
zeta[rp1][cm1] = zeta[row][col]+elev_diff;
}
if ( zeta[row][cp1] == NoDataValue)
{
zeta[row][cp1] = zeta[row][col]+elev_diff;
}
if ( zeta[row][cm1] == NoDataValue)
{
zeta[row][cm1] = zeta[row][col]+elev_diff;
}
if ( zeta[rm1][cp1] == NoDataValue)
{
zeta[rm1][cp1] = zeta[row][col]+elev_diff;
}
if ( zeta[rm1][col] == NoDataValue)
{
zeta[rm1][col] = zeta[row][col]+elev_diff;
}
if ( zeta[rm1][cm1] == NoDataValue)
{
zeta[rm1][cm1] = zeta[row][col]+elev_diff;
}
}
}
this->RasterData = zeta.copy();
RasterData = zeta.copy();
}
////------------------------------------------------------------------------------
//// impose_channels: this imposes channels onto the landscape
//// You need to print a channel to csv and then load the data
////------------------------------------------------------------------------------
void LSDRasterMaker::impose_channels_with_buffer(LSDSpatialCSVReader& source_points_data, float slope, string column_name)
{
// string column_name = "elevation(m)";
float elev_diff = DataResolution*sqrt(2)*slope;
Array2D<float> zeta=RasterData.copy();
// Step one, create donor "stack" etc. via FlowInfo
LSDRaster temp(NRows, NCols, XMinimum, YMinimum, DataResolution, NoDataValue, zeta, GeoReferencingStrings);
// Get the local coordinate as well as the elevations
vector<float> UTME, UTMN;
source_points_data.get_x_and_y_from_latlong(UTME,UTMN);
vector<float> elev = source_points_data.data_column_to_float(column_name);
// find the minimum elevation
float min_elev = 10000000000;
int node_of_min_elev = 0;
for(int i = 0; i< int(elev.size()); i++)
{
if(elev[i] < min_elev)
{
min_elev = elev[i];
node_of_min_elev = i;
}
}
cout << "The minimum elevation is: " << min_elev << endl;
// make the map
cout << "I am making an elevation map. This will not work if points in the raster lie outside of the csv channel points." << endl;
int row,col;
int rp1,rm1,cp1,cm1;
for(int i = 0; i< int(elev.size()); i++)
{
source_points_data.get_row_and_col_of_a_point(UTME[i],UTMN[i],row, col);
zeta[row][col] = elev[i];
if (i != node_of_min_elev)
{
// now search neighbouring nodes for nodata
rp1 = row+1;
if(rp1 == NRows)
{
rp1 = row;
}
rm1 = row-1;
if (rm1 == -1)
{
rm1 = 0;
}
cp1 = col+1;
if (cp1 == NCols)
{
cp1 = col;
}
cm1 = col-1;
if (cm1 == -1)
{
cm1 = 0;
}
// now search all adjacent nodes
if ( zeta[rp1][cp1] == NoDataValue)
{
zeta[rp1][cp1] = elev[i]+elev_diff;
}
if ( zeta[rp1][col] == NoDataValue)
{
zeta[rp1][col] = elev[i]+elev_diff;
}
if ( zeta[rp1][cm1] == NoDataValue)
{
zeta[rp1][cm1] = elev[i]+elev_diff;
}
if ( zeta[row][cp1] == NoDataValue)
{
zeta[row][cp1] = elev[i]+elev_diff;
}
if ( zeta[row][cm1] == NoDataValue)
{
zeta[row][cm1] = elev[i]+elev_diff;
}
if ( zeta[rm1][cp1] == NoDataValue)
{
zeta[rm1][cp1] = elev[i]+elev_diff;
}
if ( zeta[rm1][col] == NoDataValue)
{
zeta[rm1][col] = elev[i]+elev_diff;
}
if ( zeta[rm1][cm1] == NoDataValue)
{
zeta[rm1][cm1] = elev[i]+elev_diff;
}
}
}
this->RasterData = zeta.copy();
RasterData = zeta.copy();
}
////------------------------------------------------------------------------------
//// impose_channels: this imposes channels onto the landscape using XY data
//// You need to print a channel to csv and then load the data
//// ELSG 23/02/2021
////------------------------------------------------------------------------------
void LSDRasterMaker::impose_channels_with_buffer_use_XY(LSDSpatialCSVReader& source_points_data, float slope, string column_name)
{
// string column_name = "elevation(m)";
string x_column_name = "X";
string y_column_name = "Y";
float elev_diff = DataResolution*sqrt(2)*slope;
Array2D<float> zeta=RasterData.copy();
// Step one, create donor "stack" etc. via FlowInfo
LSDRaster temp(NRows, NCols, XMinimum, YMinimum, DataResolution, NoDataValue, zeta, GeoReferencingStrings);
// Get the local coordinate as well as the elevations
vector<float> X = source_points_data.data_column_to_float(x_column_name);
vector<float> Y = source_points_data.data_column_to_float(y_column_name);
//source_points_data.get_x_and_y_from_latlong(X,Y);
vector<float> elev = source_points_data.data_column_to_float(column_name);
// find the minimum elevation
float min_elev = 10000000000;
int node_of_min_elev = 0;
for(int i = 0; i< int(elev.size()); i++)
{
if(elev[i] < min_elev)
{
min_elev = elev[i];
node_of_min_elev = i;
}
}
cout << "The minimum elevation is: " << min_elev << endl;
// make the map
cout << "I am making an elevation map. This will not work if points in the raster lie outside of the csv channel points." << endl;
int row,col;
int rp1,rm1,cp1,cm1;
for(int i = 0; i< int(elev.size()); i++)
{
cout << "At i " << i << ", X is: " << X[i] << ", and Y is: " << Y[i] << "; elevation is: " << elev[i] << endl;
source_points_data.get_row_and_col_of_a_point(X[i],Y[i],row, col);
cout << "Row is: " << row << ", and col is: " << col << endl;
zeta[row][col] = elev[i];
if (i != node_of_min_elev)
{
// now search neighbouring nodes for nodata
rp1 = row+1;
if(rp1 == NRows)
{
rp1 = row;
}
rm1 = row-1;
if (rm1 == -1)
{
rm1 = 0;
}
cp1 = col+1;
if (cp1 == NCols)
{
cp1 = col;
}
cm1 = col-1;
if (cm1 == -1)
{
cm1 = 0;
}
// now search all adjacent nodes
if ( zeta[rp1][cp1] == NoDataValue)
{
zeta[rp1][cp1] = elev[i]+elev_diff;
}
if ( zeta[rp1][col] == NoDataValue)
{
zeta[rp1][col] = elev[i]+elev_diff;
}
if ( zeta[rp1][cm1] == NoDataValue)
{
zeta[rp1][cm1] = elev[i]+elev_diff;
}
if ( zeta[row][cp1] == NoDataValue)
{
zeta[row][cp1] = elev[i]+elev_diff;
}
if ( zeta[row][cm1] == NoDataValue)
{
zeta[row][cm1] = elev[i]+elev_diff;
}
if ( zeta[rm1][cp1] == NoDataValue)
{
zeta[rm1][cp1] = elev[i]+elev_diff;
}
if ( zeta[rm1][col] == NoDataValue)
{
zeta[rm1][col] = elev[i]+elev_diff;
}
if ( zeta[rm1][cm1] == NoDataValue)
{
zeta[rm1][cm1] = elev[i]+elev_diff;
}
}
}
this->RasterData = zeta.copy();
RasterData = zeta.copy();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Cap elevations using an initial raster
//------------------------------------------------------------------------------
void LSDRasterMaker::cap_elevations(LSDRaster& InitialRaster)
{
cout << "Capping elevations. WARNING: no checking rasters are same dimension" << endl;
for(int row=0; row<NRows; ++row)
{
for(int col=0; col<NCols; ++col)
{
if(RasterData[row][col]!=NoDataValue)
{
if (RasterData[row][col] > InitialRaster.get_data_element(row,col))