A GPX file contains GPS data saved in 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:
- C# API to Read GPX Files
- Read Waypoints from the GPX File
- Read Routes from the GPX File
- Extract Tracks from GPX File
- Read GPX Nested Attributes
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:
- Firstly, load the GPX file using the OpenLayer method.
- Next, for each feature in the layer, check if GeometryType is Point.
- After that, get the geometry of the feature as a Point.
- 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:
- Firstly, load the GPX file using the OpenLayer method.
- Next, for each feature in the layer, check if GeometryType is LineString.
- After that, get the geometry of the feature as LineString.
- 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:
- Firstly, load the GPX file using the OpenLayer method.
- Next, for each feature in the layer, check if GeometryType is MultiLineString.
- After that, get the geometry of the feature as MultiLineString.
- 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:
- Firstly, create an instance of the GpxOptions class.
- Next, set the ReadNestedAttributes to true.
- Then, load the GPX file using the OpenLayer() method with the GpxOptions object as an argument.
- Next, for each feature in the layer, check if GeometryType is MultiLineString.
- Then, get the geometry of the feature as MultiLineString.
- After that, read the segment as LineString from each MultiLineString.
- 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, 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 the forum.