-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLSDRasterMaker.cpp
More file actions
908 lines (769 loc) · 26.5 KB
/
LSDRasterMaker.cpp
File metadata and controls
908 lines (769 loc) · 26.5 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
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
///=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
///
/// 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"
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)
{
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 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;
}
}
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// 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);
}
}
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This smooths the raster by taking a weighted average of the given pixel
// and neighboring pixels.
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDRasterMaker::smooth(int boundary_type)
{
// at the moment the boundary type can only be 0 and this is a periodic
// boundary type at the E and W boundaries.
Array2D<float> new_data(NRows,NCols,NoDataValue);
float total_weighting;
float total_sum;
int rp1, rm1,cp1, cm1;
for(int row = 0; row<NRows; row++)
{
for(int col = 0; col<NCols; col++)
{
total_weighting = 0;
total_sum = 0;
rp1 = row+1;
rm1 = row-1;
cp1 = col+1;
cm1 = col-1;
// implement boundary conditions.
if(boundary_type == 0)
{
if (rp1 == NRows)
{
rp1 = rm1;
}
if (rm1 == -1)
{
rm1 = rp1;
}
if (cp1 == NCols)
{
cp1 = 0;
}
if(cm1 == -1)
{
cm1 = NCols-1;
}
}
else
{
if (rp1 == NRows)
{
rp1 = rm1;
}
if (rm1 == -1)
{
rm1 = rp1;
}
if (cp1 == NCols)
{
cp1 = 0;
}
if(cm1 == -1)
{
cm1 = NCols-1;
}
}
if( RasterData[row][col] != NoDataValue)
{
total_weighting += 2;
total_sum += 2*RasterData[row][col];
// now go through all the other directions.
if (RasterData[row][cp1] != NoDataValue)
{
total_weighting +=1;
total_sum += RasterData[row][cp1];
}
if (RasterData[row][cm1] != NoDataValue)
{
total_weighting +=1;
total_sum += RasterData[row][cm1];
}
if (RasterData[rp1][col] != NoDataValue)
{
total_weighting +=1;
total_sum += RasterData[rp1][col];
}
if (RasterData[rm1][col] != NoDataValue)
{
total_weighting +=1;
total_sum += RasterData[rm1][col];
}
}
// Now update the array
new_data[row][col] = total_sum/total_weighting;
}
}
RasterData = new_data.copy();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Some functions for making random values in the rasters
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDRasterMaker::random_values(float minimum_value, float maximum_value)
{
long seed = time(NULL);
for(int row = 0; row<NRows; row++)
{
for(int col = 0; col<NCols; col++)
{
if (RasterData[row][col] != NoDataValue)
{
RasterData[row][col] = ran3(&seed);
}
}
}
// Now scale to min and max
scale_to_new_minimum_and_maximum_value(minimum_value, minimum_value);
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Some functions for making random values in the rasters
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDRasterMaker::random_square_blobs(int minimum_blob_size, int maximum_blob_size, float minimum_value, float maximum_value, int n_blobs)
{
long seed = time(NULL);
// Lets make some blobs!!!
for (int blob_n = 0; blob_n < n_blobs; blob_n++)
{
// get the centrepoint of the blob
float row_frac = ran3(&seed);
float col_frac = ran3(&seed);
// get the row and column of the centre of the blob
float frow = row_frac*float(NRows);
int this_row = floor(frow);
if (this_row < 0)
{
this_row = 0;
}
if(this_row >= NRows)
{
this_row = NRows-1;
}
// get the row and column of the centre of the blob
float fcol = col_frac*float(NCols);
int this_col = floor(fcol);
if (this_col < 0)
{
this_col = 0;
}
if(this_col >= NCols)
{
this_col = NCols-1;
}
// Get the size of the blob. This will need to be odd
int size_range = maximum_blob_size-minimum_blob_size;
float this_size;
if(size_range == 0)
{
cout << "Check you prarameters, the size range is zero." << endl;
this_size = minimum_blob_size;
}
else
{
this_size = ran3(&seed)*float(size_range)+float(minimum_blob_size);
}
int size = int(this_size);
// get the starting rows and ending rows. Note that I am not being very careful about
// this being exactly the right dimension
int start_row = this_row - size/2;
int end_row = this_row + size/2;
int start_col = this_col - size/2;
int end_col = this_col + size/2;
// get the new value
float value_range = maximum_value-minimum_value;
float this_blob_value;
if(value_range == 0)
{
cout << "Check you prarameters, the value range is zero." << endl;
this_blob_value = minimum_value;
}
else
{
this_blob_value = ran3(&seed)*value_range+minimum_value;
}
//cout << "This blob is: " << blob_n << " with a K of: " << this_blob_value << endl;
//cout << "The size is: " << this_size << " or " << size <<endl;
// now update the values
for(int row = start_row; row<=end_row; row++)
{
for(int col = start_col; col<= end_col; col++)
{
if( row >= 0 && row<NRows && col >= 0 && col<NCols)
{
RasterData[row][col] = this_blob_value;
}
}
}
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Some functions for making random values in the rasters
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDRasterMaker::sine_waves(vector<float> x_coefficients, vector<float> y_coefficients)
{
int n_x_coeff = int(x_coefficients.size());
int n_y_coeff = int(y_coefficients.size());
float x_factor = M_PI/ float(NCols-1);
float y_factor = M_PI / float(NRows-1);
float this_x_value;
float this_y_value;
// so the wavelengths of the sin waves depend on the number
for (int row = 0; row<NRows; row++)
{
for(int col = 0; col<NCols; col++)
{
this_x_value = 0;
for(int xv = 0; xv<n_x_coeff; xv++)
{
this_x_value+=x_coefficients[xv]*sin(x_factor*(xv+1)*float(col));
}
this_y_value = 0;
for(int yv = 0; yv<n_y_coeff; yv++)
{
this_y_value+=y_coefficients[yv]*sin(y_factor*(yv+1)*float(row));
}
RasterData[row][col] = this_x_value+this_y_value;
}
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// THis clips to a smaller raster. The smaller raster does not need
// to have the same data resolution as the old raster
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
LSDRaster LSDRasterMaker::clip_to_smaller_raster(LSDRaster& smaller_raster)
{
// Get the MinX, MaxX, MinY, MaxY from the rasters
//float XMaximum = XMinimum + (NCols * DataResolution -1);
//float YMaximum = YMinimum + (NRows * DataResolution -1);
float SR_XMinimum = smaller_raster.get_XMinimum();
float SR_YMinimum = smaller_raster.get_YMinimum();
float SR_NRows = smaller_raster.get_NRows();
float SR_NCols = smaller_raster.get_NCols();
float SR_DataR = smaller_raster.get_DataResolution();
float SR_XMaximum = SR_XMinimum+(SR_NCols)*SR_DataR;
float SR_YMaximum = SR_YMinimum+(SR_NRows)*SR_DataR;
cout << "Small Xmin: " << SR_XMinimum << " YMin: " << SR_YMinimum << " Xmax: "
<< SR_XMaximum << " YMax: " << SR_YMaximum << endl;
cout << "This data resolution: " << DataResolution << " and smaller raster data resolution: " << SR_DataR << endl;
// find the col of old raster that has the same Xlocations as the XLL of smaller raster
// the 0.5*DataResolution is in case of rounding errors
int XLL_col = int((SR_XMinimum-XMinimum+0.5*DataResolution)/DataResolution);
int XUL_col = int((SR_XMaximum-XMinimum+0.5*DataResolution)/DataResolution);
// check these columns
if (XLL_col < 0)
{
XLL_col = 0;
}
if (XUL_col >= NCols)
{
XUL_col = NCols-1;
}
// find the row of old raster that has the same Xlocations as the XLL of smaller raster
// the 0.5*DataResolution is in case of rounding errors
// Slightly different logic for y because the DEM starts from the top corner
int YLL_row = NRows - int((SR_YMinimum-YMinimum+0.5*DataResolution)/DataResolution);
int YUL_row = NRows - int((SR_YMaximum-YMinimum+0.5*DataResolution)/DataResolution);
// check on the lower row:
cout << "Checking lower left row." << endl;
cout << "integer subtraction: " << int((SR_YMinimum-YMinimum+0.5*DataResolution)/DataResolution) << endl;
cout << "float subtraction: " << (SR_YMinimum-YMinimum+0.5*DataResolution)/DataResolution << endl;
// this catches a weird rounding error.
double int_sub = double(int((SR_YMinimum-YMinimum+0.5*DataResolution)/DataResolution));
double flt_sub = (SR_YMinimum-YMinimum+0.5*DataResolution)/DataResolution;
if ((flt_sub- int_sub) > 0.9975)
{
YUL_row = YUL_row+1;
}
// check these rows
if (YLL_row < 0)
{
YLL_row = 0;
}
if (YUL_row >= NRows)
{
YUL_row = NRows-1;
}
cout << "Small XLLCol: " << XLL_col << " XLR_col: " << XUL_col << " YLLrow: "
<< YLL_row << " YUL_row: " << YUL_row << endl;
// get the new number of rows and columns:
int New_NRows = YLL_row-YUL_row;
int New_NCols = XUL_col-XLL_col;
cout << "New NRows: " << New_NRows << " New_NCols: " << New_NCols << endl;
// now extract the data for the new raster
float NewR_XMinimum = XMinimum+float(XLL_col)*DataResolution;
float NewR_YMinimum = YMinimum + ((NRows - YLL_row ) * DataResolution);
Array2D<float> NewData(New_NRows,New_NCols, NoDataValue);
for(int row = 0; row< New_NRows; row++)
{
for(int col = 0; col<New_NCols; col++)
{
NewData[row][col] = RasterData[row+YUL_row][col+XLL_col];
}
}
LSDRaster TrimmedRaster(New_NRows, New_NCols, NewR_XMinimum,
NewR_YMinimum, DataResolution, NoDataValue, NewData,
GeoReferencingStrings);
cout << "Made a new raster" << endl;
TrimmedRaster.Update_GeoReferencingStrings();
return TrimmedRaster;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This gets the minimum and maximum values and returns them as a vector
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
vector<float> LSDRasterMaker::get_XY_MinMax()
{
vector<float> XYMaxMin(4,0);
XYMaxMin[0] = XMinimum;
XYMaxMin[1] = YMinimum;
XYMaxMin[2] = XMinimum+(NCols)*DataResolution;
XYMaxMin[3] = YMinimum+(NRows)*DataResolution;
return XYMaxMin;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This function gets the raster data into a vector
// FJC 06/11/15
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
vector<float> LSDRasterMaker::get_RasterData_vector()
{
vector<float> Raster_vector;
for (int row = 0; row < NRows; row++)
{
for (int col = 0; col < NCols; col++)
{
Raster_vector.push_back(RasterData[row][col]);
}
}
return Raster_vector;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This function writes the raster data (where != NoDataValue) to a text file
// FJC 30/09/16
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDRasterMaker::write_RasterData_to_text_file(string filename)
{
string string_filename;
string dot = ".";
string extension = "txt";
string_filename = filename+dot+extension;
cout << "The filename is " << string_filename << endl;
// open the data file
ofstream data_out(string_filename.c_str());
if( data_out.fail() )
{
cout << "\nFATAL ERROR: unable to write to " << string_filename << endl;
exit(EXIT_FAILURE);
}
data_out << "row col raster_data" << endl;
for (int row = 0; row < NRows; row++)
{
for (int col =0; col < NCols; col++)
{
if (RasterData[row][col] != NoDataValue)
{
data_out << row << " " << col << " " << RasterData[row][col] << endl;
}
}
}
data_out.close();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// THis clips to a smaller raster. The smaller raster does not need
// to have the same data resolution as the old raster
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
LSDRaster LSDRasterMaker::clip_to_smaller_raster(LSDIndexRaster& smaller_raster)
{
// Get the MinX, MaxX, MinY, MaxY from the rasters
//float XMaximum = XMinimum + (NCols * DataResolution -1);
//float YMaximum = YMinimum + (NRows * DataResolution -1);
float SR_XMinimum = smaller_raster.get_XMinimum();
float SR_YMinimum = smaller_raster.get_YMinimum();
float SR_NRows = smaller_raster.get_NRows();
float SR_NCols = smaller_raster.get_NCols();
float SR_DataR = smaller_raster.get_DataResolution();
float SR_XMaximum = SR_XMinimum+(SR_NCols)*SR_DataR;
float SR_YMaximum = SR_YMinimum+(SR_NRows)*SR_DataR;
cout << "Small Xmin: " << SR_XMinimum << " YMin: " << SR_YMinimum << " Xmax: "
<< SR_XMaximum << " YMax: " << SR_YMaximum << endl;
// find the col of old raster that has the same Xlocations as the XLL of smaller raster
// the 0.5*DataResolution is in case of rounding errors
int XLL_col = int((SR_XMinimum-XMinimum+0.5*DataResolution)/DataResolution);
int XUL_col = int((SR_XMaximum-XMinimum+0.5*DataResolution)/DataResolution);
// check these columns
if (XLL_col < 0)
{
XLL_col = 0;
}
if (XUL_col >= NCols)
{
XUL_col = NCols-1;
}
// find the row of old raster that has the same Xlocations as the XLL of smaller raster
// the 0.5*DataResolution is in case of rounding errors
// Slightly different logic for y because the DEM starts from the top corner
int YLL_row = NRows - int((SR_YMinimum-YMinimum+0.5*DataResolution)/DataResolution);
int YUL_row = NRows - int((SR_YMaximum-YMinimum+0.5*DataResolution)/DataResolution);
// check these rows
if (YLL_row < 0)
{
YLL_row = 0;
}
if (YUL_row >= NRows)
{
YUL_row = NRows-1;
}
cout << "Small XLLCol: " << XLL_col << " XLR_col: " << XUL_col << " XLLrow: "
<< YLL_row << " YUL_row: " << YUL_row << endl;
// get the new number of rows and columns:
int New_NRows = YLL_row-YUL_row;
int New_NCols = XUL_col-XLL_col;
cout << "New NRows: " << New_NRows << " New_NCols: " << New_NCols << endl;
// now extract the data for the new raster
float NewR_XMinimum = XMinimum+float(XLL_col)*DataResolution;
float NewR_YMinimum = YMinimum + ((NRows - YLL_row ) * DataResolution);
Array2D<float> NewData(New_NRows,New_NCols, NoDataValue);
//cout << "Writing the array" << endl;
for(int row = 0; row< New_NRows; row++)
{
for(int col = 0; col<New_NCols; col++)
{
NewData[row][col] = RasterData[row+YUL_row][col+XLL_col];
}
}
//cout << "Wrote the array" << endl;
LSDRaster TrimmedRaster(New_NRows, New_NCols, NewR_XMinimum,
NewR_YMinimum, DataResolution, NoDataValue, NewData,
GeoReferencingStrings);
cout << "Making the raster" << endl;
TrimmedRaster.Update_GeoReferencingStrings();
return TrimmedRaster;
}
//--------------------------------------------------------------------------//
// Function to uplift the north half of the raster at a defined rate.
// e.g. to uplift the north half an order of magnitude faster than the original
// value, set increase_amt to 10.
// FJC 28/06/18
//--------------------------------------------------------------------------//
void LSDRasterMaker::increase_north_half_raster_values(int increase_amt)
{
// loop through the north half of the raster and change the values.
for (int i = 0; i < int(NRows/2); i++)
{
for (int j = 0; j < NCols; j++)
{
RasterData[i][j] = RasterData[i][j] * increase_amt;
}
}
}
//--------------------------------------------------------------------------//
// Function to uplift the south half of the raster at a defined rate.
// e.g. to uplift the north half an order of magnitude faster than the original
// value, set increase_amt to 10.
// FJC 28/06/18
//--------------------------------------------------------------------------//
void LSDRasterMaker::increase_south_half_raster_values(int increase_amt)
{
// loop through the south half of the raster and change the values.
for (int i = int(NRows/2); i < NRows; i++)
{
for (int j = 0; j < NCols; j++)
{
RasterData[i][j] = RasterData[i][j] * increase_amt;
}
}
}
void LSDRasterMaker::increase_south_quarter_raster_values(int increase_amt)
{
// loop through the south half of the raster and change the values.
for (int i = int(NRows - NRows/4); i < NRows; i++)
{
for (int j = 0; j < NCols; j++)
{
RasterData[i][j] = RasterData[i][j] * increase_amt;
}
}
}
void LSDRasterMaker::increase_west_half_raster_values(int increase_amt)
{
// loop through the south half of the raster and change the values.
for (int i = 0; i < NRows; i++)
{
for (int j = 0; j < int(NCols/2); j++)
{
RasterData[i][j] = RasterData[i][j] * increase_amt;
}
}
}
//----------------------------------------------------------------------------//
// Tilted uplift field
// for progressive block tilting over time
//----------------------------------------------------------------------------//
void LSDRasterMaker::tilted_block(float angle, string tilt_boundary)
{
Array2D<float> old_values = RasterData.copy();
Array2D<float> new_values(NRows, NCols, NoDataValue);
// loop through each possible tilt direction and get the new array of values
// after tilting
if (tilt_boundary == "N") // north is tilt boundary - max at the S
{
for (int i = 0; i < NRows; i++)
{
for (int j = 0; j < NCols; j++)
{
// first row stays at same elevation
if (i == 0) { new_values[i][j] = old_values[i][j]; }
// other rows, calculate based on angle
else
{
float this_value = old_values[i][j];
float length = (i + 1) * DataResolution;
new_values[i][j] = (length * tan(angle)) + this_value;
//cout << "old elev: " << this_value << " new elev: " << (length * tan(angle)) + this_value << endl;
}
}
}
}
else if (tilt_boundary == "S") // south is tilt boundary - max elevation at the N
{
for (int i = 0; i < NRows; i++)
{
for (int j = 0; j < NCols; j++)
{
// last row stays at same elevation
if (i == NRows-1) { new_values[i][j] = old_values[i][j]; }
// other rows, calculate based on angle
else
{
float this_value = old_values[i][j];
float length = (NRows - i) * DataResolution;
new_values[i][j] = (length * tan(angle)) + this_value;
//cout << "old elev: " << this_value << " new elev: " << (length * tan(angle)) + this_value << endl;
}
}
}
}
else if (tilt_boundary == "E") // east is tilt boundary - max elevation at the W
{
for (int i = 0; i < NRows; i++)
{
for (int j = 0; j < NCols; j++)
{
// last col stays at same elevation
if (j == NCols-1) { new_values[i][j] = old_values[i][j]; }
// other cols, calculate based on angle
else
{
float this_value = old_values[i][j];
float length = (NCols - i) * DataResolution;
new_values[i][j] = (length * tan(angle)) + this_value;
// cout << "old value: " << this_value << " new elev: " << (length * tan(angle)) + this_value << endl;
}
}
}
}
else if (tilt_boundary == "W") // west is tilt boundary - max elevation at the E
{
for (int i = 0; i < NRows; i++)
{
for (int j = 0; j < NCols; j++)
{
// first col stays at same elevation
if (j == 0) { new_values[i][j] = old_values[i][j]; }
// other cols, calculate based on angle
else
{
float this_value = old_values[i][j];
float length = (j + 1) * DataResolution;
new_values[i][j] = (length * tan(angle)) + this_value;
//cout << "old elev: " << this_value << " new elev: " << (length * tan(angle)) + this_value << endl;
}
}
}
}
else
{
cout << "Warning - you haven't set your boundary to N, W, E, or S. Returning the original raster" << endl;
new_values = old_values;
}
// set the model to the array of new elevations
RasterData = new_values;
}
#endif