如何在 C# 中将 SHP 转换为 GPX

本快速教程通过提供配置详细信息、逐步过程和可运行的示例代码,指导如何在 C# 中将 SHP 转换为 GPX。您也可以使用一行代码执行此任务,但示例代码也将演示设置转换选项。这个在 C#** 中将 **Shapefile 转换为 GPX 的过程可以在任何安装了 .NET 的基于 Windows 的系统或具有 .NET Core 的 Linux/macOS 系统上执行。

在 C# 中将 Shape 文件转换为 GPX 的步骤

  1. 从 NuGet 包管理器安装 Aspose.GIS for .NET 以将 SHP 文件转换为 GPX
  2. 实例化 ConversionOptions 类对象以自定义转换过程
  3. 检查您的 Shapefile 驱动程序是否支持特定的空间参考系统
  4. 设置所需空间参考系统的选项
  5. 使用 VectorLayer class 中的 Convert 方法将 SHP 文件转换为 GPX

这些步骤通过识别完成任务所需的引用命名空间和类来解释将形状文件转换为 C# 中的 GPX 的过程。这个过程可以简单地使用 VectorLayer 类中的 Convert 方法完成,但是探索了不同的选项来自定义转换过程。它还提供信息以检查所选驱动程序是否支持特定的空间参考系统。

C# 中 SHP 到 GPX 转换器的代码

using System;
using Aspose.Gis;
using Aspose.Gis.SpatialReferencing;
namespace AsposeProjects
{
class Program
{
static void Main(string[] args) // Main function to convert SHP to GPX
{
// Initialize a license
Aspose.Gis.License lic = new Aspose.Gis.License();
lic.SetLicense(@"Aspose.Total.lic");
// Instantiate ConversionOptions object
ConversionOptions options = null;
// Set the desired spatial reference system
SpatialReferenceSystem spatialReferenceSystem = SpatialReferenceSystem.Wgs84;
// Check if target driver supports the selected spatial reference system
if (Drivers.Shapefile.SupportsSpatialReferenceSystem(spatialReferenceSystem))
{
options = new ConversionOptions()
{
DestinationSpatialReferenceSystem = spatialReferenceSystem,
};
}
try
{
// Convert SHP to GPX
VectorLayer.Convert("Sample.shp", Drivers.Shapefile, "Output.gpx", Drivers.Gpx, options);
}
catch(Exception)
{
System.Console.WriteLine( $"{spatialReferenceSystem} not supported" );
}
System.Console.WriteLine("Done");
}
}
}

此代码演示了在 C#* 中*将 SHP 转换为 GPX 的过程。您可以使用特定的驱动程序来处理不同的文件格式,例如 GeoJson、Kml、Shapefile、OsmXml 和 Gml,仅举几个例子。此代码使用了空间参考系统 Wgs84,但是您可以根据目标层提供的支持使用其他选项,例如 Wgs72、WebMercator、Etrs89LambertConformalConic、Etrs89 等。

在本教程中,我们学习了 SHP 到 GPX 的转换。如果您想了解将 GPX 转换为 KMZ 的过程,请参阅 如何在 C# 中将 GPX 转换为 KMZ 上的文章。

 简体中文