/mytracks_kml_merger

Quick and dirty code to merge, specifically, KLM files made by the MyTracks Android app

Primary LanguageRuby

MyTracks KML Helper

Takes a bunch of KML files generated by MyTracks and merges them into one.

This is a really janky, hacky script, but it does the job.

Updating the merged file with new tracks from the phone

  1. In My Tracks, in the list of tracks, sync to GDrive.
  2. Download the GDrive mytracks folder.
    1. Upload all new/original track files (so, non-merged, etc) to mytracks_original, to keep a backup
  3. Copy the downloaded mytracks folder to this repo's mytracks
  4. Manipulate any files you may need to manipulate. (see section below)
    • Use the visualizers below, plus this script's CLI arguments to generate the file you actually want.
    • Read the code of merge_all() to understand what kind of things need to happen to a file you touch.
    • If you manually manipulate a track, upload it to mytracks_original with suffix -fixed
    • If you'd originally renamed the track to call attention to it, rename it back to the standard Date format in mytracks_original.
    • Leave the manipulated file in mytracks, with the correct track name (with date in it) so it'll be picked up in order and its date parsed on the merge step.
  5. ruby merge.rb to merge all the files in the mytracks folder into a new one.
    • Maybe include the previous merged, maybe not... You may end up with enormous files...
    • Lots of the graphing tools have a 10 Mb limit, so mind that.
  6. Upload the Merged... generated file to GDrive in mytracks
  7. Move all non-"Merged" tracks from mytracks to mytracks_original

Getting the new merged track onto the phone:

  1. DO NOT go to Sync / Restore and "restore". It seems to re-restore old tracks that are no longer in mytracks somehow
  2. On the list of tracks, delete all of them. This will be exceedingly painful.
  3. Kill the MyTracks app
  4. Open the Drive app
  5. Choose the Merged tracks to copy to the phone and Download.
    • NOTE: In the past, I had trouble with the "downloaded" file, it wouldn't import, MyTracks would just crash.
    • I made a note at the time to hit Copy to, (not Download!).
    • That option no longer seems to be there, though, and importing after Download seems to work now...
  6. Go to list of tracks, menu on the top, Import, and select the files to import in the Downloads folder.
    • Once the track imports, leave it alone!. Don't change it to Cycling, or anythign else, or MyTracks sometimes does weird shit like double the distance.
  7. On "Tracks & Markers", delete all the markers. They can all be selected and deleted at once.

Alternatively, and especially if My Tracks is being stupid:

  • Go to "App info", and clear all Cache and Stored data
  • Import the merged tracks again
  • Go to "No ads" on the menu, so it'll realize i've already paid.
  • Settings:
    • General: Turn off everything
    • Track & Marker:
      • Width of route: 6
      • Auto save a route when stopped: Yes
      • Photos shown: No
      • Max speed should be less than: 60

Manipulating the individual tracks to merge

All of these are commands are run with ruby merge.rb command [options], act on one single file specified with the -f parameter, and generate an output.kml in the root directory with the results.

  • Slice a file: Reduce the route in a file to only points that fall between the two specified timestamps
    • ruby merge.rb slice -f "mytracks/Route from 2020-07-23 11_39.kml" --from "2020-07-23 13:14:47" --to "2020-07-23 14:00:10"
  • Split a file: Split a route into two, with the points before/after the --at parameters. Outputs output_before.kml and output_after.kml.
    • ruby merge.rb split -f "mytracks/Route from 2020-07-23 11_39.kml" --at "2020-07-23 14:00:10"
    • NOTE: If you get error merge.rb:153:in 'split': undefined method '>' for nil:NilClass (NoMethodError), you entered a time that's not in the time range for the route. Either before the start or after the end.
  • Retime a file: "Move" the times in a route, keeping the length/speed the same, so it starts at a new point in time.
    • ruby merge.rb retime -f "mytracks/Route from 2020-07-23 11_39.kml" --start "2020-07-24T10:39:49.537Z"
  • Compress time on a file: Change the length/speed of a route, so it starts at the same time, but ends at a new point in time.
    • ruby merge.rb compress_time -f "mytracks/Route from 2020-07-23 11_39.kml" --end "2020-07-23T17:20:49.537Z"
  • Compact time on a file: eliminates time gaps between rides, so it's all one non-stop continuous ride. Useful to calculate actual average speed and riding time.
    • `ruby merge.rb compact_time -f "mytracks/Route from 2020-07-23 11_39.kml"
    • These files won't import into My Tracks, for some stupid reason. GPS Visualizer has a "Show track statistics" option in "advanced leaflet" which will show total distance and total time.

Utilities