道格拉斯压缩算法

  1. import java.util.List;
  2. public interface Track {
  3. /**
  4. * 轨迹压缩
  5. * @param coordinate 原始轨迹 Array<{latitude,longitude,UTC}>
  6. * @return douglasResult 压缩后的轨迹
  7. */
  8. List<double[]> compress(List<double[]> coordinate);
  9. }
  10. import lombok.Data;
  11. import lombok.Setter;
  12. import java.util.ArrayList;
  13. import java.util.Arrays;
  14. import java.util.Comparator;
  15. import java.util.List;
  16. public class Douglas implements Track{
  17. private int dMax;
  18. private int dMaxLimit;
  19. /**
  20. * @param dMax 允许最大距离误差
  21. * @param dMaxLimit 允许最大距离误差上限,超过该上限,认为异常点
  22. */
  23. public Douglas(int dMax,int dMaxLimit){
  24. this.dMax = dMax;
  25. this.dMaxLimit = dMaxLimit;
  26. }
  27. /**
  28. * @param coordinate 原始轨迹 Array<{latitude,longitude}>
  29. * @return douglasResult 压缩后的轨迹
  30. */
  31. @Override
  32. public List<double[]> compress(List<double[]> coordinate) {
  33. //抽稀点数量需要大于2
  34. if (coordinate == null || coordinate.size() <= 2) {
  35. return null;
  36. }
  37. List<double[]> coordinate2 = new ArrayList<>();
  38. for (int i = 0; i < coordinate.size(); i++) {
  39. double[] point = Arrays.copyOf(coordinate.get(i),4) ;
  40. point[3] = i;
  41. coordinate2.add(point);
  42. }
  43. List<double[]> result = new ArrayList<>();
  44. compressLine(coordinate2, result, 0, coordinate2.size() - 1, dMax,dMaxLimit);
  45. result.add(coordinate2.get(0));
  46. result.add(coordinate2.get(coordinate.size() - 1));
  47. result.sort(new Comparator<double[]>() {
  48. @Override
  49. public int compare(double[] u1, double[] u2) {
  50. if (u1[3] > u2[3]) {
  51. return 1;
  52. } else if (u1[3] < u2[3]) {
  53. return -1;
  54. }
  55. // 相等
  56. return 0;
  57. }
  58. });
  59. return result;
  60. }
  61. /**
  62. * 计算点pX到点pA和pB所确定的直线的距离
  63. */
  64. private double distToSegment(double[] start, double[] end, double[] center) {
  65. double a = Math.abs(calculationDistance(start, end));
  66. double b = Math.abs(calculationDistance(start, center));
  67. double c = Math.abs(calculationDistance(end, center));
  68. double p = (a + b + c) / 2.0;
  69. double s = Math.sqrt(Math.abs(p * (p - a) * (p - b) * (p - c)));
  70. return s * 2.0 / a;
  71. }
  72. /**
  73. * 递归方式压缩轨迹
  74. */
  75. private void compressLine(List<double[]> coordinate, List<double[]> result, int start, int end, int dMax, int dMaxLimit) {
  76. if (start < end) {
  77. double maxDist = 0;
  78. int currentIndex = 0;
  79. double[] startPoint = coordinate.get(start);
  80. double[] endPoint = coordinate.get(end);
  81. for (int i = start + 1; i < end; i++) {
  82. double currentDist = distToSegment(startPoint, endPoint, coordinate.get(i));
  83. if (currentDist > maxDist) {
  84. maxDist = currentDist;
  85. currentIndex = i;
  86. }
  87. }
  88. if (maxDist >= dMax && maxDist <= dMaxLimit) {
  89. //将当前点加入到过滤数组中
  90. result.add(coordinate.get(currentIndex));
  91. //将原来的线段以当前点为中心拆成两段,分别进行递归处理
  92. compressLine(coordinate, result, start, currentIndex, dMax,dMaxLimit);
  93. compressLine(coordinate, result, currentIndex, end, dMax,dMaxLimit);
  94. }
  95. }
  96. }
  97. /**
  98. * 计算两点距离
  99. */
  100. private double calculationDistance(double[] point1, double[] point2) {
  101. double lat1 = point1[0];
  102. double lat2 = point2[0];
  103. double lng1 = point1[1];
  104. double lng2 = point2[1];
  105. double radLat1 = lat1 * Math.PI / 180.0;
  106. double radLat2 = lat2 * Math.PI / 180.0;
  107. double a = radLat1 - radLat2;
  108. double b = (lng1 * Math.PI / 180.0) - (lng2 * Math.PI / 180.0);
  109. double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
  110. + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  111. return s * 6370996.81;
  112. }
  113. }