Editing GPX Track Files from OsmAnd in a Text Editor (Shallow Thoughts)

Akkana's Musings on Open Source Computing and Technology, Science, and Nature.

Fri, 07 Jan 2022

Editing GPX Track Files from OsmAnd in a Text Editor

I record track files in OsmAnd for most of my hikes. But about a quarter of the time, when I get back to the car, I forget to turn tracking off. So I end up with a track file that shows the hike plus at least part of the car trip afterward. Not very useful for purposes like calculating mileage.

My PyTopo can do simple track edits, like splitting the track into two or deleting from some point to the end. But most of the time it's easier just to edit the GPX file with a text editor.

Eesh, edit GPX directly?? Sounds like something you oughtn't do, doesn't it? But actually, GPX (a form of XML) is human readable and editable. And this specific case, separating the walking from the car trip in an OsmAnd track file, is particularly easy, because OsmAnd helpfully adds a speed to every point it saves.

These instructions seem long, but really, once you've done it once, you'll realize that it's all very straightforward; explaining the steps is harder than doing them.

0. Make a Backup Copy

Make a copy of your GPX file, in case you mess it up while editing it.

1. Use grep on the Command Line

This step isn't strictly necessary: you can skip to step 2 if you want. But I promise, it makes the job much easier, and once you learn about grep you'll find plenty of other uses for it.

grep is a command-line tool that finds all lines matching a pattern (the name stands for "Generalized Recognition of Patterns"). In this case, you want to see all the lines with speed in them.

I believe Macs come with grep installed by default these days. On Windows, you can use WSL, Windows Subsystem for Linux, or you can use the Windows equivalent, findstr. Most Linux users are probably already familiar with grep.

So open up a terminal window and cd (Change Directory) to your Desktop or wherever you saved your GPX file. (Here's a tutorial on navigating in the terminal for Mac; I couldn't find a similarly simple tutorial for Windows, sorry.)

Your OsmAnd file will have a name like 2022-01-05_09-58_Wed.gpx. Search for the speed lines in it:

grep speed 2022-01-05_09-58_Wed.gpx
You should see a long list of lines like:
... and so on.

Scroll through those lines looking for a place where the speed goes from single digits to double digits. So you might see something like this:

<osmand:speed>8.25</osmand:speed> <osmand:speed>11.25</osmand:speed> <osmand:speed>10.0</osmand:speed> <osmand:speed>10.0</osmand:speed>

See how it suddenly goes from .5 to 4.5 (here highlighted in yellow) to 8 to 11? That's where I got in the car and started driving off. (I don't remember if these numbers are MPH or KPH or what, but it doesn't matter.)

Anyway, make a note of the speed where the car started moving: here, the 4.5 line.

2. Edit the File with a Text Editor

Open your GPX file in a text editor. You need a plain text editor, not a Word Processor like MS Word. Something like TextEdit on Mac or Notepad on Windows (on Linux, any text editor will do -- pico/nano, gedit, vim, emacs etc.)

DON'T PANIC! At first XML will look like gobbledygook, but it's actually pretty easy to understand.

3. Find the Right speed Line

Search forward to find the line with the speed you found in step 1. If you just search for the number, like 4.5, you'll probably get a lot of unrelated hits like <trkpt lat="36.1444452" lon="-106.1911931"> so if you get tired of that, search for a bigger part of the speed line, like osmand:speed>4.5).

If you skipped step 1, you'll have to to skim through the file looking at the osmand:speed entries for each point to find where they go from single to double digits. Start from the end of the file, not the beginning, assuming your hike was longer than the drive home afterward. (OsmAnd saves points at fixed time intervals, like one point every 15 seconds.)

4. Delete From That Point to (Almost) the End

This is the only tricky part. I mentioned earlier that GPX is a form of XML, and XML is extremely picky about things like start and end tags. For instance, here are two points in a track in a GPX file:

      <trkpt lat="36.1255001" lon="-106.2192199">
      <trkpt lat="36.1254935" lon="-106.2191965">

The first line, <trkpt lat="36.1255001" lon="-106.2192199">, begins a track point at a particular latitude and longitude.

Inside that track point is an elevation, <ele>1946.5</ele>: the elevation starts with <ele>, then there's the elevation in meters, then the elevation ends with </ele>.

The same goes for the time, the hdop ("horizontal dilution of precision", which has to do with how much GPS error there is), and extensions, which in this case include speed. Finally, that track point is closed, with </trkpt>, and the next track point begins, <trkpt lat="36.1254935" lon="-106.2191965">

If you accidentally delete part of a track point, so, say, you have a start tag <trkpt> but there's no matching end point </trkpt>, programs like OsmAnd won't be able to read it any more. Remember how I told you at the beginning to make a backup copy of the file? If you skipped that, do it now, before making any changes.

All backed up? Okay, start from the <trkpt that has the speed where your car started moving, and delete everything to the last trkpt before the end. The end of the GPX file will look something like this:

<trkpt lat="36.1505157" lon="-106.1762609"> <ele>1768.3</ele> <time>2022-01-05T20:44:34Z</time> <hdop>6.5</hdop> <extensions> <osmand:speed>1</osmand:speed> </extensions> </trkpt> <trkpt lat="36.1505661" lon="-106.1760612"> <ele>1767.7</ele> <time>2022-01-05T20:44:49Z</time> <hdop>4.8</hdop> <extensions> <osmand:speed>1.1</osmand:speed> </extensions> </trkpt>
</trkseg> </trk> </gpx>

Delete everything up to and including the last </trkpt> (delete the pink section) but not including the </trkseg> following it (keep the green section).

Eyeball the file near the end, especially the indent levels: it should have a </trkpt> indented six spaces, followed by a </trkseg> indented four spaces, </trk> two spaces, etc. If you see any place where the spacing jumps by an uneven amount, you might have deleted one line too much or too little, in which case, undo and try again.

Save the file, but keep your text editor running for the moment.

5. Try Your Revised File

Check your work. Take the file you've just written and try it in some program that understands GPX. If you don't have any mapping programs installed on your computer, copy it to the net.osmand.plus/files/tracks/ folder on the phone or tablet where you run OsmAnd, and see if it works there. You should see only your hike, without the car trip afterward.

If you get an error, most likely you deleted one line too few or too many. Since you left your editor window open, you can inspect it for unevenly indented lines, undo and try again.

That's it. It takes a lot longer to explain than to do: in practice, once you know what you're looking for, it only takes a few seconds to trim off the unwanted car section -- much faster than loading it into a program and clicking around deleting trail sections.

Tags: , ,
[ 14:13 Jan 07, 2022    More mapping | permalink to this entry | ]

Comments via Disqus:

blog comments powered by Disqus