C++实现D8算法,提取流向和河道点
#include <fstream>
#include <iostream>
#include <string>
#include<vector>
#include<sstream>
#include<ctime>
#include"math.h"
using namespace std;
#define DEM_SIZE 100
void main()
{
const char *fileName = "F:/yym/zunhua/DEM/dem.txt";
ifstream ifs;
ifs.open(fileName,ios::in);
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
return;
}
string buf;
int src[DEM_SIZE][DEM_SIZE] = {0};
int count = 0;
while (getline(ifs,buf))
{
stringstream ss;
ss << buf;
int tmp;
int num = 0;
while (ss >> tmp)
{
src[count][num] = tmp;
num++;
if (num == DEM_SIZE)
break;
if (ss.peek() == ',' || ss.peek() == ' ')
ss.ignore();
}
count++;
}
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
cout << src[i][j] << " ";
}
cout << endl;
}
ifs.close();
system("pause");
system("cls");
int Vector[DEM_SIZE][DEM_SIZE] = { 0 }, Result[DEM_SIZE][DEM_SIZE] = { 0 };
int row = DEM_SIZE, col = DEM_SIZE;
double Sqrt2;
double S = 0, N = 0, E = 0, SE = 0, NE = 0, NW = 0, W = 0, SW = 0;
Sqrt2 = sqrt(2);
time_t begin, end;
begin = clock();
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
S = (i != (row - 1)) ? (src[i][j] - src[i + 1][j]) : -1;
SE = (i != (row - 1) && j != (col - 1)) ? (src[i][j] - src[i + 1][j + 1]) / sqrt(2) : -1;
N = (i != 0) ? (src[i][j] - src[i - 1][j]) : -1;
E = (j != (col - 1)) ? (src[i][j] - src[i][j + 1]) : -1;
NE = (i != 0 && j != (col - 1)) ? (src[i][j] - src[i - 1][j + 1]) / sqrt(2) : -1;
NW = (i != 0 && j != 0) ? (src[i][j] - src[i - 1][j - 1]) / sqrt(2) : -1;
W = (j != 0) ? (src[i][j] - src[i][j - 1]) : -1;
SW = (i != (row - 1) && j != 0) ? (src[i][j] - src[i + 1][j - 1]) / sqrt(2) : -1;
double M = 0;
M = (M > S) ? M : S;
M = (M > SE) ? M : SE;
M = (M > N) ? M : N;
M = (M > E) ? M : E;
M = (M > NE) ? M : NE;
M = (M > NW) ? M : NW;
M = (M > W) ? M : W;
M = (M > SW) ? M : SW;
if (M > 0)
{
if (M == S)
{
Vector[i][j] = 4;
}
else if (M == SE)
{
Vector[i][j] = 2;
}
else if (M == N)
{
Vector[i][j] = 64;
}
else if (M == E)
{
Vector[i][j] = 1;
}
else if (M == NE)
{
Vector[i][j] = 128;
}
else if (M == NW)
{
Vector[i][j] = 32;
}
else if (M == W)
{
Vector[i][j] = 16;
}
else if (M == SW)
{
Vector[i][j] = 8;
}
else
{
Vector[i][j] = 0;
}
}
}
}
int i1 = 0, j1 = 0;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
i1 = i;
j1 = j;
while (1)
{
int x = Vector[i1][j1];
if (Vector[i1][j1] == 0 || i1 >= row || j1 >= col)
break;
switch (x)
{
Vector[i1][j1] |= 0x100000;
case 1:
{
Result[i1][j1 + 1]++;
j1 = j1 + 1;
break;
}
case 2:
{
Result[i1 + 1][j1 + 1]++;
i1 = i1 + 1;
j1 = j1 + 1;
break;
}
case 4:
{
Result[i1 + 1][j1]++;
i1 = i1 + 1;
break;
}
case 8:
{
Result[i1 + 1][j1 - 1]++;
i1 = i1 + 1;
j1 = j1 - 1; break;
}
case 16:
{
Result[i1][j1 - 1]++;
j1 = j1 - 1; break;
}
case 32:
{
Result[i1 - 1][j1 - 1]++;
i1 = i1 - 1;
j1 = j1 - 1; break;
}
case 64:
{
Result[i1 - 1][j1]++;
i1 = i1 - 1; break;
}
case 128:
{
Result[i1 - 1][j1 + 1]++;
i1 = i1 - 1;
j1 = j1 + 1; break;
}
default:
break;
}
if (Vector[i1][j1] == 0 || i1 >= row || j1 >= col)
break;
}
}
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
cout << Result[i][j] << " ";
cout << endl;
}
ofstream ofs;
ofs.open("F:/yym/zunhua/DEM/direction.txt",ios::out);
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
ofs<<Vector[i][j]<<" " ;
}
ofs << endl;
}
ofs.close();
ofstream ofs02;
ofs02.open("F:/yym/zunhua/DEM/riverpoint.txt", ios::out);
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
ofs02 << Result[i][j] << " ";
}
ofs02 << endl;
}
ofs02.close();
end = clock();
cout << "runtime: " << double(end - begin) / CLOCKS_PER_SEC << endl;
getchar();
system("pause");
}