-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLSDIndexRaster.cpp
3227 lines (2779 loc) · 99.9 KB
/
LSDIndexRaster.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
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// LSDIndexRaster
// Land Surface Dynamics IndexRaster
//
// 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
//
// The IndexRaster object stores only integer values and is used mostly
// for storing indices into raster data.
//
// 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
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// LSDIndexRaster.cpp
// cpp file for the LSDIndexRaster object
// LSD stands for Land Surface Dynamics
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// This object is written by
// Simon M. Mudd, University of Edinburgh
// David Milodowski, University of Edinburgh
// Martin D. Hurst, British Geological Survey
// <your name here>
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Version 0.0.1 20/08/2012
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//-----------------------------------------------------------------
//DOCUMENTATION URL: http://www.geos.ed.ac.uk/~s0675405/LSD_Docs/
//-----------------------------------------------------------------
#ifndef LSDIndexRaster_CPP
#define LSDIndexRaster_CPP
#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <numeric>
#include "TNT/tnt.h"
#include "LSDIndexRaster.hpp"
#include "LSDRaster.hpp"
#include "LSDStatsTools.hpp"
#include "LSDShapeTools.hpp"
using namespace std;
using namespace TNT;
// operators
// SMM 2012
LSDIndexRaster& LSDIndexRaster::operator=(const LSDIndexRaster& 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(),
rhs.get_GeoReferencingStrings() );
}
return *this;
}
// the create function. This is default and throws an error
// SMM 2012
void LSDIndexRaster::create()
{
//cout << "You need to initialize with a filename!" << endl;
//exit(EXIT_FAILURE);
}
// this creates a raster using an infile
// SMM 2012
void LSDIndexRaster::create(string filename, string extension)
{
read_raster(filename,extension);
vector<int> list_unique_values;
}
// this creates a raster filled with the data in data
// SMM 2012
void LSDIndexRaster::create(int nrows, int ncols, float xmin, float ymin,
float cellsize, int ndv, Array2D<int> data)
{
NRows = nrows;
NCols = ncols;
XMinimum = xmin;
YMinimum = ymin;
DataResolution = cellsize;
NoDataValue = ndv;
RasterData = data.copy();
if (RasterData.dim1() != NRows)
{
cout << "LSDIndexRaster create::dimension of data is not the same as stated in NRows!" << endl;
exit(EXIT_FAILURE);
}
if (RasterData.dim2() != NCols)
{
cout << "LSDIndexRaster create::dimension of data is not the same as stated in NRows!" << endl;
exit(EXIT_FAILURE);
}
vector<int> list_unique_values;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// similar to above but contains the georeferencing
// SMM 2014
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDIndexRaster::create(int nrows, int ncols, float xmin, float ymin,
float cellsize, int ndv, Array2D<int> data, map<string,string> GRS_map)
{
//cout << "YOYOYOYOY" << endl;
//cout << "NR: " << NRows << " NC: " << NCols << endl;
NRows = nrows;
NCols = ncols;
XMinimum = xmin;
YMinimum = ymin;
DataResolution = cellsize;
NoDataValue = ndv;
GeoReferencingStrings = GRS_map;
vector<int> list_unique_values;
RasterData = data.copy();
if (RasterData.dim1() != NRows)
{
cout << "LSDIndexRaster create::dimension of data is not the same as stated in NRows!" << endl;
exit(EXIT_FAILURE);
}
if (RasterData.dim2() != NCols)
{
cout << "LSDIndexRaster create::dimension of data is not the same as stated in NRows!" << endl;
exit(EXIT_FAILURE);
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// similar to above but contains no data and has a constant value
// SMM 2016
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDIndexRaster::create(int nrows, int ncols, float xmin, float ymin,
float cellsize, int ndv, map<string,string> GRS_map, int ConstValue)
{
//cout << "YOYOYOYOY" << endl;
//cout << "NR: " << NRows << " NC: " << NCols << endl;
NRows = nrows;
NCols = ncols;
XMinimum = xmin;
YMinimum = ymin;
DataResolution = cellsize;
NoDataValue = ndv;
GeoReferencingStrings = GRS_map;
vector<int> list_unique_values;
Array2D<int> TempData(NRows,NCols,ConstValue);
RasterData = TempData.copy();
if (RasterData.dim1() != NRows)
{
cout << "LSDIndexRaster create::dimension of data is not the same as stated in NRows!" << endl;
exit(EXIT_FAILURE);
}
if (RasterData.dim2() != NCols)
{
cout << "LSDIndexRaster create::dimension of data is not the same as stated in NRows!" << endl;
exit(EXIT_FAILURE);
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Creates an LSDIndexRaster from an LSDRaster by rounding floats to ints
// Doesn't work yet
// MDH, Feb 2015
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDIndexRaster::create(LSDRaster& NonIntLSDRaster)
{
NRows = NonIntLSDRaster.get_NRows();
NCols = NonIntLSDRaster.get_NCols();
XMinimum = NonIntLSDRaster.get_XMinimum();
YMinimum = NonIntLSDRaster.get_YMinimum();
DataResolution = NonIntLSDRaster.get_DataResolution();
NoDataValue = NonIntLSDRaster.get_NoDataValue();
GeoReferencingStrings = NonIntLSDRaster.get_GeoReferencingStrings();
Array2D<float> RasterDataFloat = NonIntLSDRaster.get_RasterData();
vector<int> list_unique_values;
//Declarations
Array2D<int> RasterDataInt(NRows,NCols,NoDataValue);
for (int i=0; i<NRows; ++i)
{
for (int j=0; j<NCols; ++j)
{
RasterDataInt[i][j] = int(RasterDataFloat[i][j]+0.5);
}
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Creates an LSDIndexRaster by taking the same shape as an LSDRaster
// and setting everything to a background value
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDIndexRaster::create(LSDRaster& ARaster, int ConstValue)
{
NRows = ARaster.get_NRows();
NCols = ARaster.get_NCols();
XMinimum = ARaster.get_XMinimum();
YMinimum = ARaster.get_YMinimum();
DataResolution = ARaster.get_DataResolution();
NoDataValue = ARaster.get_NoDataValue();
GeoReferencingStrings = ARaster.get_GeoReferencingStrings();
Array2D<int> RasterDataInt(NRows,NCols,ConstValue);
vector<int> list_unique_values;
RasterData = RasterDataInt.copy();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// 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 coult also be "test.01.asc"
// so filename would be "test.01"
// and ext would again be "asc"
// SMM 2012
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDIndexRaster::read_raster(string filename, string extension)
{
string string_filename;
string dot = ".";
string_filename = filename+dot+extension;
//cout << "The filename is " << string_filename << endl;
int DataType = 2;
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<int> 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 flt 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<int> 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] = int(temp);
}
}
}
ifs_data.close();
// now update the objects raster data
RasterData = data.copy();
}
else if (extension == "bil")
{
// 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;
int NoDataExists = 0;
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;
if (str != "ENVI")
{
cout << "\nFATAL ERROR: this is not an ENVI header file!, first line is: "
<< str << endl;
exit(EXIT_FAILURE);
}
else
{
// the the rest of the lines
int NChars = 5000; // need a big buffer beacause of the projection string
char* thisline = new char[NChars]; // char thisline[NChars]; <- ADAPTATION TO clang and MSVC
vector<string> lines;
while( ifs.getline(thisline, NChars) )
{
lines.push_back(thisline);
}
//cout << "Number of lines is: " << lines.size() << endl;
//for(int i = 0; i< int(lines.size()); i++)
//{
// cout << "Line["<<i<<"]: " << lines[i] << endl;
//}
// now loop through and get the number of rows
int counter = 0;
int NLines = int(lines.size());
int this_NRows = 0;
size_t found;
string str_find = "lines";
while (counter < NLines)
{
found = lines[counter].find(str_find);
if (found!=string::npos)
{
// get the data using a stringstream
istringstream iss(lines[counter]);
iss >> str >> str >> str;
this_NRows = atoi(str.c_str());
//cout << "NRows = " << this_NRows << endl;
NRows = this_NRows;
// advance to the end so you move on to the new loop
counter = lines.size();
}
else
{
counter++;
}
}
// get data type
counter = 0;
str_find = "data type";
while (counter < NLines)
{
found = lines[counter].find(str_find);
if (found!=string::npos)
{
// get the data using a stringstream
istringstream iss(lines[counter]);
iss >> str >> str >> str >> str >> str;
DataType = atoi(str.c_str());
//cout << "Data Type = " << DataType << endl;
// advance to the end so you move on to the new loop
counter = lines.size();
}
else
{
counter++;
}
}
// get the number of columns
counter = 0;
int this_NCols = 0;
str_find = "samples";
while (counter < NLines)
{
found = lines[counter].find(str_find);
if (found!=string::npos)
{
// get the data using a stringstream
istringstream iss(lines[counter]);
iss >> str >> str >> str;
this_NCols = atoi(str.c_str());
//cout << "NCols = " << this_NCols << endl;
NCols = this_NCols;
// advance to the end so you move on to the new loop
counter = lines.size();
}
else
{
counter++;
}
}
// get data ignore value
counter = 0;
float this_NoDataValue = 0;
str_find = "data ignore value";
while (counter < NLines)
{
found = lines[counter].find(str_find);
if (found!=string::npos)
{
// get the data using a stringstream
istringstream iss(lines[counter]);
iss >> str >> str >> str >> str >> str;
this_NoDataValue = atoi(str.c_str());
//cout << "NCols = " << this_NCols << endl;
NoDataValue = this_NoDataValue;
NoDataExists = 1; // set this to true
// advance to the end so you move on to the new loop
counter = lines.size();
}
else
{
counter++;
}
}
// get the map info
counter = 0;
string this_map_info = "empty";
str_find = "map info";
while (counter < NLines)
{
found = lines[counter].find(str_find);
if (found!=string::npos)
{
//cout << "Found map info on line " << counter << '\n';
// now split the line
size_t start_pos;
size_t end_pos;
string open_curly_bracket = "{";
string closed_curly_bracket = "}";
start_pos = lines[counter].find(open_curly_bracket);
end_pos = lines[counter].find(closed_curly_bracket);
//cout << "startpos: " << start_pos << " and end pos: " << end_pos << endl;
string info_str = lines[counter].substr(start_pos+1, end_pos-start_pos-1);
//cout << "\nThe map info string is:\n" << info_str << endl;
string mi_key = "ENVI_map_info";
GeoReferencingStrings[mi_key] = info_str;
// now parse the string
vector<string> mapinfo_strings;
istringstream iss(info_str);
while( iss.good() )
{
string substr;
getline( iss, substr, ',' );
mapinfo_strings.push_back( substr );
}
XMinimum = atof(mapinfo_strings[3].c_str());
float YMax = atof(mapinfo_strings[4].c_str());
DataResolution = atof(mapinfo_strings[5].c_str());
// get Y minimum
// IMPORTANT THIS USES CONVENTION THAT THE MINIMUM AND MAXIMUM VALUES
// ARE AT THE PIXEL EDGES AS IN QGIS!!!
YMinimum = YMax - (NRows)*DataResolution;
//using a string comparison as float(X) != float(X) in many cases due to floating point math
// http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm - SWDG
if (mapinfo_strings[5] != mapinfo_strings[6])
{
cout << "Warning! Loading ENVI DEM, but X and Y data spacing are different!" << endl;
}
//cout << "Xmin: " << XMinimum << " YMin: " << YMinimum << " spacing: "
// << DataResolution << endl;
counter = lines.size();
}
else
{
counter++;
}
}
// get the projection string
counter = 0;
string this_coordinate_system_string = "empty";
str_find = "coordinate system string";
while (counter < NLines)
{
found = lines[counter].find(str_find);
if (found!=string::npos)
{
//cout << "Found coordinate system string on line " << counter << '\n';
// now split the line
size_t start_pos;
size_t end_pos;
string open_curly_bracket = "{";
string closed_curly_bracket = "}";
start_pos = lines[counter].find(open_curly_bracket);
end_pos = lines[counter].find(closed_curly_bracket);
//cout << "startpos: " << start_pos << " and end pos: " << end_pos << endl;
string csys_str = lines[counter].substr(start_pos+1, end_pos-start_pos-1);
//cout << "\nThe coordinate system string is:\n" << csys_str << endl;
string cs_key = "ENVI_coordinate_system";
GeoReferencingStrings[cs_key] = csys_str;
counter = lines.size();
}
else
{
counter++;
}
}
}
}
ifs.close();
// this is the array into which data is fed
if (NoDataExists == 0)
{
NoDataValue = -9999;
}
//bool set_NDV = false;
Array2D<int> 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
{
if (DataType == 2)
{
//cout << "Loading raster, recasting data from int to float!" << endl;
int temp;
//cout << "Integer size: " << sizeof(temp) << endl;
for (int i=0; i<NRows; ++i)
{
for (int j=0; j<NCols; ++j)
{
ifs_data.read(reinterpret_cast<char*>(&temp), 2);
//cout << temp << " ";
data[i][j] = int(temp);
if (data[i][j]<-1e10)
{
data[i][j] = NoDataValue;
}
}
//cout << endl;
}
}
else if (DataType == 4)
{
float temp;
//cout << "Float size: " << sizeof(temp) << endl;
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] = int(temp);
if (data[i][j]<-1e10)
{
data[i][j] = NoDataValue;
}
}
}
}
else if (DataType == 13)
{
unsigned long int temp;
//cout << "Float size: " << sizeof(temp) << endl;
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] = int(temp);
if (data[i][j]<-1e10)
{
data[i][j] = NoDataValue;
}
}
}
}
else
{
cout << "WARNING loading ENVI raster with unusual data type. " << endl
<< "If you get a crazy DEM go to LINE 604 of LSDIndexRaster.cpp to debug" << endl;
int temp; // might need to change this
//cout << "Float size: " << sizeof(temp) << endl;
for (int i=0; i<NRows; ++i)
{
for (int j=0; j<NCols; ++j)
{
// Use data type to control the bytes being read for each entry
ifs_data.read(reinterpret_cast<char*>(&temp), DataType);
data[i][j] = int(temp);
if (data[i][j]<-1e10)
{
data[i][j] = NoDataValue;
}
}
}
}
}
ifs_data.close();
//cout << "Loading ENVI bil file; NCols: " << NCols << " NRows: " << NRows << endl
// << "X minimum: " << XMinimum << " YMinimum: " << YMinimum << endl
// << "Data Resolution: " << DataResolution << " and No Data Value: "
// << NoDataValue << endl;
// 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 LSDIndexRaster::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 if (extension == "bil")
{
// 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;
// you need to strip the filename
string frontslash = "/";
size_t found = string_filename.find_last_of(frontslash);
//cout << "Found is: " << found << endl;
int length = int(string_filename.length());
string this_fname = string_filename.substr(found+1,length-found-1);
//cout << "fname is: " << this_fname << endl;
ofstream header_ofs(header_filename.c_str());
string str;
float temp;
int data_type = sizeof(temp);
header_ofs << "ENVI" << endl;
header_ofs << "description = {" << endl << this_fname << "}" << endl;
header_ofs << "samples = " << NCols << endl;
header_ofs << "lines = " << NRows << endl;
header_ofs << "bands = 1" << endl;
header_ofs << "header offset = 0" << endl;
header_ofs << "file type = ENVI Standard" << endl;
header_ofs << "data type = " << data_type << endl;
header_ofs << "interleave = bsq" << endl;
header_ofs << "byte order = 0" << endl;
// now check to see if there are the map info and coordinate system
map<string,string>::iterator iter;
string cs_str_key = "ENVI_coordinate_system";
string mi_str_key = "ENVI_map_info";
string cs_str;
string mi_str;
iter = GeoReferencingStrings.find(mi_str_key);
if (iter != GeoReferencingStrings.end() )
{
mi_str = (*iter).second;
//cout << "Map info system string exists, it is: " << mi_str << endl;
header_ofs << "map info = {"<<mi_str<<"}" << endl;
}
else
{
cout << "Warning, writing ENVI file but no map info string" << endl;
}
iter = GeoReferencingStrings.find(cs_str_key);
if (iter != GeoReferencingStrings.end() )
{
cs_str = (*iter).second;
//cout << "Coord, system string exists, it is: " << cs_str << endl;
header_ofs << "coordinate system string = {"<<cs_str<<"}" << endl;
}
else
{
cout << "Warning, writing ENVI file but no coordinate system string" << endl;
}
header_ofs << "data ignore value = " << NoDataValue << endl;
header_ofs.close();
// now do the main data
ofstream data_ofs(string_filename.c_str(), ios::out | ios::binary);
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),data_type);
}
}
data_ofs.close();
}
else
{
cout << "You did not enter and approprate extension!" << endl
<< "You entered: " << extension << " options are .flt, .bil and .asc" << endl;
exit(EXIT_FAILURE);
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// This function returns the x and y location of a row and column
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDIndexRaster::get_x_and_y_locations(int row, int col, double& x_loc, double& y_loc)
{
x_loc = XMinimum + float(col)*DataResolution + 0.5*DataResolution;
// Slightly different logic for y because the DEM starts from the top corner
y_loc = YMinimum + float(NRows-row)*DataResolution - 0.5*DataResolution;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// This function returns list_unique_values vector
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
vector<int> LSDIndexRaster::get_list_of_values()
{
detect_unique_values();
return list_unique_values;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// This function returns the x and y location of a row and column
// Same as above but with floats
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDIndexRaster::get_x_and_y_locations(int row, int col, float& x_loc, float& y_loc)
{
x_loc = XMinimum + float(col)*DataResolution + 0.5*DataResolution;
// Slightly different logic for y because the DEM starts from the top corner
y_loc = YMinimum + float(NRows-row)*DataResolution - 0.5*DataResolution;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// Function to convert a node position with a row and column to a lat
// and long coordinate
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDIndexRaster::get_lat_and_long_locations(int row, int col, double& lat,
double& longitude, LSDCoordinateConverterLLandUTM Converter)
{
// get the x and y locations of the node
double x_loc,y_loc;
get_x_and_y_locations(row, col, x_loc, y_loc);
// get the UTM zone of the node
int UTM_zone;
bool is_North;
get_UTM_information(UTM_zone, is_North);
//cout << endl << endl << "Line 1034, UTM zone is: " << UTM_zone << endl;
if(UTM_zone == NoDataValue)
{
lat = NoDataValue;
longitude = NoDataValue;
}
else
{
// set the default ellipsoid to WGS84
int eId = 22;
double xld = double(x_loc);
double yld = double(y_loc);
// use the converter to convert to lat and long