Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def split_laps_into_tracks_if_needed(tracks)
- #let's try and be smart about splitting laps into their own tracks
- #automatically, based on two conditions: time between laps is > 24 hours
- #or one lap has location data and another lap doesn't.
- new_tracks = []
- tracks.each do |trk|
- if trk[:segments].length == 1
- new_tracks << trk
- next
- end
- #if two adjacent tracks have location/not location, split off first
- laps = []
- prev = nil
- trk[:segments].each do |seg|
- if prev.nil?
- prev = seg
- next
- end
- prev_first = trk[:track][prev[:start_index]]
- prev_has_loc = prev_first[:x] and prev_first[:y]
- first = trk[:track][seg[:start_index]]
- cur_has_loc = first[:x] and first[:y]
- if (prev_has_loc and !cur_has_loc) or (!prev_has_loc and cur_has_loc)
- #split previous lap into its own track
- points = trk[:track].slice!(prev[:start_index]..prev[:end_index])
- new_tracks << {
- :segments => [{:start_index => 0, :end_index => points.length-1}],
- :pois => [],
- :course_points => [],
- :track => points
- }
- end
- prev = seg
- end
- new_tracks << trk
- end
- return new_tracks
- end
Add Comment
Please, Sign In to add comment