beacen.

Track conversion field answer 132

How to convert a GPX track to GeoJSON without joining gaps

The short answer

For a web map, turn each continuous GPX track segment into a line of longitude, latitude pairs. A track with several separate segments can become a GeoJSON MultiLineString. Keep waypoints as separate point features if you need them. Check the exported geometry and decide explicitly how to preserve elevations, times and extensions.

Reverse the written order when the destination requires it.

Take a GPX track point with latitude 54.420 and longitude −3.080. The named XML attributes tell you which number is which whatever their order in the file. A GeoJSON position uses array positions to convey that meaning. Written as JSON, this point starts [-3.080, 54.420]. Both numbers are small enough to pass a latitude range check in either order. A successful import alone will not detect the swap.

Download the worked GPX file and its GeoJSON counterpart. Both contain four invented points near the English Lake District for comparing encodings. Open the files in a text editor and find the first coordinate in both. For an unfamiliar route, compare the map extent with a known place before judging the shapes.

Four points can describe two lines.

The GPX contains two points in the first track segment and two in the second. You know how they are grouped, but the gap between groups has no recorded course. The matching GeoJSON feature has geometry type MultiLineString. Its first nested line contains the first pair of positions. Its second contains the other pair. A viewer sees two short lines with a space between them.

Flatten all four positions into one LineString and the viewer also draws a line from the end of the first pair to the start of the second. It has acquired a third edge without acquiring any new evidence. For a dataset in which each segment needs its own name or style, two LineString features in a FeatureCollection also preserve the break.

Choose which information besides the line must survive.

In GDAL, a GPX file exposes separate layers for tracks and for individual track points. To convert the example with the command-line tool, run ogr2ogr -f GeoJSON output.geojson two-segments.gpx tracks in a folder with the sample. Select the tracks layer when you want the line geometry. Select the track points separately if you also need a table of point times or sensor fields.

A GeoJSON feature can have properties. A single time on the feature cannot describe four separate point times. If you need per-point timing, choose and document a representation that your receiving software actually understands. Treat height with similar care because RFC 7946 specifies a vertical reference for the optional third coordinate. The downloadable example deliberately contains horizontal coordinates only. Recount its two lines and four positions after conversion, then inspect where it lands on a map.

Sources & scope

What this answer is based on.

The examples follow GPX 1.1 and RFC 7946. They show one geometry-preserving representation of a track. Both sample files are constructed data.

More in GPX files

Related questions