-
Notifications
You must be signed in to change notification settings - Fork 2
/
LSDGeometry.cpp
1219 lines (1031 loc) · 35.6 KB
/
LSDGeometry.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
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// LSDGeometry
// Land Surface Dynamics LSDGeometry objects
//
// An object within the University
// of Edinburgh Land Surface Dynamics group topographic toolbox
// for dealing with geometric data
//
//
// Developed by:
// Simon M. Mudd
// Martin D. Hurst
// David T. Milodowski
// Stuart W.D. Grieve
// Declan A. Valters
// Fiona Clubb
//
// Copyright (C) 2016 Simon M. Mudd 2016
//
// 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 3 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
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// LSDGeometry.cpp
// LSDGeometry object
// LSD stands for Land Surface Dynamics
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// This object is written by
// Simon M. Mudd, University of Edinburgh
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//-----------------------------------------------------------------
//DOCUMENTATION URL: http://www.geos.ed.ac.uk/~s0675405/LSD_Docs/
//-----------------------------------------------------------------
#ifndef LSDGeometry_CPP
#define LSDGeometry_CPP
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
#include "TNT/tnt.h"
#include "LSDFlowInfo.hpp"
#include "LSDRaster.hpp"
#include "LSDRasterInfo.hpp"
#include "LSDChannel.hpp"
#include "LSDStatsTools.hpp"
#include "LSDShapeTools.hpp"
#include "LSDGeometry.hpp"
#include "LSDRasterInfo.hpp"
using namespace std;
using namespace TNT;
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Creates an LSDGeometry from an LSDRaster
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDGeometry::create()
{
// this does nothing
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Creates an LSDGeometry object. This needs to be lat-long data (since it)
// doesn't come with UTM information
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDGeometry::create(vector<double> x, vector<double> y)
{
// This reads in x and y vectors.
// It judges whether or not these are UTM coordinates by checking if
// any of the data elements are over the maximum or minimum of latitude
// or longitude
int n_x_points = int(x.size());
int n_y_points = int(y.size());
bool is_UTM = false;
if (n_x_points != n_y_points)
{
cout << "X and Y vectors not the same size! No data loaded. " << endl;
}
else if (n_x_points == 0)
{
cout << "Your data vectors are empty! " << endl;
}
else
{
int i = 0;
do
{
// Check if latitude/Northing outside of 90: if it is this is UTM
if (y[i] > 90 || y[i] <-90)
{
is_UTM = true;
}
// Check if longitude/easting outside of 180: if it is this is UTM
if((x[i] > 180 || x[i] <-180))
{
is_UTM = true;
}
i++;
} while (not is_UTM || i < n_x_points);
}
if(is_UTM)
{
cout << "This appears to be UTM data but you have not provided a zone!" << endl;
}
else
{
WGS84Points_longitude = x;
WGS84Points_latitude = y;
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Creates an LSDGeometry object. This needs to be lat-long data (since it)
// doesn't come with UTM information
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDGeometry::create(vector<float> x, vector<float> y)
{
int n_x_points = int(x.size());
int n_y_points = int(y.size());
vector<double> x_double;
vector<double> y_double;
if (n_x_points != n_y_points)
{
cout << "X and Y vectors not the same size! No data loaded. " << endl;
}
for(int i = 0; i<n_x_points; i++)
{
x_double.push_back( double(x[i]));
y_double.push_back( double(y[i]));
}
create(x_double, y_double);
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Creates an LSDGeometry object. This is in UTM format and so has UTM information
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDGeometry::create(vector<double> x, vector<double> y, int thisUTMzone)
{
// This reads in x and y vectors.
// It judges whether or not these are UTM coordinates by checking if
// any of the data elements are over the maximum or minimum of latitude
// or longitude
int n_x_points = int(x.size());
int n_y_points = int(y.size());
if (n_x_points != n_y_points)
{
cout << "X and Y vectors not the same size! No data loaded. " << endl;
}
else if (n_x_points == 0)
{
cout << "Your data vectors are empty! " << endl;
}
else
{
UTMPoints_Easting = x;
UTMPoints_Northing = y;
UTMZone = thisUTMzone;
isNorth = true;
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Creates an LSDGeometry object. This is in UTM format and so has UTM information
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDGeometry::create(vector<float> x, vector<float> y, int thisUTMzone)
{
int n_x_points = int(x.size());
int n_y_points = int(y.size());
vector<double> x_double;
vector<double> y_double;
if (n_x_points != n_y_points)
{
cout << "X and Y vectors not the same size! No data loaded. " << endl;
}
for(int i = 0; i<n_x_points; i++)
{
x_double.push_back( double(x[i]));
y_double.push_back( double(y[i]));
}
create(x_double, y_double, thisUTMzone);
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Creates an LSDGeometry object. This is in UTM format and so has UTM information
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDGeometry::create(vector<double> x, vector<double> y, int thisUTMzone, bool ThisisNorth)
{
// This reads in x and y vectors.
// It judges whether or not these are UTM coordinates by checking if
// any of the data elements are over the maximum or minimum of latitude
// or longitude
int n_x_points = int(x.size());
int n_y_points = int(y.size());
if (n_x_points != n_y_points)
{
cout << "X and Y vectors not the same size! No data loaded. " << endl;
}
else if (n_x_points == 0)
{
cout << "Your data vectors are empty! " << endl;
}
else
{
UTMPoints_Easting = x;
UTMPoints_Northing = y;
UTMZone = thisUTMzone;
isNorth = ThisisNorth;
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Creates an LSDGeometry object. This is in UTM format and so has UTM information
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDGeometry::create(vector<float> x, vector<float> y, int thisUTMzone, bool ThisisNorth)
{
int n_x_points = int(x.size());
int n_y_points = int(y.size());
vector<double> x_double;
vector<double> y_double;
if (n_x_points != n_y_points)
{
cout << "X and Y vectors not the same size! No data loaded. " << endl;
}
for(int i = 0; i<n_x_points; i++)
{
x_double.push_back( double(x[i]));
y_double.push_back( double(y[i]));
}
create(x_double, y_double, thisUTMzone,ThisisNorth);
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This converts points from Lat/Long to UTM
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDGeometry::convert_points_to_UTM()
{
if( WGS84Points_latitude.size() == 0)
{
cout << "Trying to convert from Latitude and Longitude but you don't have any data." << endl;
}
else
{
LSDCoordinateConverterLLandUTM Converter;
// set the default ellipsoid to WGS84
int eId = 22;
double Northing,Easting;
double Lat,Long;
int thisZone = 1; // This gets replaced in the LLtoUTM operation
vector<double> new_UTM_Northing;
vector<double> new_UTM_Easting;
// get UTMZone of the first coordinate and set that as the object zone
Converter.LLtoUTM(eId, WGS84Points_latitude[0], WGS84Points_longitude[0], Northing, Easting, thisZone);
// get the zone and the isNorth parameters
UTMZone = thisZone;
if(WGS84Points_latitude[0] > 0)
{
isNorth = true;
}
else
{
isNorth = false;
}
int n_nodes = int(WGS84Points_longitude.size());
for(int i = 0; i<n_nodes; i++)
{
Lat =WGS84Points_latitude[i];
Long =WGS84Points_longitude[i];
// We use force zone here since all points will be forced into the UTM zone
// of the first data element.
Converter.LLtoUTM_ForceZone(eId, Lat, Long, Northing, Easting, UTMZone);
new_UTM_Northing.push_back(Northing);
new_UTM_Easting.push_back(Easting);
}
UTMPoints_Easting = new_UTM_Easting;
UTMPoints_Northing = new_UTM_Northing;
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Creates an LSDGeometry from UTM to Lat/Long
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDGeometry::convert_points_to_LatLong()
{
if( UTMPoints_Northing.size() == 0)
{
cout << "Trying to convert from UTM but you don't have any data." << endl;
}
else
{
LSDCoordinateConverterLLandUTM Converter;
// set the default ellipsoid to WGS84
int eId = 22;
double Northing,Easting;
double Lat,Long;
vector<double> new_WGS84Points_latitude;
vector<double> new_WGS84Points_longitude;
int n_nodes = int(UTMPoints_Easting.size());
for(int i = 0; i<n_nodes; i++)
{
Northing =UTMPoints_Northing[i];
Easting =UTMPoints_Easting[i];
Converter.UTMtoLL(eId, Northing, Easting, UTMZone, isNorth, Lat,Long);
new_WGS84Points_latitude.push_back(Lat);
new_WGS84Points_longitude.push_back(Long);
}
WGS84Points_latitude = new_WGS84Points_latitude;
WGS84Points_longitude = new_WGS84Points_longitude;
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This prints the underlying point data to csv
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDGeometry::print_points_to_csv(string path, string file_prefix)
{
string fname = path+"."+file_prefix;
int n_UTM_nodes = int(UTMPoints_Northing.size());
int n_WGS_nodes = int(WGS84Points_latitude.size());
bool have_UTM = false;
bool have_WGS = false;
if (n_UTM_nodes > 0)
{
have_UTM = true;
}
if (n_WGS_nodes > 0)
{
have_WGS = true;
}
if (not have_UTM && not have_WGS)
{
cout << "Trying to print points but there is no data!" << endl;
}
else
{
if (have_UTM && not have_WGS)
{
convert_points_to_LatLong();
n_WGS_nodes = int(WGS84Points_latitude.size());
}
if (not have_UTM && have_WGS)
{
convert_points_to_UTM();
n_UTM_nodes = int(UTMPoints_Northing.size());
}
ofstream csv_out;
csv_out.open(fname.c_str());
csv_out << "latitude,longitude,Northing,Easting" << endl;
for (int i = 0; i< n_UTM_nodes; i++)
{
csv_out.precision(7);
csv_out << WGS84Points_latitude[i] << "," << WGS84Points_longitude[i] << ",";
csv_out.precision(9);
csv_out << UTMPoints_Northing[i] << "," << UTMPoints_Easting[i] << endl;
}
csv_out.close();
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Gets the row and column of all points in the raster
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDGeometry::find_row_and_col_of_points(LSDRasterInfo& RI, vector<int>& RowOfNodes, vector<int>& ColOfNodes)
{
// empty vectors for adding the rows and columns
vector<int> row_vec;
vector<int> col_vec;
// first check to see if there is any UTM data
if (UTMPoints_Northing.size() == 0)
{
// If you don't have UTM points, convert WGS to UTM
convert_points_to_UTM();
}
// Get the X and Y minimums
float XMinimum = RI.get_XMinimum();
float YMinimum = RI.get_YMinimum();
int NoDataValue = int(RI.get_NoDataValue());
float DataResolution = RI.get_DataResolution();
int NRows = RI.get_NRows();
int NCols = RI.get_NCols();
int this_row;
int this_col;
// Shift origin to that of dataset
float X_coordinate_shifted_origin;
float Y_coordinate_shifted_origin;
// the actual coordinates
float X_coordinate;
float Y_coordinate;
// now loop through nodes
int N_nodes = int(UTMPoints_Northing.size());
for(int i = 0; i<N_nodes; i++)
{
this_row = NoDataValue;
this_col = NoDataValue;
X_coordinate = UTMPoints_Easting[i];
Y_coordinate = UTMPoints_Northing[i];
// Shift origin to that of dataset
X_coordinate_shifted_origin = X_coordinate - XMinimum;
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(round(Y_coordinate_shifted_origin/DataResolution));
//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_vec.push_back(this_row);
col_vec.push_back(this_col);
}
RowOfNodes = row_vec;
ColOfNodes = col_vec;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Gets the row and column of a single point.
// It includes out of bounds points (i.e., with rows and columsn either less than zero
// or greater than NRows or NCols)
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDGeometry::find_row_and_col_of_point_inc_out_of_bounds(LSDRasterInfo& RI,
int point_index, int& RowOfNode, int& ColOfNode, bool& IsOutOfBounds)
{
// Get the X and Y minimums
float XMinimum = RI.get_XMinimum();
float YMinimum = RI.get_YMinimum();
int NoDataValue = int(RI.get_NoDataValue());
float DataResolution = RI.get_DataResolution();
int NRows = RI.get_NRows();
int NCols = RI.get_NCols();
int this_row = NoDataValue;
int this_col = NoDataValue;
bool OutOfBounds = true;
// Shift origin to that of dataset
float X_coordinate_shifted_origin;
float Y_coordinate_shifted_origin;
// the actual coordinates
float X_coordinate;
float Y_coordinate;
// first check to see if there is any UTM data
if (UTMPoints_Northing.size() == 0)
{
// If you don't have UTM points, convert WGS to UTM
convert_points_to_UTM();
}
// Test to see if node index is in range
int N_nodes = int(UTMPoints_Northing.size());
if (point_index < 0 || point_index >= N_nodes)
{
cout << "Your node index is out of the bounds of the point data." << endl;
}
else
{
X_coordinate = UTMPoints_Easting[point_index];
Y_coordinate = UTMPoints_Northing[point_index];
// Shift origin to that of dataset
X_coordinate_shifted_origin = X_coordinate - XMinimum;
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(round(Y_coordinate_shifted_origin/DataResolution));
//cout << "Getting row and col, " << row_point << " " << col_point << endl;
if(col_point > 0 && col_point < NCols-1)
{
//cout << "I'm in the cols!";
if(row_point > 0 && row_point < NRows -1)
{
//cout << " and in the rows, I'm not out of bounds!" << endl;
OutOfBounds = false;
}
}
this_row = row_point;
this_col = col_point;
}
RowOfNode = this_row;
ColOfNode = this_col;
IsOutOfBounds = OutOfBounds;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Gets the row and column of a single point.
// It includes out of bounds points (i.e., with rows and columsn either less than zero
// or greater than NRows or NCols)
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDGeometry::find_row_and_col_of_point_inc_out_of_bounds(LSDRasterInfo& RI,
double UTM_Easting, double UTM_northing, int& RowOfNode, int& ColOfNode,
bool& IsOutOfBounds)
{
// Get the X and Y minimums
float XMinimum = RI.get_XMinimum();
float YMinimum = RI.get_YMinimum();
int NoDataValue = int(RI.get_NoDataValue());
float DataResolution = RI.get_DataResolution();
int NRows = RI.get_NRows();
int NCols = RI.get_NCols();
int this_row = NoDataValue;
int this_col = NoDataValue;
bool OutOfBounds = true;
// Shift origin to that of dataset
float X_coordinate_shifted_origin;
float Y_coordinate_shifted_origin;
// the actual coordinates
float X_coordinate;
float Y_coordinate;
// first check to see if there is any UTM data
if (UTMPoints_Northing.size() == 0)
{
// If you don't have UTM points, convert WGS to UTM
convert_points_to_UTM();
}
X_coordinate = UTM_Easting;
Y_coordinate = UTM_northing;
// Shift origin to that of dataset
X_coordinate_shifted_origin = X_coordinate - XMinimum;
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(round(Y_coordinate_shifted_origin/DataResolution));
//cout << "POINTY!! Getting row and col, " << row_point << " " << col_point << endl;
if(col_point > 0 && col_point < NCols-1)
{
//cout << "I'm in the cols!";
if(row_point > 0 && row_point < NRows -1)
{
//cout << " and in the rows, I'm not out of bounds!" << endl;
OutOfBounds = false;
}
}
this_row = row_point;
this_col = col_point;
RowOfNode = this_row;
ColOfNode = this_col;
IsOutOfBounds = OutOfBounds;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This checks if there is UTM data and if not updates it
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDGeometry::check_and_update_UTM()
{
int n_nodes = int(UTMPoints_Northing.size());
if (n_nodes == 0)
{
convert_points_to_UTM();
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// These functions get the maximum and minimum values for Northing and easting
// In general these are for scaling svg output.
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
double LSDGeometry::get_max_UTM_Northing()
{
check_and_update_UTM();
int n_nodes = int(UTMPoints_Northing.size());
double max_Northing = UTMPoints_Northing[0];
for(int n = 0; n<n_nodes; n++)
{
if(UTMPoints_Northing[n] > max_Northing)
{
max_Northing = UTMPoints_Northing[n];
}
}
return max_Northing;
}
double LSDGeometry::get_min_UTM_Northing()
{
check_and_update_UTM();
int n_nodes = int(UTMPoints_Northing.size());
double min_Northing = UTMPoints_Northing[0];
for(int n = 0; n<n_nodes; n++)
{
if(UTMPoints_Northing[n] < min_Northing)
{
min_Northing = UTMPoints_Northing[n];
}
}
return min_Northing;
}
double LSDGeometry::get_max_UTM_Easting()
{
check_and_update_UTM();
int n_nodes = int(UTMPoints_Easting.size());
double max_Easting = UTMPoints_Easting[0];
for(int n = 0; n<n_nodes; n++)
{
if(UTMPoints_Easting[n] > max_Easting)
{
max_Easting = UTMPoints_Easting[n];
}
}
return max_Easting;
}
double LSDGeometry::get_min_UTM_Easting()
{
check_and_update_UTM();
int n_nodes = int(UTMPoints_Easting.size());
double min_Easting = UTMPoints_Easting[0];
for(int n = 0; n<n_nodes; n++)
{
if(UTMPoints_Easting[n] < min_Easting)
{
min_Easting = UTMPoints_Easting[n];
}
}
return min_Easting;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This checks to see if the NodeOrder exists and if not simply links the poitns in order
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDPolyline::make_simple_polyline()
{
vector<int> new_node_order;
int n_order = (node_order.size());
if(n_order == 0)
{
int n_nodes;
n_nodes = int(UTMPoints_Easting.size());
if( n_nodes == 0)
{
n_nodes = int(WGS84Points_latitude.size());
}
for(int i = 0; i<n_nodes; i++)
{
new_node_order.push_back(i);
}
node_order = new_node_order;
}
else
{
cout << "This polyline already has a node order: I won't do anything" << endl;
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This checks to see if the NodeOrder exists and if not simply links the points in order
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDPolyline::force_simple_polyline()
{
vector<int> new_node_order;
int n_nodes;
n_nodes = int(UTMPoints_Easting.size());
if( n_nodes == 0)
{
n_nodes = int(WGS84Points_latitude.size());
}
for(int i = 0; i<n_nodes; i++)
{
new_node_order.push_back(i);
}
node_order = new_node_order;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// This gets affected pixels along line
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDPolyline::get_affected_pixels_in_line(LSDRasterInfo& RI,
vector<int>& affected_rows, vector<int>& affected_cols)
{
// get the number of segments in the node order
int n_points = (node_order.size());
if (n_points == 0)
{
cout << "This polyline doesn't have segments: I am making segments in the node order" << endl;
make_simple_polyline();
n_points = (node_order.size());
}
int start_node;
int end_node;
vector<int> all_pixel_rows;
vector<int> all_pixel_cols;
// now loop through the segments collecting the nodes
for (int seg = 0; seg< n_points-1; seg++)
{
start_node = node_order[seg];
end_node = node_order[seg+1];
vector<int> these_rows;
vector<int> these_cols;
get_affected_pixels_in_line_segment_brute_force(RI,these_rows, these_cols,
start_node, end_node);
int n_affected_nodes = int(these_rows.size());
for (int n = 0; n<n_affected_nodes; n++)
{
all_pixel_rows.push_back(these_rows[n]);
all_pixel_cols.push_back(these_cols[n]);
}
}
affected_rows = all_pixel_rows;
affected_cols = all_pixel_cols;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This is a brute force way to get all the affected pixels. It just slides along the path
// in jumps of fixed distance and collects pixels.
// There is some probablity of missing out pixels with this method if the
// line intersects a very small corner.
// collecting pixels
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDPolyline::get_affected_pixels_in_line_segment_brute_force(LSDRasterInfo& RI,
vector<int>& affected_rows, vector<int>& affected_cols,
int start_node, int end_node)
{
// steps are in 1/1000th of the grid resolution
//double step = 0.001;
double step = 0.001;
double Slope;
// get some info from the RasterInfo object
double DataResolution = double(RI.get_DataResolution());
// make empty vectors that will contain the nodes
vector<int> node_row;
vector<int> node_col;
// check to make sure there are UTM coords
check_and_update_UTM();
int n_nodes = int(UTMPoints_Easting.size());
//cout << n_nodes << endl;
bool valid_nodes = true;
// get the starting node location in UTM
if (start_node < 0 || start_node >= n_nodes)
{
cout << "Start node does not have a valid index" << endl;
valid_nodes = false;
}
if (end_node < 0 || end_node >= n_nodes)
{
cout << "End node does not have a valid index" << endl;
valid_nodes = false;
}
// The start and end nodes are valid, enter the analysis
if (valid_nodes)
{
double UTM_East_start = UTMPoints_Easting[start_node];
double UTM_North_start = UTMPoints_Northing[start_node];
double UTM_East_end = UTMPoints_Easting[end_node];
double UTM_North_end = UTMPoints_Northing[end_node];
//cout << "Start E: " << UTM_East_start << " N: " << UTM_North_start << endl;
//cout << "end E: " << UTM_East_end << " N: " << UTM_North_end << endl;
// get the row and column of both the start and end node
int start_row,start_col;
int end_row,end_col;
bool OOB_start = true;
bool OOB_end = true;
find_row_and_col_of_point_inc_out_of_bounds(RI, start_node, start_row, start_col, OOB_start);
find_row_and_col_of_point_inc_out_of_bounds(RI, end_node, end_row, end_col, OOB_end);
//cout << "Now looking at the start and end points"<< endl;
//cout << "SR: " << start_row << " SC: " << start_col << " OOB: " << OOB_start << endl;
//cout << "ER: " << end_row << " EC: " << end_col << " OOB: " << OOB_end << endl;
int current_col = start_col;
int current_row = start_row;
double CurrentEasting = UTM_East_start;
double CurrentNorthing = UTM_North_start;
double denominator = UTM_East_end-UTM_East_start;
double numerator = UTM_North_end-UTM_North_start;
//cout << "numerator: " << numerator << endl;
//cout << "denominator: " << denominator << endl;
// Set up the increments along the line
double NorthingIncrement;
double EastingIncrement;
if(denominator == 0)
{
EastingIncrement = 0;
NorthingIncrement = DataResolution*step;
}
else
{
if (numerator == 0)
{
EastingIncrement = DataResolution*step;
NorthingIncrement = 0;
}
else
{
Slope = numerator/denominator;
//cout << "Slope is: " << Slope << endl;
if (numerator*numerator > denominator*denominator)
{
if(numerator > 0)
{
NorthingIncrement = DataResolution*step;
}
else
{
NorthingIncrement = -DataResolution*step;
}
EastingIncrement = NorthingIncrement/Slope;
}
else
{
if(denominator > 0)
{
EastingIncrement = DataResolution*step;
}
else
{
EastingIncrement = -DataResolution*step;
}
NorthingIncrement = EastingIncrement*Slope;
}
}
}
//cout << "DataResolution: " << DataResolution << " DeltaE: " << EastingIncrement << endl
// << "DeltaN: " << NorthingIncrement << endl;
// This is for debugging!
//ofstream points_out;
//points_out.open("/home/smudd/SMMDataStore/analysis_for_papers/Test_map_chi_gradient/test_force_points.csv");
//points_out.open("/LSDTopoTools/Topographic_projects/Mandakini/mandakini_line_test.csv");
//points_out.precision(9);
//points_out << "X,Y,row,col" << endl;
// Now I've got the increments.
int last_row = current_row;
int last_col = current_col;
//cout << "OOB_start is: " << OOB_start << endl;
if(not OOB_start)
{
//cout << "Pushing first points" << endl;