-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdem.cpp
127 lines (120 loc) · 2.26 KB
/
dem.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
#include "DEM.h"
#include "utils.h"
bool DEM::Allocate()
{
delete[] pDem;
try
{
pDem=new float[width*height];
setNoData(pDem, width * height, NO_DATA_VALUE);
}
catch(const std::bad_alloc& e)
{
fprintf(stderr, "Failed to create memory for the DEM!\n");
return false;
}
return true;
}
void DEM::freeMem()
{
delete[] pDem;
pDem = NULL;
}
void DEM::initialElementsNodata()
{
setNoData(pDem, width * height, NO_DATA_VALUE);
}
float DEM::asFloat(int row,int col) const
{
return pDem[row*width+col];
}
void DEM::Set_Value(int row,int col, float z)
{
pDem[row*width+col]=z;
}
bool DEM::is_NoData(int row, int col) const
{
if (fabs(pDem[row*width+col]-NO_DATA_VALUE)<0.00001) return true;
return false;
}
void DEM::Assign_NoData()
{
for (int i=0; i<width*height; i++)
pDem[i]=NO_DATA_VALUE;
}
int DEM::Get_NY() const
{
return height;
}
int DEM::Get_NX() const
{
return width;
}
float* DEM::getDEMdata() const
{
return pDem;
}
void DEM::SetHeight(int height)
{
this->height = height;
}
void DEM::SetWidth(int width)
{
this->width = width;
}
void DEM::readDEM(const std::string& filePath)
{
std::ifstream is;
is.open (filePath, std::ios::binary);
is.read((char*)pDem,sizeof(float)*width*height);
is.close();
}
bool DEM::is_InGrid(int row, int col) const
{
if ((row >=0 && row < height) && (col >= 0 && col < width))
return true;
return false;
}
float DEM::getLength(unsigned int dir)
{
if ((dir & 0x1) == 1)
{
return 1.41421f;
}
else return 1.0f;
}
/*
* flow direction
* 32 64 128
* 16 0 1
* 8 4 2
*/
unsigned char DEM::computeDirection(int row, int col, float spill)
{
int iRow, iCol;
float iSpill, max, gradient;
unsigned char steepestSpill;
max = 0.0f;
steepestSpill = 255;
unsigned char lastIndexINGridNoData = 0;
//using D8 method to calculate the flow direction
for (int i = 0; i < 8; i++)
{
iRow = Get_rowTo(i, row);
iCol = Get_colTo(i, col);
if (is_InGrid(iRow, iCol) && !is_NoData(iRow, iCol) && (iSpill = asFloat(iRow, iCol)) < spill)
{
gradient = (spill - iSpill) / getLength(i);
if (max < gradient)
{
max = gradient;
steepestSpill = i;
}
}
if (!is_InGrid(iRow, iCol) || is_NoData(iRow, iCol))
{
lastIndexINGridNoData = i;
}
}
return steepestSpill != 255 ? dir[steepestSpill] : dir[lastIndexINGridNoData];
}