-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlowDirection.java
More file actions
55 lines (44 loc) · 2.03 KB
/
Copy pathFlowDirection.java
File metadata and controls
55 lines (44 loc) · 2.03 KB
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
package heyingzhe;
public class FlowDirection {
private static final int[][] FLOW_DIRECTIONS = {
{0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}, {-1, 1} // 8个方向
};
// 流向计算函数:为每个栅格计算流向
public int[][] calculateFlowDirection(int[][] dem) {
int rows = dem.length;
int cols = dem[0].length;
int[][] flowDirection = new int[rows][cols];
// 计算每个栅格的流向
for (int i = 1; i < rows - 1; i++) {
for (int j = 1; j < cols - 1; j++) {
int current = dem[i][j];
if (current == -9999) continue; // 跳过无效值
double maxSlope = 0;
int bestDirection = 0;
// 遍历8个邻域,计算坡度并选择坡度最大的流向
for (int k = 0; k < 8; k++) {
double slope = 0;
int ni = i + FLOW_DIRECTIONS[k][0];
int nj = j + FLOW_DIRECTIONS[k][1];
if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
int neighbor = dem[ni][nj];
if (neighbor != -9999) {
// 计算坡度:高度差 / 距离(假设每个栅格之间的距离为1)
if (FLOW_DIRECTIONS[k][0]==0 || FLOW_DIRECTIONS[k][1]==0) {
slope = (current - neighbor);
} else {
slope = (current - neighbor) / 1.414;
}
if (slope > maxSlope) {
maxSlope = slope;
bestDirection = (int) Math.pow(2, k); // 设置对应的流向编码
}
}
}
}
flowDirection[i][j] = bestDirection;
}
}
return flowDirection;
}
}