-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIDWInterpolation.java
More file actions
82 lines (72 loc) · 2.79 KB
/
Copy pathIDWInterpolation.java
File metadata and controls
82 lines (72 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
package heyingzhe;
import java.util.ArrayList;
import java.util.List;
// 该类是反距离权重插值,并在线程运算中使用
public class IDWInterpolation {
public static boolean areAllZeros(double[][] data) {
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
if (data[i][j] != 0) {
return false;
}
}
}
return true;
}
/**
* 进行反距离权重插值计算(优化版)
*
* @param data 输入的二维数组,包含已知值和待插值的点(值为0)
* @param power 距离的幂次,通常为2
* @return 插值后的二维数组
*/
public static double[][] idwInterpolation(double[][] data, double power) {
// 如果没有有效站点数据值,就会返回空数组
if (areAllZeros(data)) {
return data;
}
int rows = data.length;
int cols = data[0].length;
double[][] result = new double[rows][cols];
// 提取所有已知点,减少重复计算
List<double[]> knownPoints = new ArrayList<>();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (data[i][j] != 0) {
knownPoints.add(new double[]{i, j, data[i][j]});
}
}
}
// 遍历每个点
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (data[i][j] != 0) {
// 已知点直接保留
result[i][j] = data[i][j];
} else {
// 插值点
double numerator = 0.0; // 分子
double denominator = 0.0; // 分母
// 使用所有已知点计算插值
for (double[] point : knownPoints) {
int x = (int) point[0];
int y = (int) point[1];
double value = point[2];
double distance = Math.sqrt(Math.pow(i - x, 2) + Math.pow(j - y, 2));
if (distance == 0) {
// 如果距离为0,直接赋值
numerator = value;
denominator = 1;
break;
}
double weight = 1.0 / Math.pow(distance, power);
numerator += weight * value;
denominator += weight;
}
result[i][j] = numerator / denominator;
}
}
}
return result;
}
}