在这个分步教程中,我们将向您展示如何在 C# 中将 GPX 转换为 KMZ。这将通过将 GPX 转换为 KML 格式,然后使用 C# 代码将 KML 转换为 KMZ 格式来实现。
在 C# 中将 GPX 转换为 KMZ 的步骤
- 从 NuGet.org 安装 Aspose.GIS for .NET 和 Aspose.Zip for .NET 包
- 包括 Aspose.Gis 和 Aspose.Zip 命名空间
- 使用 SetLicense 方法为两个 API 设置许可证
- 使用 VectorLayer class 将 GPX 格式转换为 KML 文件类型
- 创建 Archive class 的实例以创建 Zip 文件
- 将输出 KML 和其他相关文件添加为 Zip 条目
- 将 KML 和其他文件另存为单个 Zip 文件
- 将最终文件重命名为 KMZ(KML 压缩格式)
KMZ 文件格式是一种压缩格式,不仅包含 KML 地图文件,还包含图像、音频和其他格式的相关文件。所以我们首先需要转换GPX file to KML,然后再将KML转换为KMZ地图文件格式。
在 C# 中将 GPX 转换为 KMZ 的代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} |
在此代码段中,将 GPX 转换为 KM 格式后,我们将创建 KML 文件的 Zip 存档以及图像文件。这仅用于示例目的,您可以将与您关注的 KML 文件相关的任何文件添加到 KMZ 包中。