ในบทช่วยสอนทีละขั้นตอนนี้ เราจะแสดงวิธีแปลง GPX เป็น KMZ ใน C# ซึ่งจะทำได้โดยการแปลงรูปแบบ GPX เป็น KML จากนั้นจึงแปลง KML เป็นรูปแบบ KMZ โดยใช้โค้ด C#
ขั้นตอนในการแปลง GPX เป็น KMZ ใน C#
- ติดตั้งแพ็คเกจ Aspose.GIS for .NET และ Aspose.Zip for .NET จาก NuGet.org
- รวมเนมสเปซ Aspose.Gis และ Aspose.Zip
- ตั้งค่าใบอนุญาตสำหรับ API ทั้งสองโดยใช้เมธอด SetLicense
- ใช้ VectorLayer class เพื่อแปลงรูปแบบ GPX เป็นประเภทไฟล์ KML
- สร้างอินสแตนซ์ของ Archive class เพื่อสร้างไฟล์ Zip
- เพิ่มเอาต์พุต KML และไฟล์อื่นๆ ที่เกี่ยวข้องเป็นรายการ Zip
- บันทึก KML และไฟล์อื่นๆ เป็นไฟล์ Zip ไฟล์เดียว
- เปลี่ยนชื่อไฟล์สุดท้ายเป็น KMZ (รูปแบบ KML Zip)
รูปแบบไฟล์ KMZ เป็นรูปแบบซิปที่มีไฟล์แผนที่ KML ไม่เพียงเท่านั้น แต่ยังรวมถึงไฟล์ที่เกี่ยวข้อง เช่น รูปภาพ เสียง และรูปแบบอื่นๆ ดังนั้น ก่อนอื่นเราต้องแปลง GPX file to KML แล้วจึงแปลงรูปแบบไฟล์แผนที่ KML เป็น KMZ
รหัสเพื่อแปลง GPX เป็น KMZ ใน 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"); | |
} | |
} | |
} |
ในข้อมูลโค้ดนี้ หลังจากแปลง GPX เป็นรูปแบบ KM แล้ว เรากำลังสร้างไฟล์ Zip ของไฟล์ KML พร้อมกับไฟล์รูปภาพ นี่เป็นเพียงตัวอย่างเท่านั้น คุณสามารถเพิ่มไฟล์ใดๆ ที่เกี่ยวข้องกับไฟล์ KML ที่เกี่ยวข้องของคุณลงในแพ็คเกจ KMZ