-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlopeCalculation.java
More file actions
71 lines (60 loc) · 2.69 KB
/
Copy pathSlopeCalculation.java
File metadata and controls
71 lines (60 loc) · 2.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
package heyingzhe;
public class SlopeCalculation {
// 定义邻域的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 坡度值二维数组
*/
public double[][] calculateSlope(int[][] dem) {
int rows = dem.length;
int cols = dem[0].length;
double[][] slope = new double[rows][cols];
// 遍历每个点,计算其坡度
for (int i = 1; i < rows - 1; i++) {
for (int j = 1; j < cols - 1; j++) {
// 跳过无效数据 -9999 的点
if (dem[i][j] == -9999) {
slope[i][j] = 0.0; // 可以选择设为0或忽略
continue;
}
double dzx = 0; // x 方向(东西方向)的海拔变化
double dzy = 0; // y 方向(南北方向)的海拔变化
// 计算 x 方向的海拔变化(东西方向)
int validCountX = 0; // 有效邻域数目
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 && dem[nx][ny] != -9999) {
// 如果是有效点,计算方向上的差值
dzx += dem[nx][ny] - dem[i][j];
validCountX++;
}
}
// 计算 y 方向的海拔变化(南北方向)
int validCountY = 0; // 有效邻域数目
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 && dem[nx][ny] != -9999) {
// 如果是有效点,计算方向上的差值
dzy += dem[nx][ny] - dem[i][j];
validCountY++;
}
}
// 使用勾股定理计算坡度
if (validCountX > 0 && validCountY > 0) {
slope[i][j] = Math.sqrt(dzx * dzx + dzy * dzy);
} else {
slope[i][j] = 0.0; // 如果没有有效的邻域,坡度为 0
}
}
}
return slope;
}
}