-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlowDirectionUpdater.java
More file actions
73 lines (61 loc) · 2.71 KB
/
Copy pathFlowDirectionUpdater.java
File metadata and controls
73 lines (61 loc) · 2.71 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package heyingzhe;
import java.util.*;
public class FlowDirectionUpdater {
// 定义流向编码:右、右下、下、左下、左、左上、上、右上
private static final int[] FLOW_DIRECTIONS = {1, 2, 4, 8, 16, 32, 64, 128};
// 自定义点类来存储坐标和海拔值
static class Point {
int x, y, elevation;
Point(int x, int y, int elevation) {
this.x = x;
this.y = y;
this.elevation = elevation;
}
}
// 主方法:更新流向
public static int[][] updateFlowDirection(int[][] dem, int[][] initialFlowDirection) {
int rows = dem.length;
int cols = dem[0].length;
int[][] resultFlowDirection = new int[rows][cols];
// 初始化结果流向数组,先复制初步流向数组,并将 -9999 的点在结果数组中赋值为 0
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (dem[i][j] == -9999) {
resultFlowDirection[i][j] = 0; // 缺失数据点流向为0
} else {
resultFlowDirection[i][j] = initialFlowDirection[i][j]; // 复制初步流向
}
}
}
// 创建一个点的列表,存储所有非 -9999 的有效点
List<Point> points = new ArrayList<>();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (dem[i][j] != -9999) {
points.add(new Point(i, j, dem[i][j]));
}
}
}
// 按照海拔从低到高排序
points.sort(Comparator.comparingInt(p -> p.elevation));
// 遍历排序后的点,从最低点开始
for (Point p : points) {
int x = p.x;
int y = p.y;
// 如果当前点在初步流向数组中的流向为0
if (initialFlowDirection[x][y] == 0) {
// 获取当前点周围8个邻域
for (int k = 0; k < 8; k++) {
int nx = x + (k == 1 || k == 2 || k == 3 ? 1 : (k == 5 || k == 6 || k == 7 ? -1 : 0));
int ny = y + (k == 3 || k == 4 || k == 5 ? 1 : (k == 0 || k == 1 || k == 7 ? -1 : 0));
// 检查邻域在有效范围内,且流向为0的点
if (nx >= 0 && nx < rows && ny >= 0 && ny < cols && resultFlowDirection[nx][ny] == 0) {
// 将邻域的流向指向当前点
resultFlowDirection[nx][ny] = FLOW_DIRECTIONS[k];
}
}
}
}
return resultFlowDirection;
}
}