Simplification / optimization of GPS track

后端 未结 10 2092
[愿得一人]
[愿得一人] 2020-12-13 11:38

I\'ve got a GPS track produced by gpxlogger(1) (supplied as a client for gpsd). GPS receiver updates its coordinates every 1 second, gpxlogger\'s logic is very

10条回答
  •  爱一瞬间的悲伤
    2020-12-13 12:00

    One really simple method is to repeatedly remove the point that creates the largest angle (in the range of 0° to 180° where 180° means it's on a straight line between its neighbors) between its neighbors until you have few enough points. That will start off removing all points that are perfectly in line with their neighbors and will go from there.

    You can do that in Ο(n log(n)) by making a list of each index and its angle, sorting that list in descending order of angle, keeping how many you need from the front of the list, sorting that shorter list in descending order of index, and removing the indexes from the list of points.

    def simplify_points(points, how_many_points_to_remove)
      angle_map = Array.new
      (2..points.length - 1).each { |next_index|
        removal_list.add([next_index - 1, angle_between(points[next_index - 2], points[next_index - 1], points[next_index])])
      }
      removal_list = removal_list.sort_by { |index, angle| angle }.reverse
      removal_list = removal_list.first(how_many_points_to_remove)
      removal_list = removal_list.sort_by { |index, angle| index }.reverse
      removal_list.each { |index| points.delete_at(index) }
      return points
    end
    

提交回复
热议问题