-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlowAccumulation.java
More file actions
134 lines (113 loc) · 4.69 KB
/
Copy pathFlowAccumulation.java
File metadata and controls
134 lines (113 loc) · 4.69 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
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
128
129
130
131
132
133
134
package heyingzhe;
import java.util.*;
public class FlowAccumulation {
private int[][] dem; // 高程数组(整数类型)
private double[][] rainfall; // 降水量数组(双精度浮动数组)
private int[][] flowDirection; // 流向数组
private double[][] flowAccumulation; // 累积流量数组
// 构造函数,传入 DEM(int 类型)、降水量和流向数组
public FlowAccumulation(int[][] dem, double[][] rainfall, int[][] flowDirection) {
this.dem = dem;
this.rainfall = rainfall;
this.flowDirection = flowDirection;
this.flowAccumulation = new double[dem.length][dem[0].length]; // 初始化累积流量数组
}
// 计算累积流量的方法
public double[][] calculateAccumulatedFlow() {
int rows = dem.length;
int cols = dem[0].length;
// Step 1: 初始化累积流量为降水量
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
flowAccumulation[i][j] = rainfall[i][j]; // 初始流量为降水量
}
}
// Step 2: 对 DEM 数组按海拔从高到低排序
List<Cell> cells = new ArrayList<>();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (dem[i][j] != -9999) { // 排除无效区域(海平面或无数据区域)
cells.add(new Cell(i, j, dem[i][j]));
}
}
}
// 按海拔从高到低排序
cells.sort((a, b) -> Integer.compare(b.height, a.height));
// Step 3: 从高海拔到低海拔进行累积流量计算
for (Cell cell : cells) {
int i = cell.row;
int j = cell.col;
// 获取当前单元格的流向
int[] direction = getFlowDirection(i, j);
if (direction != null) {
int iNext = direction[0];
int jNext = direction[1];
// 如果流向的单元格有效且有流向,累加流量
if (iNext >= 0 && iNext < rows && jNext >= 0 && jNext < cols && dem[iNext][jNext] != -9999) {
flowAccumulation[iNext][jNext] += flowAccumulation[i][j];
}
}
}
// Step 4: 返回最终的累积流量数组
return flowAccumulation;
}
// 获取单元格的流向
private int[] getFlowDirection(int i, int j) {
int directionCode = flowDirection[i][j];
// 根据流向编码返回流向的相邻单元格坐标
switch (directionCode) {
case 1: return new int[]{i, j+1}; // 右
case 2: return new int[]{i+1, j+1}; // 右下
case 4: return new int[]{i+1, j}; // 下
case 8: return new int[]{i+1, j-1}; // 左下
case 16: return new int[]{i, j-1}; // 左
case 32: return new int[]{i-1, j-1}; // 左上
case 64: return new int[]{i-1, j}; // 上
case 128: return new int[]{i-1, j+1}; // 右上
default: return null; // 无流向
}
}
// 内部类,表示每个网格单元格(包含行、列和海拔)
class Cell {
int row;
int col;
int height; // 高程为整数类型
public Cell(int row, int col, int height) {
this.row = row;
this.col = col;
this.height = height;
}
}
// 示例调用方法
public static void main(String[] args) {
// 示例 DEM 数组(整数类型)
int[][] dem = {
{100, 101, 102},
{99, 98, 97},
{96, 95, 94}
};
// 示例降水量数组
double[][] rainfall = {
{10.0, 15.0, 20.0},
{5.0, 10.0, 15.0},
{0.0, 5.0, 10.0}
};
// 示例流向数组
int[][] flowDirection = {
{1, 1, 1},
{4, 8, 16},
{32, 64, 128}
};
// 创建 FlowAccumulation 对象
FlowAccumulation flowAccumulationCalculator = new FlowAccumulation(dem, rainfall, flowDirection);
// 计算累积流量
double[][] accumulatedFlow = flowAccumulationCalculator.calculateAccumulatedFlow();
// 输出累积流量结果
for (int i = 0; i < accumulatedFlow.length; i++) {
for (int j = 0; j < accumulatedFlow[i].length; j++) {
System.out.print(accumulatedFlow[i][j] + " ");
}
System.out.println();
}
}
}