-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLSDRaster.cpp
6367 lines (5636 loc) · 216 KB
/
LSDRaster.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
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// LSDRasterSpectral
// Land Surface Dynamics StatsTools
//
// An object within the University
// of Edinburgh Land Surface Dynamics group topographic toolbox
// for manipulating
// and analysing raster data, with a particular focus on topography
//
// 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
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// LSDRaster.cpp
// cpp file for the LSDRaster object
// LSD stands for Land Surface Dynamics
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// This object is written by
// Simon M. Mudd, University of Edinburgh
// David T. Milodowski, University of Edinburgh
// Martin D. Hurst, British Geological Survey
// Fiona Clubb, University of Edinburgh
// Stuart Grieve, University of Edinburgh
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Version 1.0.0 16/07/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// change log
// MASSIVE MERGE: Starting version 1.0.0 on 15/07/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//-----------------------------------------------------------------
//DOCUMENTATION URL: http://www.geos.ed.ac.uk/~s0675405/LSD_Docs/
//-----------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <math.h>
#include <string.h>
#include "TNT/tnt.h"
#include "TNT/jama_lu.h"
#include "TNT/jama_eig.h"
#include "LSDRaster.hpp"
#include "LSDStatsTools.hpp"
#include "LSDIndexRaster.hpp"
#include "LSDShapeTools.hpp"
using namespace std;
using namespace TNT;
using namespace JAMA;
#ifndef LSDRaster_CPP
#define LSDRaster_CPP
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// operators
// SMM, 2012
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
LSDRaster& LSDRaster::operator=(const LSDRaster& rhs)
{
if (&rhs != this)
{
create(rhs.get_NRows(),rhs.get_NCols(),rhs.get_XMinimum(),rhs.get_YMinimum(),
rhs.get_DataResolution(),rhs.get_NoDataValue(),rhs.get_RasterData());
}
return *this;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// the create function. This is default and throws an error
// SMM 2012
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDRaster::create()
{
cout << "LSDRaster line 64 Warning you have an empty LSDRaster!" << endl;
//exit(EXIT_FAILURE);
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// this creates a raster using an infile
// SMM 2012
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDRaster::create(string filename, string extension)
{
read_raster(filename,extension);
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// this creates a raster filled with no data values
// SMM 2012
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDRaster::create(int nrows, int ncols, float xmin, float ymin,
float cellsize, float ndv, Array2D<float> data)
{
NRows = nrows;
NCols = ncols;
XMinimum = xmin;
YMinimum = ymin;
DataResolution = cellsize;
NoDataValue = ndv;
RasterData = data.copy();
if (RasterData.dim1() != NRows)
{
cout << "LSDRaster line 89 dimension of data is not the same as stated in NRows!" << endl;
exit(EXIT_FAILURE);
}
if (RasterData.dim2() != NCols)
{
cout << "LSDRaster line 94 dimension of data is not the same as stated in NRows!" << endl;
exit(EXIT_FAILURE);
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This function reads a DEM
// One has to provide both the filename and the extension
// the '.' between the filename and extension is not included
// for example, if the full filename is test.asc
// then
// filename = "test"
// and
// ext = "asc"
// The full filename could also be "test.01.asc"
// so filename would be "test.01"
// and ext would again be "asc"
//
// SMM 2012
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDRaster::read_raster(string filename, string extension)
{
string string_filename;
string dot = ".";
string_filename = filename+dot+extension;
cout << "The filename is " << string_filename << endl;
if (extension == "asc")
{
// open the data file
ifstream data_in(string_filename.c_str());
//Read in raster data
string str; // a temporary string for discarding text
// read the georeferencing data and metadata
data_in >> str >> NCols >> str >> NRows
>> str >> XMinimum >> str >> YMinimum
>> str >> DataResolution
>> str >> NoDataValue;
cout << "Loading asc file; NCols: " << NCols << " NRows: " << NRows << endl
<< "X minimum: " << XMinimum << " YMinimum: " << YMinimum << endl
<< "Data Resolution: " << DataResolution << " and No Data Value: "
<< NoDataValue << endl;
// this is the array into which data is fed
Array2D<float> data(NRows,NCols,NoDataValue);
// read the data
for (int i=0; i<NRows; ++i)
{
for (int j=0; j<NCols; ++j)
{
data_in >> data[i][j];
}
}
data_in.close();
// now update the objects raster data
RasterData = data.copy();
}
else if (extension == "flt")
{
// float data (a binary format created by ArcMap) has a header file
// this file must be opened first
string header_filename;
string header_extension = "hdr";
header_filename = filename+dot+header_extension;
ifstream ifs(header_filename.c_str());
if( ifs.fail() )
{
cout << "\nFATAL ERROR: the header file \"" << header_filename
<< "\" doesn't exist" << std::endl;
exit(EXIT_FAILURE);
}
else
{
string str;
ifs >> str >> NCols >> str >> NRows
>> str >> XMinimum >> str >> YMinimum
>> str >> DataResolution
>> str >> NoDataValue;
}
ifs.close();
cout << "Loading asc file; NCols: " << NCols << " NRows: " << NRows << endl
<< "X minimum: " << XMinimum << " YMinimum: " << YMinimum << endl
<< "Data Resolution: " << DataResolution << " and No Data Value: "
<< NoDataValue << endl;
// this is the array into which data is fed
Array2D<float> data(NRows,NCols,NoDataValue);
// now read the DEM, using the binary stream option
ifstream ifs_data(string_filename.c_str(), ios::in | ios::binary);
if( ifs_data.fail() )
{
cout << "\nFATAL ERROR: the data file \"" << string_filename
<< "\" doesn't exist" << endl;
exit(EXIT_FAILURE);
}
else
{
float temp;
for (int i=0; i<NRows; ++i)
{
for (int j=0; j<NCols; ++j)
{
ifs_data.read(reinterpret_cast<char*>(&temp), sizeof(temp));
data[i][j] = float(temp);
}
}
}
ifs_data.close();
// now update the objects raster data
RasterData = data.copy();
}
else
{
cout << "You did not enter and approprate extension!" << endl
<< "You entered: " << extension << " options are .flt and .asc" << endl;
exit(EXIT_FAILURE);
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// write_raster
// this function writes a raster. One has to give the filename and extension
// currently the options are for .asc and .flt files
//
// SMM 2012
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDRaster::write_raster(string filename, string extension)
{
string string_filename;
string dot = ".";
string_filename = filename+dot+extension;
cout << "The filename is " << string_filename << endl;
// this first bit of logic is for the asc file.
if (extension == "asc")
{
// 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 << "ncols " << NCols
<< "\nnrows " << NRows
<< "\nxllcorner " << setprecision(14) << XMinimum
<< "\nyllcorner " << setprecision(14) << YMinimum
<< "\ncellsize " << DataResolution
<< "\nNODATA_value " << NoDataValue << endl;
for (int i=0; i<NRows; ++i)
{
for (int j=0; j<NCols; ++j)
{
data_out << setprecision(6) << RasterData[i][j] << " ";
}
if (i != NRows-1) data_out << endl;
}
data_out.close();
}
else if (extension == "flt")
{
// float data (a binary format created by ArcMap) has a header file
// this file must be opened first
string header_filename;
string header_extension = "hdr";
header_filename = filename+dot+header_extension;
ofstream header_ofs(header_filename.c_str());
string str;
header_ofs << "ncols " << NCols
<< "\nnrows " << NRows
<< "\nxllcorner " << setprecision(14) << XMinimum
<< "\nyllcorner " << setprecision(14) << YMinimum
<< "\ncellsize " << DataResolution
<< "\nNODATA_value " << NoDataValue
<< "\nbyteorder LSBFIRST" << endl;
header_ofs.close();
// now do the main data
ofstream data_ofs(string_filename.c_str(), ios::out | ios::binary);
float temp;
for (int i=0; i<NRows; ++i)
{
for (int j=0; j<NCols; ++j)
{
temp = float(RasterData[i][j]);
data_ofs.write(reinterpret_cast<char *>(&temp),sizeof(temp));
}
}
data_ofs.close();
}
else
{
cout << "You did not enter and approprate extension!" << endl
<< "You entered: " << extension << " options are .flt and .asc" << endl;
exit(EXIT_FAILURE);
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// rewrite_with_random_values
// This overwrites existing data with random values
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDRaster::rewrite_with_random_values(float range)
{
// set a seed for the random values
long seed = time(NULL);
for(int row = 0; row<NRows; row++)
{
for(int col = 0; col<NCols; col++)
{
RasterData[row][col] = ran3(&seed)*range;
}
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// /\ | | DIAMOND SQUARE
// \/ |__|
// Routines for the Diamond Square algorithm
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This function allows one to sample across values in the raster while wrapping
// around the sides of the raster
// SMM 9/1/2014
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
float LSDRaster::WrapSample(int row, int col)
{
int wraprow;
int wrapcol;
if(row >=0)
{
wraprow = row % NRows;
}
else
{
wraprow = NRows - (-row % NRows);
}
if(col >= 0)
{
wrapcol = col % NCols;
}
else
{
wrapcol = NCols - (-col % NCols);
}
//cout << "Nrows: " << NRows << " row: " << row << " wraprow: " << wraprow << endl;
//cout << "Ncols: " << NCols << " col: " << col << " wrapcol: " << wrapcol << endl;
return RasterData[wraprow][wrapcol];
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This function allows one to set values in the raster while wrapping
// around the sides of the raster
// SMM 9/1/2014
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDRaster::SetWrapSample(int row, int col, float value)
{
int wraprow;
int wrapcol;
if(row >=0)
{
wraprow = row % NRows;
}
else
{
wraprow = NRows - (-row % NRows);
}
if(col >= 0)
{
wrapcol = col % NCols;
}
else
{
wrapcol = NCols - (-col % NCols);
}
RasterData[wraprow][wrapcol] = value;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This function start with the corners of the algorithm to make individual 'features'
// note: the scale gives the range, so it should be a random value between -scale*0.5 and scale*0.5
// SMM 9/1/2014
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDRaster::DSSetFeatureCorners(int featuresize, float scale)
{
long seed = time(NULL);
// the function starts from -featuresize since in the diamond square step
// of the algorithm it wraps from row and column 0
for (int row = 0; row < NRows; row+= featuresize)
{
for (int col = 0; col<NCols; col+= featuresize)
{
float randn = (ran3(&seed)-0.5)*scale;
//cout << "setting feature corners, row: " << row << " and col: " << col << endl;
SetWrapSample(row,col,randn);
}
}
//cout << "Set the feature corners" << endl;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This function samples on the square:
// a b
//
// x
//
// c d
// The corners are known and the centre is calculated
// SMM 9/1/2014
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDRaster::DSSampleSquare(int row,int col, int size, float value)
{
int hs = size / 2;
float a = WrapSample(col - hs, row - hs);
float b = WrapSample(col + hs, row - hs);
float c = WrapSample(col - hs, row + hs);
float d = WrapSample(col + hs, row + hs);
if (a == -9999 || b == -9999 || c == -9999 || d == -9999)
{
cout << "got a nodata; row: " << row << " and col: " << col << endl;
if (a == -9999)
{
cout << "col - hs: " << col - hs << " and row - hs: " << row - hs << endl;
}
if (b == -9999)
{
cout << "col + hs: " << col + hs << " and row - hs: " << row - hs << endl;
}
if (c == -9999)
{
cout << "col - hs: " << col - hs << " and row + hs: " << row + hs << endl;
}
if (d == -9999)
{
cout << "col + hs: " << col + hs << " and row + hs: " << row + hs << endl;
}
}
SetWrapSample(row, col, ((a + b + c + d) / 4.0) + value);
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This calculates the diamond step
// c
//
//a x b
//
// d
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDRaster::DSSampleDiamond(int row, int col, int size, float value)
{
int hs = size / 2;
double a = WrapSample(col - hs, row);
double b = WrapSample(col + hs, row);
double c = WrapSample(col, row - hs);
double d = WrapSample(col, row + hs);
if (a == -9999 || b == -9999 || c == -9999 || d == -9999)
{
cout << "got a nodata DSSampleDiamond; row: " << row << " and col: " << col << endl;
if (a == -9999)
{
cout << "col - hs: " << col - hs << " and row: " << row << endl;
}
if (b == -9999)
{
cout << "col + hs: " << col + hs << " and row: " << row << endl;
}
if (c == -9999)
{
cout << "col: " << col << " and row - hs: " << row - hs << endl;
}
if (d == -9999)
{
cout << "col: " << col << " and row + hs: " << row + hs << endl;
}
}
SetWrapSample(row, col, ((a + b + c + d) / 4.0) + value);
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// this function performs the core of the diamond square algorithm
// the 'features' have got to be of size 2^n+1, so the
// feature order is the 'n'. THis means that, say a feature order of 4
// will make a feature with size of 17
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDRaster::DiamondSquare_SampleStep(int stepsize, float scale)
{
int halfstep = stepsize / 2;
long seed = time(NULL);
// first do the square step. This gets the sqare for the node
// at the half distance between the starting points
for (int row = -halfstep; row < NRows+halfstep; row += stepsize)
{
for (int col = -halfstep; col < NCols + halfstep; col += stepsize)
{
//cout << "SS row and col: " << row << " " << col << endl;
DSSampleSquare(row, col, stepsize, ((ran3(&seed)-0.5) * scale));
}
}
// now the DiamondStep.
// The first diamond gets the point halfway below the first column
// Second diamond gets the column halfway past the first row.
// That means for col 0 and row 0 it will wrap to the other side
// This means these values will need to be wrapped in the inital
// set corners stage!
for (int row = -stepsize; row < NRows; row += stepsize)
{
for (int col = -stepsize; col < NCols; col += stepsize)
{
//cout << "DS row and col: " << row << " " << col << endl;
DSSampleDiamond(row + halfstep, col, stepsize, ((ran3(&seed)-0.5) * scale));
DSSampleDiamond(row, col + halfstep, stepsize, ((ran3(&seed)-0.5) * scale));
}
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// this is the diamond square algorithm
// it creates a resized diamond square pseudo-fractal raster
// it has the same xllcorner and yllcorner as the original raster,
// but is resized so the NRows and NCols are to the closed power of 2
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
LSDRaster LSDRaster::DiamondSquare(int feature_order, float scale)
{
// determine the size of the padded array:
int PaddedRows = int(pow(2,ceil(log(NRows)/log(2))));
int PaddedCols = int(pow(2,ceil(log(NCols)/log(2))));
// now get the maximum feature size. This will be the minimum
// power of 2 in the x or y direction
int max_feature_size;
if (PaddedRows >= PaddedCols)
{
max_feature_size = PaddedCols;
}
else
{
max_feature_size = PaddedRows;
}
cout << "NRows: " << NRows << " and PaddedRows: " << PaddedRows << endl;
cout << "NCols: " << NCols << " and PaddedCols: " << PaddedCols << endl;
cout << "max_feature_size: " << max_feature_size << endl;
//create an array
Array2D<float> DSRaster_array(PaddedRows,PaddedCols,NoDataValue);
//create LSDRaster diamond square object
LSDRaster DSRaster(PaddedRows, PaddedCols, XMinimum, YMinimum, DataResolution,
NoDataValue, DSRaster_array);
// get the feature size: it must be a power of 2.
int featuresize = pow(2,feature_order);
if (featuresize > max_feature_size)
{
cout << "Your featuresize is too big for the DEM. Changing to max feature size" << endl;
}
cout << "feature size is: " << featuresize << endl;
// now initialize the raster over some feature scale
DSRaster.DSSetFeatureCorners(featuresize, scale);
// now loop through the features, running the diamond square algorithm.
int samplesize = featuresize;
scale = scale/2;
cout << "Starting diamond square algorithm, samplesize is: " << samplesize << endl;
while (samplesize > 1)
{
cout << "Running Diamond square, samplesize: " << samplesize << endl;
DSRaster.DiamondSquare_SampleStep(samplesize,scale);
// halve the sample size and scale
samplesize = samplesize/2;
scale = scale/2;
}
return DSRaster;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Make LSDRaster object using a 'template' raster and an Array2D of data.
// SWDG 29/8/13
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
LSDRaster LSDRaster::LSDRasterTemplate(Array2D<float> InputData){
//do a dimensions check and exit on failure
if (InputData.dim1() == NRows && InputData.dim2() == NCols){
LSDRaster OutputRaster(NRows, NCols, XMinimum, YMinimum, DataResolution, NoDataValue, InputData);
return OutputRaster;
}
else{
cout << "Array dimensions do not match template LSDRaster object" << endl;
exit(EXIT_FAILURE);
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This function generates a hillshade raster using the algorithm outlined in
// Burrough and McDonnell Principles of GIS 1990 and in the ArcMap web help
// http://edndoc.esri.com/arcobjects/9.2/net/shared/geoprocessing/
// spatial_analyst_tools/how_hillshade_works.htm
//
// Takes 3 floats, representing the altitude of the illumination source in
// degrees, the azimuth of the illumination source in degrees and the z factor.
//
// Default values are altitude = 45, azimuth = 315, z_factor = 1
//
// Outputs an LSDRaster object.
//
// SWDG, February 2013
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
LSDRaster LSDRaster::hillshade(float altitude, float azimuth, float z_factor)
{
float PI = 3.14159265;
//print parameters to screen
cout << "Hillshading with altitude: " << altitude
<< ", azimuth: " << azimuth << " and z-factor: " << z_factor << endl;
//create output array
Array2D<float> hillshade(NRows,NCols,NoDataValue);
//convert zenith and azimuth into radians for calculation
float zenith_rad = (90 - altitude) * PI / 180.0;
float azimuth_math = 360-azimuth + 90;
if (azimuth_math >= 360.0) azimuth_math = azimuth_math - 360;
float azimuth_rad = azimuth_math * PI /180.0;
//calculate hillshade value for every non nodata value in the input raster
for (int i = 1; i < NRows-1; ++i){
for (int j = 1; j < NCols-1; ++j){
float slope_rad = 0;
float aspect_rad = 0;
float dzdx = 0;
float dzdy = 0;
if (RasterData[i][j] != NoDataValue){
dzdx = ((RasterData[i][j+1] + 2*RasterData[i+1][j] + RasterData[i+1][j+1]) -
(RasterData[i-1][j-1] + 2*RasterData[i-1][j] + RasterData[i-1][j+1]))
/ (8 * DataResolution);
dzdy = ((RasterData[i-1][j+1] + 2*RasterData[i][j+1] + RasterData[i+1][j+1]) -
(RasterData[i-1][j-1] + 2*RasterData[i][j-1] + RasterData[i+1][j-1]))
/ (8 * DataResolution);
slope_rad = atan(z_factor * sqrt((dzdx*dzdx) + (dzdy*dzdy)));
if (dzdx != 0){
aspect_rad = atan2(dzdy, (dzdx*-1));
if (aspect_rad < 0) aspect_rad = 2*PI + aspect_rad;
}
else{
if (dzdy > 0) aspect_rad = PI/2;
else if (dzdy < 0) aspect_rad = 2 * PI - PI/2;
else aspect_rad = aspect_rad;
}
hillshade[i][j] = 255.0 * ((cos(zenith_rad) * cos(slope_rad)) +
(sin(zenith_rad) * sin(slope_rad) *
cos(azimuth_rad - aspect_rad)));
if (hillshade[i][j] < 0) hillshade[i][j] = 0;
}
}
}
//create LSDRaster hillshade object
LSDRaster hillshade_raster(NRows, NCols, XMinimum, YMinimum, DataResolution,
NoDataValue, hillshade);
return hillshade_raster;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This function generates a hillshade derivative raster using the algorithm outlined in
// Codilean (2006), identifying areas in shadow as 1 and all other values as 0.
//
// Is interfaced through LSDRaster::TopoShield and should not be called directly,
// to generate a hillshade use LSDRaster::hillshade instead.
//
// Takes 2 ints, representing theta, the zenith angle of the illumination source in
// degrees and the azimuth angle, phi, of the illumination source in degrees.
//
// Outputs an Array2D of floats.
//
// SWDG, 11/4/13
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Array2D<float> LSDRaster::Shadow(int theta, int phi)
{
float PI = 3.14159265;
//create array of input data and an output array of same dimensions
Array2D<float> data = RasterData;
Array2D<float> hillshade(NRows,NCols,NoDataValue);
//convert zenith and azimuth into radians for calculation
float zenith_rad = (90 - theta) * PI / 180.0;
float azimuth_math = 360-phi + 90;
if (azimuth_math >= 360.0) azimuth_math = azimuth_math - 360;
float azimuth_rad = azimuth_math * PI /180.0;
//calculate hillshade value for every non nodata value in the input raster
for (int i = 1; i < NRows-1; ++i){
for (int j = 1; j < NCols-1; ++j){
float slope_rad = 0;
float aspect_rad = 0;
float dzdx = 0;
float dzdy = 0;
if (data[i][j] != NoDataValue){
dzdx = ((data[i][j+1] + 2*data[i+1][j] + data[i+1][j+1]) -
(data[i-1][j-1] + 2*data[i-1][j] + data[i-1][j+1]))
/ (8 * DataResolution);
dzdy = ((data[i-1][j+1] + 2*data[i][j+1] + data[i+1][j+1]) -
(data[i-1][j-1] + 2*data[i][j-1] + data[i+1][j-1]))
/ (8 * DataResolution);
slope_rad = atan(sqrt((dzdx*dzdx) + (dzdy*dzdy)));
if (dzdx != 0){
aspect_rad = atan2(dzdy, (dzdx*-1));
if (aspect_rad < 0) aspect_rad = 2*PI + aspect_rad;
}
else{
if (dzdy > 0) aspect_rad = PI/2;
else if (dzdy < 0) aspect_rad = 2 * PI - PI/2;
else aspect_rad = aspect_rad;
}
hillshade[i][j] = acos(((sin(zenith_rad) * cos(slope_rad)) +
(sin(zenith_rad) * sin(slope_rad) *
cos(azimuth_rad - aspect_rad))))*180/PI;
if (hillshade[i][j] > 90) hillshade[i][j] = 1;
else hillshade[i][j] = 0;
}
}
}
//create LSDRaster hillshade object
//LSDRaster hillshade_raster(NRows, NCols, XMinimum, YMinimum, DataResolution,
// NoDataValue, hillshade);
return hillshade;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This function generates a topographic sheilding raster using the algorithm outlined in
// Codilean (2006), creating a raster of values between 0 and 1 whihc can be used as a
// scaling factor in Cosmo analysis.
//
// Goes further than the original algorithm allowing a theoretical theta, phi pair of
// 1,1 to be supplied and although this will increase the computatin time significantly,
// it is much faster than the original Avenue and VBScript implementations.
//
// Takes 2 ints, representing the theta, phi paring required. Codilean (2006) used 5,5
// as the standard values, but in reality values of 10,15 are often preferred to save
// processing time. **phi_step must be a factor of 360**
//
// Outputs an LSDRaster
//
// SWDG, 11/4/13
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
LSDRaster LSDRaster::TopoShield(int theta_step, int phi_step){
//calculate parameter of scaling calculation
int phi_factor = 360/phi_step;
float MaxFactor = 0;
for(int theta = 10; theta < 90; theta += theta_step){
MaxFactor += pow(sin(rad(theta)),3.3) * phi_factor;
}
//calculate maximum scaling factor and write it to an array
MaxFactor += pow(sin(rad(90)),3.3);
Array2D<float> MaxFactorArray(NRows,NCols,MaxFactor);
//Calculate first shadow with theta value of 90 and scale it
Array2D<float> Scaler90(NRows,NCols, pow(sin(rad(90)),3.3));
Array2D<float> FinalArray = Shadow(0,90) * Scaler90;
//loop through all the theta, phi pairs and increment the FinalArray with the scaled values
for(int theta = 10; theta < 90; theta += theta_step){
for(int phi = 0; phi < 360; phi += phi_step){
cout << flush << "\tTheta = " << theta << ", Phi = " << phi << " \r";
Array2D<float> TempArray = Shadow(theta,phi);
Array2D<float> Scaler(NRows, NCols, pow(sin(rad(theta)),3.3));
FinalArray += TempArray * Scaler;
}
}
//create array of ones needed in sheilding calculation
Array2D<float> Ones(NRows,NCols,1);
//Shielding factor calculation
Array2D<float> ShFactor = Ones - (FinalArray/MaxFactorArray);
//deal with nodata values on border of dem - more efficient than doing it in the hillshade function 10s of times.
for (int i = 0; i < NRows; ++i){
for (int j = 0; j < NCols; ++j){
if (ShFactor[i][j] > 10){ //arbitrary number. But don't want to miss any values >1
ShFactor[i][j] = NoDataValue; //as they may indicate an error in the above calculations.
}
}
}
//flush output onto a new line
cout << endl;
//write LSDRaster
LSDRaster Shielding(NRows, NCols, XMinimum, YMinimum, DataResolution, NoDataValue, ShFactor);
return Shielding;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// this looks for isolated instances of nodata and fills them
//
// Not sure about author, I think MDH (SMM comment) 2012
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDRaster::check_isolated_nodata()
{
for (int row=0; row<NRows; ++row)
{
for(int col=0; col<NCols; ++col)
{
if(RasterData[row][col] < 0)
{
cout << "LSDRaster::check_isolated_nodata stargine data point: row: "
<< row << " col: " << col << " data: " << RasterData[row][col];
RasterData[row][col] = NoDataValue;
}
if(RasterData[row][col] == NoDataValue)
{
cout << "LSDRaster::check_isolated_nodata found nodata: row: "
<< row << " col: " << col << " data: " << RasterData[row][col];
}
}
}
cout << "Done!" << endl;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// calculate_polyfit_surface_metrics
//
// This routine houses the code to fit a 6 term polynomial (z =ax^2 + by^2 + cxy
// + dx + ey + f) to a surface, and then use the derivatives of this to
// calculate various useful geometric properties such as slope and curvature.
//
// The surface is fitted to all the points that lie within circular
// neighbourhood that is defined by the designated window radius. The user also
// inputs a binary raster, which tells the program which rasters it wants to
// create (label as "true" to produce them, "false" to ignore them. This has 8
// elements, as listed below: