-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepressionFilling.java
More file actions
83 lines (69 loc) · 2.79 KB
/
Copy pathDepressionFilling.java
File metadata and controls
83 lines (69 loc) · 2.79 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
package heyingzhe;
public class DepressionFilling {
// 定义邻域的8个方向:右、右下、下、左下、左、左上、上、右上
private static final int[] DX = {1, 1, 0, -1, -1, -1, 0, 1};
private static final int[] DY = {0, 1, 1, 1, 0, -1, -1, -1};
/**
* 填洼算法,返回填洼后的 DEM 数组
*
* @param dem 输入的 DEM 数组,表示地形高度
* @return 填洼后的 DEM 数组
*/
public static int[][] fillDepressions(int[][] dem) {
int rows = dem.length;
int cols = dem[0].length;
int[][] filledDEM = new int[rows][cols];
// 将结果数组初始化为输入的 DEM 数组
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
filledDEM[i][j] = dem[i][j];
}
}
// 遍历整个 DEM 数组,逐个点进行填洼操作
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// 跳过无效数据 -9999 的点
if (dem[i][j] == -9999) continue;
int currentValue = dem[i][j];
int minValue = currentValue;
// 检查周围8个邻域,获取邻域中的最小值
for (int k = 0; k < 8; k++) {
int nx = i + DX[k];
int ny = j + DY[k];
// 确保邻域在有效范围内
if (nx >= 0 && nx < rows && ny >= 0 && ny < cols) {
int neighborValue = dem[nx][ny];
// 只选择邻域中较低的有效值
if (neighborValue < currentValue && neighborValue != -9999) {
minValue = Math.min(minValue, neighborValue);
}
}
}
// 如果当前点是低洼区域且需要填洼,更新它的值
if (minValue < currentValue) {
filledDEM[i][j] = minValue;
}
}
}
return filledDEM;
}
public static void main(String[] args) {
// 示例 DEM 数组
int[][] dem = {
{100, 100, 100, 100, 100},
{100, 50, 40, 50, 100},
{100, 40, 30, 40, 100},
{100, 50, 40, 50, 100},
{100, 100, 100, 100, 100}
};
// 输出填洼后的 DEM 数组
int[][] filledDEM = fillDepressions(dem);
// 打印结果
for (int i = 0; i < filledDEM.length; i++) {
for (int j = 0; j < filledDEM[i].length; j++) {
System.out.print(filledDEM[i][j] + " ");
}
System.out.println();
}
}
}