Guest User

Untitled

a guest
Jan 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. def split_laps_into_tracks_if_needed(tracks)
  2. #let's try and be smart about splitting laps into their own tracks
  3. #automatically, based on two conditions: time between laps is > 24 hours
  4. #or one lap has location data and another lap doesn't.
  5. new_tracks = []
  6. tracks.each do |trk|
  7. if trk[:segments].length == 1
  8. new_tracks << trk
  9. next
  10. end
  11.  
  12. #if two adjacent tracks have location/not location, split off first
  13. laps = []
  14. prev = nil
  15. trk[:segments].each do |seg|
  16. if prev.nil?
  17. prev = seg
  18. next
  19. end
  20.  
  21. prev_first = trk[:track][prev[:start_index]]
  22. prev_has_loc = prev_first[:x] and prev_first[:y]
  23. first = trk[:track][seg[:start_index]]
  24. cur_has_loc = first[:x] and first[:y]
  25.  
  26. if (prev_has_loc and !cur_has_loc) or (!prev_has_loc and cur_has_loc)
  27. #split previous lap into its own track
  28. points = trk[:track].slice!(prev[:start_index]..prev[:end_index])
  29. new_tracks << {
  30. :segments => [{:start_index => 0, :end_index => points.length-1}],
  31. :pois => [],
  32. :course_points => [],
  33. :track => points
  34. }
  35. end
  36.  
  37. prev = seg
  38. end
  39. new_tracks << trk
  40. end
  41. return new_tracks
  42. end
Add Comment
Please, Sign In to add comment