In this step by step tutorial, we’ll show you how to convert GPX to KMZ in C#. This will be achieved by converting GPX to KML format and then converting KML to KMZ format using C# code.
Steps to Convert GPX to KMZ in C#
- Install Aspose.GIS for .NET and Aspose.Zip for .NET packages from NuGet.org
- Include Aspose.Gis and Aspose.Zip namespaces
- Set license for both APIs using SetLicense method
- Use VectorLayer class to convert GPX format to KML file type
- Create an instance of Archive class to create a Zip file
- Add output KML and other related files as Zip entries
- Save KML and other files as a single Zip file
- Rename the final file as KMZ (KML Zipped format)
The KMZ file format is the zipped format containing not only KML map file but also related files like images, audio, and other formats. So we first need to convert the GPX file to KML and then convert the KML to KMZ map file format.
Code to Convert GPX to KMZ in C#
using System; | |
//Add reference to Aspose.GIS for .NET & Aspose.Zip for .NET APIs | |
//Use following namespaces to convert GPX file format to KMZ format | |
using Aspose.Gis; | |
using Aspose.Zip; | |
namespace ConvertGPXToKMZFileFormat | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Set Aspose license before converting GPX file to KMZ format | |
//using Aspose.GIS for .NET | |
Aspose.Gis.License AsposeGISLicense = new Aspose.Gis.License(); | |
AsposeGISLicense.SetLicense(@"c:\asposelicense\license.lic"); | |
//Set Aspose license to use Aspose.Zip to zip KML file | |
Aspose.Zip.License AsposeZipLicense = new Aspose.Zip.License(); | |
AsposeZipLicense.SetLicense(@"c:\asposelicense\license.lic"); | |
//Convert GPX file to KML File | |
VectorLayer.Convert("InputGPXFile.gpx", Drivers.Gpx, "OutputKMLFile.kml", Drivers.Kml); | |
//Create Archive class instance | |
Archive ZipArchive = new Archive(); | |
//Create entry for each file in the zip archive | |
ZipArchive.CreateEntry("OutputKMLFile.kml", "OutputKMLFile.kml"); | |
ZipArchive.CreateEntry("ImageRelatedToKMLFile.png", "ImageRelatedToKMLFile.png"); | |
//Save output Zip file | |
ZipArchive.Save("KMLandImageFilesCombined.zip"); | |
//Rename Zip file to KMZ | |
System.IO.File.Move("KMLandImageFilesCombined.zip", "FinalOutputKMZFile.kmz"); | |
} | |
} | |
} |
In this code snippet, after converting GPX to KM format, we’re creating a Zip archive of the KML file along with an image file. This is just for sample purpose, you can add any files which are related to your concerned KML file into a KMZ package.