又是地图,用射线法(点在多边形内部)计算地图点阵,当边界点多起来的时候,计算就很难了。
高德地图给出的基础中国经纬度边界有8万多个点,运行起来基本瘫痪。所以希望能够在尽量保持形状的基础上减少点的个数。下面是最常用的 道格拉斯-普克算法

道格拉斯-普克(Douglas-Peuker)算法

Douglas-Peuker算法(DP算法)过程如下:
1、连接曲线首尾两点A、B;
2、依次计算曲线上所有点到A、B两点所在曲线的距离;
3、计算最大距离D,如果D小于阈值threshold,则去掉曲线上出A、B外的所有点;如果D大于阈值threshold,则把曲线以最大距离分割成两段;
4、对所有曲线分段重复1-3步骤,知道所有D均小于阈值。即完成抽稀。

这种算法的抽稀精度与阈值有很大关系,阈值越大,简化程度越大,点减少的越多;反之简化程度越低,点保留的越多,形状也越趋于原曲线。下面Python代码是抄来的:

  1. # -*- coding: utf-8 -*-
  2. """
  3. -------------------------------------------------
  4. File Name DouglasPeuker
  5. Description : -
  6. Author : J_hao
  7. date 2017/8/16
  8. -------------------------------------------------
  9. Change Activity:
  10. 2017/8/16: -
  11. -------------------------------------------------
  12. """
  13. from __future__ import division
  14. from math import sqrt, pow
  15. __author__ = 'J_hao'
  16. THRESHOLD = 0.0001 #
  17. def point2LineDistance(point_a, point_b, point_c):
  18. """
  19. ab c
  20. :param point_a:
  21. :param point_b:
  22. :param point_c:
  23. :return:
  24. """
  25. # b c
  26. if point_b[0] == point_c[0]:
  27. return 9999999
  28. slope = (point_b[1] - point_c[1]) / (point_b[0] - point_c[0])
  29. intercept = point_b[1] - slope * point_b[0]
  30. # ab c
  31. distance = abs(slope * point_a[0] - point_a[1] + intercept) / sqrt(1 + pow(slope, 2))
  32. return distance
  33. class DouglasPeuker(object):
  34. def __init__(self):
  35. self.threshold = THRESHOLD
  36. self.qualify_list = list()
  37. self.disqualify_list = list()
  38. def diluting(self, point_list):
  39. """
  40. :param point_list:
  41. :return:
  42. """
  43. if len(point_list) < 3:
  44. self.qualify_list.extend(point_list[::-1])
  45. else:
  46. #
  47. max_distance_index, max_distance = 0, 0
  48. for index, point in enumerate(point_list):
  49. if index in [0, len(point_list) - 1]:
  50. continue
  51. distance = point2LineDistance(point, point_list[0], point_list[-1])
  52. if distance > max_distance:
  53. max_distance_index = index
  54. max_distance = distance
  55. #
  56. if max_distance < self.threshold:
  57. self.qualify_list.append(point_list[-1])
  58. self.qualify_list.append(point_list[0])
  59. else:
  60. #
  61. sequence_a = point_list[:max_distance_index]
  62. sequence_b = point_list[max_distance_index:]
  63. for sequence in [sequence_a, sequence_b]:
  64. if len(sequence) < 3 and sequence == sequence_b:
  65. self.qualify_list.extend(sequence[::-1])
  66. else:
  67. self.disqualify_list.append(sequence)
  68. def main(self, point_list):
  69. self.diluting(point_list)
  70. while len(self.disqualify_list) > 0:
  71. self.diluting(self.disqualify_list.pop())
  72. print self.qualify_list
  73. print len(self.qualify_list)
  74. if __name__ == '__main__':
  75. d = DouglasPeuker()
  76. d.main([[104.066228, 30.644527], [104.066279, 30.643528]])

垂距限值法

垂距限值法其实和DP算法原理一样,但是垂距限值不是从整体角度考虑,而是依次扫描每一个点,检查是否符合要求。算法过程如下:
1、以第二个点开始,计算第二个点到前一个点和后一个点所在直线的距离d;
2、如果d大于阈值,则保留第二个点,计算第三个点到第二个点和第四个点所在直线的距离d;若d小于阈值则舍弃第二个点,计算第三个点到第一个点和第四个点所在直线的距离d;
3、依次类推,直线曲线上倒数第二个点。

  1. # -*- coding: utf-8 -*-
  2. """
  3. -------------------------------------------------
  4. File Name LimitVerticalDistance
  5. Description :
  6. Author : J_hao
  7. date 2017/8/17
  8. -------------------------------------------------
  9. Change Activity:
  10. 2017/8/17:
  11. -------------------------------------------------
  12. """
  13. from __future__ import division
  14. from math import sqrt, pow
  15. __author__ = 'J_hao'
  16. THRESHOLD = 0.0001 #
  17. def point2LineDistance(point_a, point_b, point_c):
  18. """
  19. ab c
  20. :param point_a:
  21. :param point_b:
  22. :param point_c:
  23. :return:
  24. """
  25. # b c
  26. if point_b[0] == point_c[0]:
  27. return 9999999
  28. slope = (point_b[1] - point_c[1]) / (point_b[0] - point_c[0])
  29. intercept = point_b[1] - slope * point_b[0]
  30. # ab c
  31. distance = abs(slope * point_a[0] - point_a[1] + intercept) / sqrt(1 + pow(slope, 2))
  32. return distance
  33. class LimitVerticalDistance(object):
  34. def __init__(self):
  35. self.threshold = THRESHOLD
  36. self.qualify_list = list()
  37. def diluting(self, point_list):
  38. """
  39. :param point_list:
  40. :return:
  41. """
  42. self.qualify_list.append(point_list[0])
  43. check_index = 1
  44. while check_index < len(point_list) - 1:
  45. distance = point2LineDistance(point_list[check_index], self.qualify_list[-1], point_list[check_index + 1])
  46. if distance < self.threshold:
  47. check_index += 1
  48. else:
  49. self.qualify_list.append(point_list[check_index])
  50. check_index += 1
  51. return self.qualify_list
  52. if __name__ == '__main__':
  53. l = LimitVerticalDistance()
  54. diluting = l.diluting([[104.066228, 30.644527], [104.066279,
  55. 30.643528]])

其实DP算法和垂距限值法原理一样,DP算法是从整体上考虑一条完整的曲线,实现时较垂距限值法复杂,但垂距限值法可能会在某些情况下导致局部最优。另外在实际使用中发现采用点到另外两点所在直线距离的方法来判断偏离,在曲线弧度比较大的情况下比较准确。如果在曲线弧度比较小,弯曲程度不明显时,这种方法抽稀效果不是很理想,建议使用三点所围成的三角形面积作为判断标准。