Read GPX Files using C#

A GPX file is a GPS data file contains GPS data saved in the GPS Exchange Format. It is an XML schema to describe geographic information such as waypoints, tracks, routes, etc. It allows transferring GPS data between GPS units and software applications. We can easily load the GPX file and extract GPS information programmatically in .NET applications. In this article, we will learn how to read GPX files using C#.

The article shall cover the following topics:

  1. What is a GPX File
  2. C# API to Read GPX Files
  3. Read Waypoints from the GPX File
  4. Read Routes from the GPX File
  5. Extract Tracks from GPX File
  6. Read GPX Nested Attributes

What is a GPX File

GPX, or GPS Exchange Format, is a file format that allows you to exchange GPS data between different devices and software programs. GPX files are just XML files that contain GPS data, and can be created and edited with any text editor. GPX files can contain waypoints, tracks, and routes, and can be used to plan and share your trips with others. Waypoints are simply locations that you want to remember, such as your current location or the location of a specific destination. Tracks are a series of points that represent your movement over time, and can be used to retrace your steps or track your progress on a longer journey. Routes are a series of waypoints that represent a planned trip, and can be used to navigate from one point to another. GPX files are a great way to share your GPS data with others, and can be used by many GPS devices and software programs.

C# API to Read GPX Files

For reading features from GPX files, we will be using the Aspose.GIS for .NET API. It allows you to render maps, and create, read, and convert geographic data without additional software. It also enables you to convert KML files to GPX format and vice versa.

Please either download the DLL of the API or install it using NuGet.

PM> Install-Package Aspose.GIS

Read Waypoints from the GPX File in C#

We can read waypoints from the GPX file as Point Geometry by following the steps given below:

  1. Firstly, load the GPX file using the OpenLayer method.
  2. Next, for each feature in the layer, check if GeometryType is Point.
  3. After that, get the geometry of the feature as a Point.
  4. Finally, show X and Y coordinate points.

The following code sample shows how to read waypoints from a GPX file using C#.

POINT (-90.29408 38.63473) X: -90.29408 Y: 38.63473
POINT (-90.28679 38.63368) X: -90.28679 Y: 38.63368
POINT (-90.29323 38.63408) X: -90.29323 Y: 38.63408
POINT (-90.29019 38.63533) X: -90.29019 Y: 38.63533
POINT (-90.28976 38.63677) X: -90.28976 Y: 38.63677
POINT (-90.28948 38.63496) X: -90.28948 Y: 38.63496
POINT (-90.29458 38.63421) X: -90.29458 Y: 38.63421
POINT (-90.29083 38.63633) X: -90.29083 Y: 38.63633
POINT (-90.28715 38.63395) X: -90.28715 Y: 38.63395
POINT (-90.28769 38.63347) X: -90.28769 Y: 38.63347

Read Routs from the GPX File in C#

We can read routes from the GPX file as Line String geometry by following the steps given below:

  1. Firstly, load the GPX file using the OpenLayer method.
  2. Next, for each feature in the layer, check if GeometryType is LineString.
  3. After that, get the geometry of the feature as LineString.
  4. Finally, show X, Y, and Z coordinate points.

The following code sample shows how to read routes from a GPX file using C#.

=====================================================
 X: -4.03601769647726 Y: 56.6758328268945 Z: 351.247702398777
 X: -4.03583038137853 Y: 56.6753865835736 Z: 344.690721458414
 X: -4.03614000315429 Y: 56.6735618299578 Z: 349.066837113628
 X: -4.03711323311608 Y: 56.6726922276694 Z: 352.76479861559
 X: -4.03921535478461 Y: 56.6708156570976 Z: 358.078238232484
 X: -4.04184722532733 Y: 56.668930361342 Z: 371.315914270806
 X: -4.04446052766014 Y: 56.668213511889 Z: 372.334546538997
 X: -4.04552528394144 Y: 56.6682858833434 Z: 398.610199355698
 X: -4.04660281552745 Y: 56.6678413316366 Z: 439.24188764472
 X: -4.04765411258453 Y: 56.6661616045966 Z: 430.695575764036
.
.
.

Extract Tracks from the GPX File in C#

We can read tracks from the GPX file as MultiLineString geometry by following the steps given below:

  1. Firstly, load the GPX file using the OpenLayer method.
  2. Next, for each feature in the layer, check if GeometryType is MultiLineString.
  3. After that, get the geometry of the feature as MultiLineString.
  4. Finally, show the tracks.

The following code sample shows how to read tracks from a GPX file using C#.

LINESTRING (0 0, 1 1, 2 2, 3 3)
LINESTRING EMPTY
LINESTRING EMPTY
LINESTRING (10 10, 11 11, 12 12, 13 13)

Read GPX Nested Attributes in C#

We can read features for each point in the segment and extract nested attribute values by following the steps given below:

  1. Firstly, create an instance of the GpxOptions class.
  2. Next, set the ReadNestedAttributes to true.
  3. Then, load the GPX file using the OpenLayer() method with the GpxOptions object as an argument.
  4. Next, for each feature in the layer, check if GeometryType is MultiLineString.
  5. Then, get the geometry of the feature as MultiLineString.
  6. After that, read the segment as LineString from each MultiLineString.
  7. Finally, read points in the segment and show the attribute values.

The following code sample shows how to read GPX nested attributes from a GPX file using C#.

Get a Free License

You can get a free temporary license to try the library without evaluation limitations.

Conclusion

In this article, we have learned how to:

  • load a GPX file using OpenLayer in C#;
  • read waypoints, routes, and tracks from the GPX file programmatically;
  • read the nested attribute from a GPX file using C#.

Besides reading GPX files in C#, you can learn more about Aspose.GIS for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also