Author Topic: Clean up GPX containing only Waypoints  (Read 1728 times)

Feanor

  • It's mostly downhill from here.
Clean up GPX containing only Waypoints
« on: 30 July, 2021, 12:39:26 pm »
Most GPX cleanup tools seem to designed to work with tracks and trackpoints.

I have a GPX exported from Mapsource which contains only Waypoints.
These have been exported with timedate, elevation, and proprietary garmin extensions relating to display options.

I'd like to strip everything except lat/long.
I could do it by hand, but it would be a lot of work.

Can anyone suggest a tool that would do this?

Re: Clean up GPX containing only Waypoints
« Reply #1 on: 30 July, 2021, 12:43:46 pm »
Open in a text editor and search/replace wpt with trkpt?

(you might also need to add a trk parent tag)

Feanor

  • It's mostly downhill from here.
Re: Clean up GPX containing only Waypoints
« Reply #2 on: 30 July, 2021, 12:48:56 pm »
But I dont want trackpoints!
I want them to remain as waypoints.

I'd also prefer to avoid too much notepad++ fiddling, as I will need to re-do the process every time I revise the points in Mapsource and re-export.

quixoticgeek

  • Mostly Harmless
Re: Clean up GPX containing only Waypoints
« Reply #3 on: 30 July, 2021, 12:53:03 pm »
I'd also prefer to avoid too much notepad++ fiddling,

Use vi instead...

J
--
Beer, bikes, and backpacking
http://b.42q.eu/

Re: Clean up GPX containing only Waypoints
« Reply #4 on: 30 July, 2021, 12:57:08 pm »
There's never been a better time to learn grep!

Or alternatively gpsbabel can convert any GPS file to CSV.

Code: [Select]
gpsbabel -i gpx -f blah.gpx -o csv -F blah.txt
(completely untested)

Pingu

  • Put away those fiery biscuits!
  • Mrs Pingu's domestique
    • the Igloo
Re: Clean up GPX containing only Waypoints
« Reply #5 on: 30 July, 2021, 01:07:00 pm »
Excel  :P

Re: Clean up GPX containing only Waypoints
« Reply #6 on: 30 July, 2021, 03:06:58 pm »
Find / replace with a simple regular expression will sort it.  Most text editors accept regular expressions in the dialog boxes.

Feanor

  • It's mostly downhill from here.
Re: Clean up GPX containing only Waypoints
« Reply #7 on: 30 July, 2021, 03:21:47 pm »
Text editor approach would be fine for a one-of, but is not really something I'd want to use as a workflow.

I'm just going to write my own code.
Read a line, make some decisions, perhaps write the line out.

Feanor

  • It's mostly downhill from here.
Re: Clean up GPX containing only Waypoints
« Reply #8 on: 30 July, 2021, 06:40:53 pm »
Done. I have a working GPX Waypoint cleanup tool.

Quick-n-dirty, here's the core part of my code ( I'm using .net file and string functions ).
(I'm not a programmer, so go easy on my laughable code!)

If anyone wants a copy of either the final program, or the entire Visual Studio project, drop me a PM.

ETA: This tool is doing *exactly* what I wanted, and I'm really happy with it.

It reads in a GPX file, and passes it through un-changed, except WPT elements, which are parsed through and I only output the lines I want.
The Lat/Long are in the opening tags of the WPT in my example source GPX, so I'm not searching for those explicitly; they get included in the opening tag.

The only other tag I'm preserving is the name.
I've added the option to preserve timedate stamps, and elevations.

It is coded to work with my mapsource exports, and may barf with other GPX files which do their XML differently. I'm not attempting to parse the XML; I'm just doing line-by-line text editor stuff.

Code: [Select]
            using (StreamReader Input = new StreamReader(InFileName))
            {
                using (StreamWriter Output = new StreamWriter(OutFileName))
                {
                    //Read input file line by line
                    while ((CurrentLine = Input.ReadLine()) != null)
                    {
                        //Is this line the opening tag of a wpt?
                        if (CurrentLine.Trim().StartsWith("<wpt"))
                        {
                            //yes, so process the wpt up to it's closing tag.
                            WPT_count++;
                            //We already have the opening tag ,so write it out.
                            Output.WriteLine(CurrentLine);
                            //now continue reading lines till we find the closing tag
                            do
                            {
                                CurrentLine = Input.ReadLine();
                                //Write out only the Chosen Ones...
                                //Output.WriteLine(CurrentLine);
                                if (CurrentLine.Trim().StartsWith("<name")) Output.WriteLine(CurrentLine);
                                if (CurrentLine.Trim().StartsWith("<ele") && (checkBox2.Checked == true)) Output.WriteLine(CurrentLine);
                                if (CurrentLine.Trim().StartsWith("<time") && (checkBox1.Checked == true)) Output.WriteLine(CurrentLine);
                                //All other attributes in the wpt are ignored.
                            }
                            while (!(CurrentLine.Trim().StartsWith("</wpt")));
                            //we just exited the loop because we just read the closing tag. We must now write it out too.
                            Output.WriteLine(CurrentLine);
                        }
                        else
                        {
                            //no, it's not, eg header info so just copy it out.
                            Output.WriteLine(CurrentLine);
                        }
                    }
                }
            }