
简单说明:用DEM根据D8算法选择高程最小的格点为下游方向,用Strahler Order的思路编码每一个格点的序号值,0代表没有下游流向
该代码无法处理有相互流向的格点,比如左边格点流向为5,右边格点流向为4;
当然,只输入DEM没有问题,如果要用下方的函数去处理别的Flow Direction数据,需要注意。
附测试DEM
链接:https://pan.baidu.com/s/1V8K69Rw50xcQ-kEx8g5_gw
提取码:t9hr
如有其他错误,请告知改正~
#include
#include
#include
#include
using namespace std;
void Strahler_Order(int **FDR, int row, int col, int **order);
int main()
{
string inDEM_path = "./DEM.txt";
ifstream DEM_file(inDEM_path.c_str());
if (!DEM_file)
{
cout << "cannot open DEM file : " << inDEM_path << endl;
exit(0);
}
/*string FDR_path = "E:\arcgis\ChangSha\Strahler\FDR.txt";
ifstream FDR_file(FDR_path.c_str());
if (!FDR_file)
{
cout << "cannot open infile : " << FDR_path << endl;
exit(0);
}*/
string outfile_path = "./Order.txt";
ofstream outfile(outfile_path.c_str());
if (!outfile)
{
cout << "cannot open outfile : " << outfile_path << endl;
exit(0);
}
int row = 240;
int col = 295;
int i, j;
int **order = new int*[row];
for (i = 0; i < row; i++)
{
order[i] = new int[col];
}
int **DEM = new int*[row];
for (i = 0; i < row; i++)
DEM[i] = new int[col];
int **FDR = new int*[row];
for (i = 0; i < row; i++)
FDR[i] = new int[col];
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
DEM_file >> DEM[i][j];
//FDR_file >> FDR[i][j];
}
}
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (DEM[i][j] == -9999)
{
FDR[i][j] = -9999;
continue;
}
if (i == 0 || j == 0 || i == row - 1 || j == col - 1)
{
FDR[i][j] = 0;
continue;
}
int ii, jj;
int min_abs_h = DEM[i][j]; //initialize minimum
int min_abs_index_row = i;
int min_abs_index_col = j;
for (ii = i - 1; ii < i + 2; ii++)
{
for (jj = j - 1; jj < j + 2; jj++)
{
if (DEM[ii][jj] == -9999)
continue;
if (DEM[ii][jj] < min_abs_h)
{
min_abs_h = DEM[ii][jj];
min_abs_index_row = ii;
min_abs_index_col = jj;
}
}
}
if (min_abs_index_row == i && min_abs_index_col == j)
{
FDR[i][j] = 0;
}
else
{
if (min_abs_index_row == i - 1 && min_abs_index_col == j - 1)
{
FDR[i][j] = 1;
}
else if (min_abs_index_row == i - 1 && min_abs_index_col == j)
{
FDR[i][j] = 2;
}
else if (min_abs_index_row == i - 1 && min_abs_index_col == j+1)
{
FDR[i][j] = 3;
}
else if (min_abs_index_row == i && min_abs_index_col == j-1)
{
FDR[i][j] = 4;
}
else if (min_abs_index_row == i && min_abs_index_col == j+1)
{
FDR[i][j] = 5;
}
else if (min_abs_index_row == i + 1 && min_abs_index_col == j-1)
{
FDR[i][j] = 6;
}
else if (min_abs_index_row == i + 1 && min_abs_index_col == j)
{
FDR[i][j] = 7;
}
else if (min_abs_index_row == i + 1 && min_abs_index_col == j +1)
{
FDR[i][j] = 8;
}
}
}
}
Strahler_Order(FDR, row, col, order);
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
outfile << order[i][j] << " ";
}
outfile << endl;
}
}
void Strahler_Order(int **FDR, int row, int col, int **order)
{
int i, j;
int **up_num = new int*[row];
for (i = 0; i < row; i++)
up_num[i] = new int[col];
int **num_order = new int*[row];
for (i = 0; i < row; i++)
num_order[i] = new int[col];
int **max_order = new int*[row];
for (i = 0; i < row; i++)
max_order[i] = new int[col];
int **max_order_num = new int*[row];
for (i = 0; i < row; i++)
max_order_num[i] = new int[col];
int to_row, to_col;
int **flag = new int*[row];
for (i = 0; i < row; i++)
flag[i] = new int[col];
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
max_order[i][j] = -1;
order[i][j] = -1;
up_num[i][j] = 0;
flag[i][j] = -1;
num_order[i][j] = 0;
//max_order_num[i][j] = 0;
}
}
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (FDR[i][j] == -9999)
continue;
if (FDR[i][j] == 1)
{
to_row = i - 1;
to_col = j - 1;
}
else if (FDR[i][j] == 2)
{
to_row = i - 1;
to_col = j;
}
else if (FDR[i][j] == 3)
{
to_row = i - 1;
to_col = j + 1;
}
else if (FDR[i][j] == 4)
{
to_row = i;
to_col = j - 1;
}
else if (FDR[i][j] == 5)
{
to_row = i;
to_col = j + 1;
}
else if (FDR[i][j] == 6)
{
to_row = i + 1;
to_col = j - 1;
}
else if (FDR[i][j] == 7)
{
to_row = i + 1;
to_col = j;
}
else if (FDR[i][j] == 8)
{
to_row = i + 1;
to_col = j + 1;
}
else if (FDR[i][j] == 0)
{
continue;
}
if (to_row<0 || to_col<0 || to_row>row - 1 || to_col>col - 1)
continue;
up_num[to_row][to_col]++;
}
}
/*for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
cout << FDR[i][j] << " ";
}
cout << endl;
}*/
//system("pause");
int order_now = 1;
int flag_continue;
int time = 0;
while (true)
{
time++;
//cout << " now run time :" << time << endl;
flag_continue = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
max_order_num[i][j] = 0;
}
}
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
int ii, jj;
ii = i; jj = j;
if (FDR[ii][jj] == -9999)
continue;
if (flag[ii][jj] == 1)
continue;
else
flag_continue = 1;;
if (up_num[ii][jj] == 0)
{
order[ii][jj] = 1;
flag[ii][jj] = 1;
}
if (order[ii][jj] == order_now)
{
while (true)
{
if (FDR[ii][jj] == 1)
{
to_row = ii - 1;
to_col = jj - 1;
}
else if (FDR[ii][jj] == 2)
{
to_row = ii - 1;
to_col = jj;
}
else if (FDR[ii][jj] == 3)
{
to_row = ii - 1;
to_col = jj + 1;
}
else if (FDR[ii][jj] == 4)
{
to_row = ii;
to_col = jj - 1;
}
else if (FDR[ii][jj] == 5)
{
to_row = ii;
to_col = jj + 1;
}
else if (FDR[ii][jj] == 6)
{
to_row = ii + 1;
to_col = jj - 1;
}
else if (FDR[ii][jj] == 7)
{
to_row = ii + 1;
to_col = jj;
}
else if (FDR[ii][jj] == 8)
{
to_row = ii + 1;
to_col = jj + 1;
}
else if (FDR[ii][jj] == 0)
{
// cout << "here 0 " << endl;
// system("pause");
order[ii][jj] = 0;
flag[ii][jj] = 1;
break;
}
if (to_row<0 || to_col<0 || to_row>row - 1 || to_col>col - 1)
{
// cout << "break" << endl;
break;
}
if (up_num[to_row][to_col] == 1)
{
order[to_row][to_col] = order[ii][jj];
flag[to_row][to_col] = 1;
ii = to_row;
jj = to_col;
}
else
{
num_order[to_row][to_col]++;
if (order[ii][jj] == max_order[to_row][to_col])
{
max_order_num[to_row][to_col]++;
}
else if (order[ii][jj] > max_order[to_row][to_col])
{
max_order_num[to_row][to_col] = 1;
max_order[to_row][to_col] = order[ii][jj];
}
if (num_order[to_row][to_col] == up_num[to_row][to_col])
{
if (max_order_num[to_row][to_col] > 1)
{
order[to_row][to_col] = max_order[to_row][to_col] +1; // order_now已经加过1了
flag[to_row][to_col] = 1;
}
else
{
order[to_row][to_col] = max_order[to_row][to_col];
flag[to_row][to_col] = 1;
}
ii = to_row;
jj = to_col;
}
else
break;
}
}
}
}
}
if (flag_continue == 0)
break;
order_now++;
}
// Free Memory
/***************************************************/
for (i = 0; i < row; i++)
delete[]up_num[i];
delete[]up_num;
for (i = 0; i < row; i++)
delete[]num_order[i];
delete[]num_order;
for (i = 0; i < row; i++)
delete[]max_order[i];
delete[]max_order;
for (i = 0; i < row; i++)
delete[]max_order_num[i];
delete[]max_order_num;
for (i = 0; i < row; i++)
delete[]flag[i];
delete[]flag;
/***************************************************/
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)