[GIS] How to split a GPX track file into several files of N trackpoints each

gpxsplittingtracking

I have some GPX track files, each containing tracks with thousands of trackpoints.

I need to split those files into multiple ones, each with 500 (or more, accordingly) points each.

Unfortunately I cannot simplify those tracks, I really need to split them.

As long as I could find, gpsbabel isn't able to do so. I am open to use any tool either on Linux (preferably) or Windows as well.

Best Answer

Surprisingly it seems gpsbabel indeed can't do it. But at least it can help by first converting to csv. Then I split the result with Unix' split and convert back to gpx with gpsbabel. A similar strategy might work with PowerShell, but I don't know if it has a split equivalent. Here is the bash function I used:

splitTrack() {
    # $1 shall be the gpx file to split
    pfx="${1%.*}-"
    gpsbabel -i gpx -f "$1" -t -o csv -F - \
    | split -d -l 500 --additional-suffix=.csv - "$pfx"

    for f in "$pfx"*.csv; do
        fout=${f%.*}.gpx
        gpsbabel -i csv -f "$f" -x transform,trk=wpt -x nuketypes,waypoints \
                            -o gpx -F "$fout"
        rm "$f"
    done
}

EDIT: Added hint from comments about getting track points out instead of way points.