There might be situations where you need to convert KML files to GPX format or vice versa. For such cases, this article will teach you how to convert KML files to GPX format and GPX files to KML format using C#.
C# KML to GPX Converter API - Free Download
Aspose.GIS for .NET API allows you to render maps and create, read, and convert geographic data without additional software. Furthermore, the API allows you to convert KML files to GPX format and vice versa. You can either install the API through NuGet or download it directly from the Downloads section.
PM> Install-Package Aspose.GIS
Convert KML to GPX using C#
We can convert KML files to GPX format by following the steps given below:
- Create an instance of the ConversionOptions class.
- Assign the SpatialReferenceSystem.Wgs84 to the ConversionOptions object using the DestinationSpatialReferenceSystem property.
- Convert the KML file to GPX format using the VectorLayer.Convert(string sourcePath, FileDriver sourceDriver, string destinationPath, FileDriver destinationDriver, ConversionOptions options) method.
The following sample code shows how to convert KML files to GPX format using C#.
string dataDir = RunExamples.GetDataDir(); | |
string sourceFile = dataDir + "Kml_File.kml"; | |
string outputFile = dataDir + "output.gpx"; | |
// Specify conversion settings if necessary. It is optional. | |
ConversionOptions options = null; | |
// This options assigns Wgs84 to the destination layer. | |
// Conversion may throw error If destination layer does not support the Wgs84 spatial reference. So need to check. | |
if (Drivers.Shapefile.SupportsSpatialReferenceSystem(SpatialReferenceSystem.Wgs84)) | |
{ | |
options = new ConversionOptions() | |
{ | |
DestinationSpatialReferenceSystem = SpatialReferenceSystem.Wgs84, | |
}; | |
} | |
// Convert file format from KML to GPX. | |
VectorLayer.Convert(sourceFile, Drivers.Kml, outputFile, Drivers.Gpx, options); |
Convert GPX to KML using C#
We can also convert GPX files to KML format by following the given below steps:
- Create an instance of the ConversionOptions class.
- Assign the SpatialReferenceSystem.Wgs84 to the ConversionOptions object using the DestinationSpatialReferenceSystem property.
- Convert the GPX file to KML format using the VectorLayer.Convert(string sourcePath, FileDriver sourceDriver, string destinationPath, FileDriver destinationDriver, ConversionOptions options) method.
The following sample code shows how to convert a GPX file to KML format using C#.
string dataDir = RunExamples.GetDataDir(); | |
string sourceFile = dataDir + "schiehallion.gpx"; | |
string outputFile = dataDir + "output.kml"; | |
// Specify conversion settings if necessary. It is optional. | |
ConversionOptions options = null; | |
// This options assigns Wgs84 to the destination layer. | |
// Conversion may throw error If destination layer does not support the Wgs84 spatial reference. So need to check. | |
if (Drivers.Shapefile.SupportsSpatialReferenceSystem(SpatialReferenceSystem.Wgs84)) | |
{ | |
options = new ConversionOptions() | |
{ | |
DestinationSpatialReferenceSystem = SpatialReferenceSystem.Wgs84, | |
}; | |
} | |
// Convert file format from GPX to KML. | |
VectorLayer.Convert(sourceFile, Drivers.Gpx, outputFile, Drivers.Kml, options); |
Get a Free License
In order to try the API without evaluation limitations, you can get a free temporary license.
Conclusion
In this article, you have learned how to convert KML files to GPX format and vice versa using C#. The shared code snippets demonstrate how to achieve these conversions with just a few lines of code. Aspose.GIS for .NET is a powerful and feature-rich API that assists you in working with geospatial data. You can explore the API in detail by visiting the official documentation. In case of any questions, please feel free to reach us at our free support forum.